String problem after space
Hello,
In some functions of my program this code doesn`t work normally
Code:
string w1;
string w2;
cout<<"1st msg: Enter 2 words: ";
getline(cin,w1);
cout<<endl;
cout<<"2nd msg: Enter 2 words: ";
getline(cin,w2);
cout<<endl;
cout<<endl<<"w1: "<<w1<<endl;
cout<<"w2: "<<w2<<endl<<endl;
But when i run it is some functions(except main and some others) of my program shows other output.
Code:
1st msg: Enter 2 words:
2nd msg: Enter 2 words: word1 word2
w1:
w2: word1 word2
My variables are again local, any ideas whats the problem ?
Re: String problem after space
Please post the entire program. There is probably some other stdin operation that you didn't post. We can only guess as to what the problem is but it sounds like it is one of those asked and answered about a million times questions. The below example is using cin but you get the same problem if you use cin for an integral value followed by getline for a string. There are certain types of input operations that leave a '\n' in the input stream which causes the next getline to read a blank line.
http://www.parashift.com/c++-faq-lit....html#faq-15.6
Re: String problem after space
I send you the code because is very long to post it here.
Re: String problem after space
I don't know whether kempofighter has exposed his e-mail address on his property page, but regardless of that I think you should prefer to attach the .cpp file to a post, what you can do unless it's larger than 97.7 kB.
Re: String problem after space
It is difficult to tell without seeing your code, but you are probably
using operator >> somewhere before the getline ... The '\n' from
the operator >> input is still in the stream buffer.
Example:
Code:
int n;
string s;
cin >> n; // '\n' still in buffer
getline(cin,s); // reads the '\n'
The simplest fix:
Code:
cin >> n; // '\n' still in buffer
cin.ingore(100,'\n');
getline(cin,s);
Re: String problem after space
The bottom line is that anytime you use operator>> you have to clear up the input stream when you are finished, otherwise you will experience unexpected behavior in other parts of the program. You should also read other parts of that FAQ on error checking. If you are properly reading from the input stream and handling errors in each and every case this problem shouldn't occur.
You don't have to post the whole thing. Simply create a small example that demonstrates the problem. During that process you may discover the problem for yourself. If not, post the example. That is the best way to use these forums. Search forum for similar problem, duplicate with small example, and then post example plus question.
Re: String problem after space
Quote:
Originally Posted by
1024mbyte
Hello,
In some functions of my program this code doesn`t work normally
Code:
string w1;
string w2;
cout<<"1st msg: Enter 2 words: ";
getline(cin,w1);
cout<<endl;
cout<<"2nd msg: Enter 2 words: ";
getline(cin,w2);
cout<<endl;
cout<<endl<<"w1: "<<w1<<endl;
cout<<"w2: "<<w2<<endl<<endl;
But when i run it is some functions(except main and some others) of my program shows other output.
Code:
1st msg: Enter 2 words:
2nd msg: Enter 2 words: word1 word2
w1:
w2: word1 word2
My variables are again local, any ideas whats the problem ?
What is the problem? It looks like you just hit {ENTER} at the first prompt, and "word1 word2{ENTER}" at the second prompt. The output confirms that this is what happened. :confused:
Viggy