| | |
string input not working as expected
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
C++ Syntax (Toggle Plain Text)
if(cmd=="new"){ string name,desc; cout<<"Name? "; cin>>name; Setcursor(Conloc); cout<<"Desc? "; cin>>desc; Setcursor(Bodyloc); Invitem Item(name,desc);
When the values of the string desc is set through cin, if a space is used, all text after that space isn't used. Also, If two words are used when setting name, it will give the second word to desc and skip that. I tried flushing the input buffer with cout.flush(), but it didn't work. What am I to do here?
Thanks!
Squirrel Prodigy
•
•
•
•
Originally Posted by SquirrelProdigy
C++ Syntax (Toggle Plain Text)
if(cmd=="new"){ string name,desc; cout<<"Name? "; cin>>name; Setcursor(Conloc); cout<<"Desc? "; cin>>desc; Setcursor(Bodyloc); Invitem Item(name,desc);
When the values of the string desc is set through cin, if a space is used, all text after that space isn't used. Also, If two words are used when setting name, it will give the second word to desc and skip that. I tried flushing the input buffer with cout.flush(), but it didn't work. What am I to do here?
Thanks!
I could barely decipher what the heck you were saying. In the future could you please be a bit more articulate. That's the least you could do in return for help.
Now, let's see.... You have two variables, name and desc and you're having the problem where you might want to specify a first and last name as well as a description containing more then one word.
My understanding is that by default C++ cin streams ignore whitespace, carriage returns, and tabs.
So, let's say your input is "John Doe", with your code we'll first do a "cin>>name". You will read the stream, one character at a time, filling "name". Once you come upon an ignored character (the whitespace right after the 'n' in "John") you'll stop, making name equal to "John". Then, when you read from the stream again with "cin>>desc", you will read the stream, one character at a time, filling desc. Once you come upon an ignored character (carriage return right after the 'e' in "Doe") you'll stop, making desc equal to "Doe".
A simple solution to your problem is to use getline() by which you can specify "read the stream up to N characters or until we reach our delimiter" By default, our delimiter is "\n" (newline)
#include <iostream>
using namespace std;
int main()
{
char name[30];
char desc[256];
cout << "Name? ";
cin.getline(name, 30);
cout << "Desc? " << endl;
cin.getline(desc, 256);
cout << "Name: " << name << endl;
cout << "Desc: " << endl;
cout << desc << endl;
return 0;
} cin's operator>> uses whitespace as a delimiter:
>I tried flushing the input buffer with cout.flush()
What made you think that calling the flush member function of an output stream would somehow "flush" the input stream? I think you're confused about the definition of flush, and the relationship of cin and cout.
To flush a stream is to write the contents of the stream buffer to the external device for an output stream. This in no way implies that flushing an input stream will somehow discard the contents of the stream. In fact, basic_istream doesn't even have a flush member function because it's nonsensical.
cin and cout are related, but only because they are tied together so that any request for input using cin will automatically flush cout. Notice that any unwritten characters in cout's stream buffer are written to the output device. No part of this has an effect on cin's stream buffer.
So calling cout.flush() will force the stream buffer for cout to be written to the output device. It won't affect cin in any way. The proper way to "flush" the input stream is with basic_istream's ignore member function:
A good way to "flush" the input stream without using arbitrary magic numbers is using numeric_limits, defined in <limits>:
>What am I to do here?
The general rule of thumb is that if you want to use whitespace as a separater, use operator>>. Otherwise use getline.
>you might be able to put " " around what you're typing
Then again, you might not. How the shell works is beyond the scope of C++, so it's hard to guarantee a correct and portable program when you rely on it.
C++ Syntax (Toggle Plain Text)
string s; cin>> s; // Type "John Doe" cout<< s; // Prints "John". "Doe" left in the stream
What made you think that calling the flush member function of an output stream would somehow "flush" the input stream? I think you're confused about the definition of flush, and the relationship of cin and cout.
To flush a stream is to write the contents of the stream buffer to the external device for an output stream. This in no way implies that flushing an input stream will somehow discard the contents of the stream. In fact, basic_istream doesn't even have a flush member function because it's nonsensical.
cin and cout are related, but only because they are tied together so that any request for input using cin will automatically flush cout. Notice that any unwritten characters in cout's stream buffer are written to the output device. No part of this has an effect on cin's stream buffer.
So calling cout.flush() will force the stream buffer for cout to be written to the output device. It won't affect cin in any way. The proper way to "flush" the input stream is with basic_istream's ignore member function:
C++ Syntax (Toggle Plain Text)
cin.ignore(); // Discard one character cin.ignore(80); // Discard up to 80 characters or EOF cin.ignore(80, '\n'); // Discard up to 80 characters or a newline
C++ Syntax (Toggle Plain Text)
cin.ignore(numeric_limits<streamsize>::max(), '\n');
The general rule of thumb is that if you want to use whitespace as a separater, use operator>>. Otherwise use getline.
>you might be able to put " " around what you're typing
Then again, you might not. How the shell works is beyond the scope of C++, so it's hard to guarantee a correct and portable program when you rely on it.
I'm here to prove you wrong.
Oh and please take note of the subtle hint that our dear friend Narue left us: stream input, NOT string input.
It's trully important as a developing programmer to fully understand and internalize such concepts. Do you know what a stream is? Do you know how a stream is implemented? Learning by example can only get you so far, insight takes you the rest of the way, IMHO.
It's trully important as a developing programmer to fully understand and internalize such concepts. Do you know what a stream is? Do you know how a stream is implemented? Learning by example can only get you so far, insight takes you the rest of the way, IMHO.
>Do you know what a stream is?
That's a good concept to understand.
>Do you know how a stream is implemented?
Most programmers need not know or care how streams are implemented. It's sufficiently complicated enough to be abstracted away for everyone except those who have a need to write a custom stream.
That's a good concept to understand.
>Do you know how a stream is implemented?
Most programmers need not know or care how streams are implemented. It's sufficiently complicated enough to be abstracted away for everyone except those who have a need to write a custom stream.
I'm here to prove you wrong.
•
•
•
•
>Do you know how a stream is implemented?
Most programmers need not know or care how streams are implemented. It's sufficiently complicated enough to be abstracted away for everyone except those who have a need to write a custom stream.
>I think it can seperate a good programmer from an expert programmer
No debate there.
>they also know how the mechanism works
We still agree.
>It's my belief that all programmers should strive for this type of excellence.
Yes, of course. But, not all programmers are as addicted to the craft as you and I, and it would be foolish of us to expect them to be. Most programmers couldn't care less how good they are as long as they're good enough to do the job. For them, "good" is good enough and "expert" requires too much work learning things that they just aren't going to use. So, for average programmers, who form the majority, there's no need to learn how streams are implemented unless they intend to implement a stream.
>But... this is a C programmer talking
And this is a C programmer responding.
No debate there.
>they also know how the mechanism works
We still agree.
>It's my belief that all programmers should strive for this type of excellence.
Yes, of course. But, not all programmers are as addicted to the craft as you and I, and it would be foolish of us to expect them to be. Most programmers couldn't care less how good they are as long as they're good enough to do the job. For them, "good" is good enough and "expert" requires too much work learning things that they just aren't going to use. So, for average programmers, who form the majority, there's no need to learn how streams are implemented unless they intend to implement a stream.
>But... this is a C programmer talking
And this is a C programmer responding.
I'm here to prove you wrong.
•
•
•
•
Originally Posted by subtronic
It's my belief that all programmers should strive for this type of excellence.
Squirrel Prodigy
![]() |
Similar Threads
- user input into a string (C++)
- string input (Java)
- Use string input for enum (C++)
- string input segmentation fault on linux (C++)
- Masking a string input (Python)
Other Threads in the C++ Forum
- Previous Thread: Help with < (less than) operator on classes
- Next Thread: MP3 Player
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






