Connecting Tech Pros Worldwide Help | Site Map

Why doesn't this work?

Chris Lount
Guest
 
Posts: n/a
#1: Jul 19 '05
-----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-----
Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Why doesn't this work?


Chris Lount wrote:
[color=blue]
> 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?[/color]

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().
[color=blue]
> #include<iostream>
> #include<string>[/color]

You're not using anything from <string>, though you actually should. Why
aren't you using std::string instead of char arrays?
[color=blue]
> 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;
>
> }[/color]

Chris Lount
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Why doesn't this work?


Rolf Magnus wrote:
[color=blue]
> Chris Lount wrote:
>[color=green]
>> 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?[/color]
>
> 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().
>[color=green]
>> #include<iostream>
>> #include<string>[/color]
>
> You're not using anything from <string>, though you actually should. Why
> aren't you using std::string instead of char arrays?[/color]

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.

[color=blue]
>[color=green]
>> 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;
>>
>> }[/color][/color]

PT
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Why doesn't this work?


> Hi, I'm pretty new to c++ . I'm trying to work out why the following code[color=blue]
> 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?[/color]

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.
Peter van Merkerk
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Why doesn't this work?


> > You're not using anything from <string>, though you actually should. Why[color=blue][color=green]
> > aren't you using std::string instead of char arrays?[/color]
>
> 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[/color]
more[color=blue]
> info on the standard functions.[/color]

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




Closed Thread