Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why doesn't this work?

Reply
Thread Tools

Why doesn't this work?

 
 
Chris Lount
Guest
Posts: n/a
 
      08-30-2003
-----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-----
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      08-30-2003
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;
>
> }


 
Reply With Quote
 
 
 
 
Chris Lount
Guest
Posts: n/a
 
      08-30-2003
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;
>>
>> }


 
Reply With Quote
 
PT
Guest
Posts: n/a
 
      08-30-2003
> 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.
 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      08-30-2003
> > 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57