Why doesn't this work?

C

Chris Lount

-----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+ybcOy0AcBACdHrUc
X3eTqKmgH6XGCCfw7u5u/9g=
=JgsO
-----END PGP SIGNATURE-----
 
R

Rolf Magnus

Chris said:
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>

though you actually should. Why said:
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;

}
 
C

Chris Lount

Rolf said:
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().


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;

}
 
P

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.
 
P

Peter van Merkerk

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top