473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why doesn't this work?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi, I'm pretty new to c++ . I'm trying to work out why the following code
doesn't work.
I've just learned about cin.get() and written the following program to enter
a string and store it in an array.

When I run the program it asks for the first string and then prints it.
However, it doesn't ask for input for the second string, it simply prints
"Enter the second string", then "The second string is: ".

Why doesn't cin.get() ask for input the second time around?

Thanks alot for any help

Chris.

#include<iostream>
#include<string>

int main()
{
char bufferOne[50];
std::cout << "Enter the first string: ";
std::cin.get( bufferOne, 49 );
std::cout << "The first string is " << bufferOne << std::endl;

char bufferTwo[50];
std::cout << "Enter the second string: ";
std::cin.get( bufferTwo, 49 );
std::cout << "The second string is " << bufferTwo << std::endl;

return 0;

}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE/TncRrAxy75AP1L8RAneAAJ91c46HwkcLcCJfCk7+ybcOy0AcBA CdHrUc
X3eTqKmgH6XGCCfw7u5u/9g=
=JgsO
-----END PGP SIGNATURE-----
Jul 19 '05 #1
4 3821
Chris Lount wrote:
Hi, I'm pretty new to c++ . I'm trying to work out why the following
code doesn't work.
I've just learned about cin.get() and written the following program to
enter a string and store it in an array.

When I run the program it asks for the first string and then prints
it. However, it doesn't ask for input for the second string, it simply
prints "Enter the second string", then "The second string is: ".

Why doesn't cin.get() ask for input the second time around?
get() reads until (but not including) the first \n character. That
character is left in the input buffer. When you use get() again, the
first character that is found is again that \n, so nothing is read. Use
getline() instead of get().
#include<iostream>
#include<string>
You're not using anything from <string>, though you actually should. Why
aren't you using std::string instead of char arrays?
int main()
{
char bufferOne[50];
std::cout << "Enter the first string: ";
std::cin.get( bufferOne, 49 );
std::cout << "The first string is " << bufferOne << std::endl;

char bufferTwo[50];
std::cout << "Enter the second string: ";
std::cin.get( bufferTwo, 49 );
std::cout << "The second string is " << bufferTwo <<
std::endl;

return 0;

}


Jul 19 '05 #2
Rolf Magnus wrote:
Chris Lount wrote:
Hi, I'm pretty new to c++ . I'm trying to work out why the following
code doesn't work.
I've just learned about cin.get() and written the following program to
enter a string and store it in an array.

When I run the program it asks for the first string and then prints
it. However, it doesn't ask for input for the second string, it simply
prints "Enter the second string", then "The second string is: ".

Why doesn't cin.get() ask for input the second time around?
get() reads until (but not including) the first \n character. That
character is left in the input buffer. When you use get() again, the
first character that is found is again that \n, so nothing is read. Use
getline() instead of get().
#include<iostream>
#include<string>


You're not using anything from <string>, though you actually should. Why
aren't you using std::string instead of char arrays?


I'm still new, the only string functions i know are getlen() strcpy() and
strncpy(). I'm currently going through my Linux info pages to find out more
info on the standard functions.

Thanks for the help, I'll look into cin.getline() and also explore the
functions in string a bit more.

int main()
{
char bufferOne[50];
std::cout << "Enter the first string: ";
std::cin.get( bufferOne, 49 );
std::cout << "The first string is " << bufferOne << std::endl;

char bufferTwo[50];
std::cout << "Enter the second string: ";
std::cin.get( bufferTwo, 49 );
std::cout << "The second string is " << bufferTwo <<
std::endl;

return 0;

}


Jul 19 '05 #3
PT
> Hi, I'm pretty new to c++ . I'm trying to work out why the following code
doesn't work.
I've just learned about cin.get() and written the following program to enter
a string and store it in an array.

When I run the program it asks for the first string and then prints it.
However, it doesn't ask for input for the second string, it simply prints
"Enter the second string", then "The second string is: ".

Why doesn't cin.get() ask for input the second time around?


The problem is, because cin.get() reads characters from stdin until it
encounters a newline ('\n'). Then it finishes and writes a terminating 0 at
the end of the string. But the newline is pushed back to the input stream.
:-(
Then you run your second cin.get(). It starts reading characters from the
input stream and what does it notice first? The first character available
is '\n'! That means that we must stop and write the terminating 0!
That is the problem!

The simplest solution would be:

#include<iostream>

// #include<string> is not necessary here!

int main()
{
char bufferOne[50];
std::cout << "Enter the first string: ";
std::cin.get( bufferOne, 49 );
std::cout << "The first string is " << bufferOne << std::endl;

//OK, let's just read the remaining '\n'!
std::cin.get();
char bufferTwo[50];
std::cout << "Enter the second string: ";
std::cin.get( bufferTwo, 49 );
std::cout << "The second string is " << bufferTwo << std::endl;

return 0;

}

I know that there are other solutions also, but mine worked fine...
I hope it is useful for you.
Jul 19 '05 #4
> > You're not using anything from <string>, though you actually should. Why
aren't you using std::string instead of char arrays?
I'm still new, the only string functions i know are getlen() strcpy() and
strncpy(). I'm currently going through my Linux info pages to find out

more info on the standard functions.


Forget about those functions, there are remnants from C and in most cases
not needed in C++. The std::string class is much easier and safer to use:

#include<iostream>
#include<string>

int main()
{
std::string str1;
std::cout << "Enter the first string: ";
std::getline(std::cin, str1);
std::cout << "The first string is " << str1 << std::endl;

std::string str2;
std::cout << "Enter the second string: ";
std::getline(std::cin, str2);
std::cout << "The second string is " << str2 << std::endl;

return 0;
}

You see, no need to worry about the length of the string. I recommend you
get a good C++ beginners book; learning C++ from just info pages is going to
be a very slow and painfull process.

Good luck with your study!
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
39
by: Mark Johnson | last post by:
It doesn't seem possible. But would the following also seem a violation of the general notions behind css? You have a DIV, say asociated with class, 'topdiv'. Inside of that you have an anchor...
3
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
4
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't...
3
by: Dave Moore | last post by:
Hi All, Ok, here's my problem. I want to open a file and process its contents. However, because it is possible that the file may not exist, I also want to check whether the file() function is...
10
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
6
by: Johnny Jörgensen | last post by:
I've got a usercontrol derived from a normal ComboBox that contains some special formatting code. On my main form I've got a lot of my custom comboboxes. I discovered a bug in the derived...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.