WTF? Need help with getline!!!

Started by
16 comments, last by TLAK1001 17 years, 11 months ago
I am currently writing a program that containin a class called po. The constructor to po contains the following:

cout<<"Number: ";
cin>>m_Number;
cout<<"Date: ";
cin.getline(m_Date,20);
cout<<"Vendor: ";
cin.getline(m_Vendor,100);
cout<<"Description: ";
cin.getline(m_Description,100);
cout<<"Advisor: ";
cin.getline(m_Advisor,100);
cout<<"Account: ";
cin.getline(m_Account,100);
cout<<"Account #: ";
cin>>m_AccountNum;
cout<<"Amount: ";
cin>>m_Amount;
cout<<"Check #: ";
cin>>m_CheckNum;

The first getline (Date: ) is ignored and the program displays Date: Vendor: Later, when po.editFields() is called

cout<<"Change to: ";
switch(field)
{
                    case 1:
                         cin>>currentPo.m_Number;
                         break;
                    case 2:
                         cin.getline(currentPo.m_Date,20);
                         break;
                    case 3:
                         cin.getline(currentPo.m_Vendor,100);
                         break;
                    case 4:
                         cin.getline(currentPo.m_Description,100);
                         break;
                    case 5:
                         cin.getline(currentPo.m_Advisor,100);
                         break;
                    case 6:
                         cin.getline(currentPo.m_Account,100);
                         break;
                    case 7:
                         cin>>currentPo.m_AccountNum;
                         break;
                    case 8:
                         cin>>currentPo.m_Amount;
                         break;
                    case 9:
                         cin>>currentPo.m_CheckNum;
                         break;                      
                    default:
                            cout<<"Invalid field!"<<endl;
}

All getline commands are completely ignored. The program displays the prompt, sets the data member to null, then moves on. Please help me fix this!!
Advertisement
Let me guess, the program doesn't ignore the cin but it does ignore the cin.getlines?
Use an extra break statement after your default label and see if that works. Good luck!
which IDE are you using? i remember in VC 6 getline() was broken and needed a downloadable fix/patch.

Beginner in Game Development?  Read here. And read here.

 

If you're not using VC6 or you've already got the patch, try putting a cin.ignore() every time you switch between using cin>> and cin.getline() - that should fix it. I think. The cin>> call takes the "26" out of the user input "26\n" and then when you try to getline it will immediately see the "\n" in the buffer that was leftover when the cin>> took out the number, so it immediately skips forward because it thinks the user has pressed enter, sortakinda. The cin.ignore() will eat the "\n".
It only takes one mistake to wake up dead the next morning.
Quote:Original post by Alpha_ProgDes
which IDE are you using? i remember in VC 6 getline() was broken and needed a downloadable fix/patch.


IIRC (I read a lot of weird stuff and just memorize it for no reason... not that I try to, it just pops in my head), the only broken part is that it waits for two '\n's instead of one... ONLY IIRC.

As for you, OP, what if there are many '\n's already in the stream? Correct me guys, but wouldn't that please getline enough?
Hey, Im not quie sure is cin.getline(whatever,number) will work, it didnt work for me. But try this, this is how I get lines back from the user. Also, are declairing the variables that you want to be sentences or lines as string variables? If not, you should. Hope this helps.

Should Work Now:
cout<<"Number: ";cin>>m_Number;cout<<"Date: ";getline(cin,m_Date);cout<<"Vendor: ";getline(cin,m_Vendor);cout<<"Description: ";getline(cin,m_Description);cout<<"Advisor: ";getline(cin,m_Advisor);cout<<"Account: ";getline(cin,m_Account);cout<<"Account #: ";cin>>m_AccountNum;cout<<"Amount: ";cin>>m_Amount;cout<<"Check #: ";cin>>m_CheckNum;
You can use cin.ignore(1000, '\n') to clear the buffer. Check this site for some examples and explanation. Also, you might want to consider using std::string instead of char[], which I assume you're using now.

Edit: If you decide to use strings instead, NUCLEAR RABBIT's example shows how to use getline. However, his code will have the same problem that you're having right now. If you use cin.ignore it will work fine though.
Don't do interactive prompting and inputting within the object functions; it's horrible style. The only time objects normally interact directly with streams is for (de)serialization, i.e. loading/saving from/to file.

Anyway, the reason it doesn't work is that although the console input is line-buffered, the input data is still a stream - and when you read a number off of a stream, by {stream} >> {int variable}, it does NOT discard any trailing whitespace (such as newlines). Also, the console output stream does not have anything to do with the console input stream, except for how it's displayed: when you output something, any unprocessed input is still "there". Thus when the next read happens, the data that is seen for the current line is a blank line, because the very next character available is the newline that the user typed following the number.
Perost,
thank you very much. That fixed the problem. PS: Thanks alot for the link. Good stuff, that is!

I tried switching my char arrays to strings and switching the cin.getline s to what nuclear rabbit said, but I can't get that to compile (?)

Zahlman,
I am a complete noob and that made NO sense at all. Can someone please translate that post to stupid?

Thank you all

This topic is closed to new replies.

Advertisement