# WTF? Need help with getline!!!

## Recommended Posts

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<<"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:
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!!

##### Share on other sites
Let me guess, the program doesn't ignore the cin but it does ignore the cin.getlines?

##### Share on other sites
Use an extra break statement after your default label and see if that works. Good luck!

##### Share on other sites
which IDE are you using? i remember in VC 6 getline() was broken and needed a downloadable fix/patch.

##### Share on other sites
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".

##### Share on other sites
Quote:
 Original post by Alpha_ProgDeswhich 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?

##### Share on other sites
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;

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
Not to derail the post, but why do so many newbies post WTF in thier titles? lmao

- Anon

##### Share on other sites
Because it was confusing the hell out of me since I didn't know how it was supposed to work. This is a common problem with us noobs :)

##### Share on other sites
To use strings you have to make sure to do #include <string>. Here's a tutorial. Also, to translate what Zahlman said to "stupid" [smile]:

Say that you use cin and the user inputs 15, and presses return. The input buffer will now contain 15 and the return char. cin wants an integer, and therefore it takes 15 away, and puts it in your variabel. Then next time you use cin it will see that the buffer isn't empty, it still contains the return char. "Perfect, a string!" says cin and puts it in your string without asking the user for input. Now the buffer is empty, and the next cin will wait for the user to input something.

Consider this:
int n1, n2;cin >> n1;cin >> n2;

If you input "16 17", then cin will read 16 into n1. Then it wants another number, and discards characters until it finds a number, 17, that it puts into n2. Thus cin fills two variabels with one user input. You could also have written cin >> n1 >> n2; if you wanted.

When you use cin.ignore(1000, '\n') it will discard the next 1000 chars or until it encounters a new line, which will clear the buffer assuming that the user didn't input more than 1000 chars.

Hope this makes it easier to understand.

Oh, almost forgot. As Zalhman said, newer take input from the user from inside the class. Write a function outside your class or something, and let your constructor take the input as arguments, or write set-functions, like void SetDate(int date).

##### Share on other sites
Thanks a million perost!

I tried using getline with strings in this example:
#include<string>#include<iostream>using namespace std;int main(){    string theString;    cin.getline(cin,theString,"\n");    cout<<"You entered: \n"<<theString<<endl;    while(true);    return 0;}

But it gives me this error:
7 | E:\Programming\Tests\Testbed.cpp | no matching function for call to std::basic_istream<char, std::char_traits<char> >::getline(std::istream&, std::string&, const char[2])'

Same thing happens with cin.getline(cin,theString,'\n');

Ps: the program also generates a table of objects. It must cut down member strings beyond a certain length to their maximum when it displays them. That was easy with character arrays, but how should I do it with a string?

##### Share on other sites
you probably want to use std::getline

example:
std::string TestString;std::getline(std::cin, TestString, '\n');

#include<string>#include<iostream>using namespace std;int main(){	string theString;    	getline(cin, theString, '\n');    	cout<<"You entered: \n"<<theString<<endl;    	//while(true);	cin.ignore(1);    	return 0;}`

##### Share on other sites
Big thanks!

Can someone please tell me how to display only the first x characters of a string?

##### Share on other sites
Quote:
 Original post by TLAK1001Big thanks!Can someone please tell me how to display only the first x characters of a string?

cout << myString.substr(0, x) << endl;

##### Share on other sites
Awesome! Thank you very much everyone, that's all I needed.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628401
• Total Posts
2982462

• 9
• 10
• 9
• 19
• 24