-
getline problem
I dont know if the problem is the way I use getline.
If I run this little program, it is supposed to read a string, a number...5 times, but it does not, after the second atepmt,
the program goes crazy.
I tried a fix I was given (STLFix), and still doesn't work,
I found an article in microsoft to solve the getline bug, but
I can't find where to fix it, how do I get to the header to
change it ?
http://support.microsoft.com/default...;EN-US;q240015
anyway, this is the little program, it is getline or the program
the one that doesn't work?
Code:
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
main()
{
string str;
int num;
for(int i=0;i<5;i++){
cout<<endl<<"String ";
getline(cin, str, '\n');
cout<<"Number ";
cin>>num;
}
return 0;
}
thank you if you guys can be VERY explicit with your guidelines.
-
Well, first of all, main is defined to return an int. So, it's int main not just main.
Try adding a cin.clear() and a cin.ignore(80, '\n'). This will clear the error state and ignore any left over characters in the input buffer. I'm assuming that the program is skipping the input. Is this correct?
-
yes
yes, it pretty much does it, thank u, the cin.clear doesn't
seem to affect the program though. Only one thing, the
first time I still have to press enter twice.
-
Re: getline problem
Quote:
Originally posted by jriano
I dont know if the problem is the way I use getline.
If I run this little program, it is supposed to read a string, a number...5 times, but it does not, after the second atepmt,
the program goes crazy.
I tried a fix I was given (STLFix), and still doesn't work,
I found an article in microsoft to solve the getline bug, but
I can't find where to fix it, how do I get to the header to
change it ?
http://support.microsoft.com/default...;EN-US;q240015
anyway, this is the little program, it is getline or the program
the one that doesn't work?
Code:
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
main()
{
string str;
int num;
for(int i=0;i<5;i++){
cout<<endl<<"String ";
getline(cin, str, '\n');
cout<<"Number ";
cin>>num;
}
return 0;
}
thank you if you guys can be VERY explicit with your guidelines.
It's on line 165 (god why do I remember this) of the string header file. Change snextc to sbumpc. You can easily open the file by clicking on the #include <string> directive and selecting "open document string" or something similar.