I want to read a string from a file and then put each word into a linked list node. Would I need to use getline() for this or is there another way????
e.g THIS IS A SAMPLE
node 1 --> THIS
node 1 --> IS
node 1 --> A
node 1 --> SAMPLE
Cheers
Printable View
I want to read a string from a file and then put each word into a linked list node. Would I need to use getline() for this or is there another way????
e.g THIS IS A SAMPLE
node 1 --> THIS
node 1 --> IS
node 1 --> A
node 1 --> SAMPLE
Cheers
Nah, I'd just use regular the regular input operator (>>).
when handeling strings getline() is usually a good idea. You could use it but you dont have too.
No, getline is a good idea when you want to get an entire line. This person only wants to get a single word at a time.Quote:
Originally posted by RoD
when handeling strings getline() is usually a good idea. You could use it but you dont have too.
The only reason I would use getline in this program would be to get all the input at once and then parse at a later time. This just doesn't seem necessary for this problem though.
well cin>> will only read one word because it stops at a space right? Hmm, ya guess that is the better option. I tend to use getline when i work with strings if its anything more than a quick help program.
Just one more thing...when the user inputs a number which I get with cin>>ans will the '\n' remain in the buffer??? Because I ask the user to also enter a string straight after it, for which I was also using cin.getline()??? So will getline be taking in '\n' OR the actual string entered by the user???
If getline is reading in the '\n'...how can I prevent it from doing so??
cheers
>If getline is reading in the '\n'...how can I prevent it from doing so??
it will destroy the '\n'Code:cin.getline(string,[int],'\n');
why not use getline?Quote:
Originally posted by golfinguy4
No, getline is a good idea when you want to get an entire line. This person only wants to get a single word at a time.
The only reason I would use getline in this program would be to get all the input at once and then parse at a later time. This just doesn't seem necessary for this problem though.
that would take in the first token and destroy the space...Code:cin.getline(string,[int],' ');
oh well, i'd just use cin as well...
To me, that implies that you want to get an entire line when you are really just taking a word. It would work, I just don't like the implications it gives.Quote:
Originally posted by major_small
>If getline is reading in the '\n'...how can I prevent it from doing so??
it will destroy the '\n'Code:cin.getline(string,[int],'\n');
why not use getline?
that would take in the first token and destroy the space...Code:cin.getline(string,[int],' ');
oh well, i'd just use cin as well...
Thanx all....
I think that I have found a solution to the lagging '\n'
cin.clear();
cin.ignore (BUFFER, '\n');
seems to be doing what I want, but whether it will cause other complications is yet to be seen..
Cheers
You understand what is happening pefectly: the '\n' is left in the input stream by the >> operator. Since '\n' is the default delimiter for getline(), getline() will read in the '\n' and end, making it appear that the getline() statement was skipped. An easy solution is to use cin.ignore() after using the >> operator when you are subsequently going to use getline(). cin.ignore() has 3 forms:Quote:
Just one more thing...when the user inputs a number which I get with cin>>ans will the '\n' remain in the buffer??? Because I ask the user to also enter a string straight after it, for which I was also using cin.getline()??? So will getline be taking in '\n' OR the actual string entered by the user???
1)No arguments: A single character is taken from the input buffer and discarded:
cin.ignore(); //discard 1 character
2)One argument: The number of characters specified are taken from the input buffer and discarded:
cin.ignore(33); //discard 33 characters
3)Two arguments: discard the number of characters specified, or discard characters up to and including the specified delimiter (whichever comes first):
cin.ignore(26, '\n'); //ignore 26 characters or to a newline, whichever comes first
So, for your code you can do this:
inFile>>word;
cin.ignore();
getline(inFile, text);
And to clear the input buffer completly, without any wait:
cin.ignore(cin.rdbuf()->in_avail());