void CreateCharacter()
{
string sName;
cout << "\nCharacter Creator" << endl;
cout << "\nEnter name: ";
getline(cin, sName);
cout << "Name: " << sName << endl;
}
The only output I''m getting is "Name: " and no string. Funny thing is, if I encapsulate getline(cin,sName); in a while loop, the 2nd time through the string shows up.
Any help is appreciated. Thanks.
getline problems.
I''m having a problem with the following code. I''ve tried it multiple different methods to no avail.
Please guys, this has to be simple. I''ve posted on TONS of messageboards and all of them are still 0 replies after HOURS. Please, help the little people.
could your cin stream have a ''\n'' char still in the buffer? It could be from your last use in cin so make sure it''s cleared out first.
I''ve tried your code using just a simple main() test and it works fine on my end.
--{You fight like a dairy farmer!}
I''ve tried your code using just a simple main() test and it works fine on my end.
--{You fight like a dairy farmer!}
Perhaps a newline feed is stuck in the input buffer from previous input? Input with operator<< and getline() treat these differently; I believe operator<< (which ignores whitespace) reads up to the newline and leaves it, whereas getline() reads up to the next newline and removes it.
Even a cin.clear(); didn''t work. Here''s the entire file:
#include <iostream>#include <fstream>#include <string>using namespace std;void prompt();void DisplayMenu();void DetAction(int choice);void CreateCharacter();//void DisplayCharacter();//void EditCharacter();int main (void){ cout << "Welcome to the character creator: a program that lets you create a character \nand save it to a file, then reload it. Woo Hoo." << endl; DisplayMenu(); return 0;}//Function that chooses an option from the main menu.void DisplayMenu(){ int choice; cout << "\n\n1. Create a new character." << endl; cout << "2. Display a saved character." << endl; cout << "3. Edit a saved character." << endl; prompt(); cin >> choice; DetAction(choice);}void DetAction(int choice){ switch(choice) { case 1: CreateCharacter(); break; //case 2: DisplayCharacter(); break; //case 3: EditCharacter(); break; default: { cout << "\nThat was not a valid option. Please try again." << endl; DisplayMenu(); } }}void CreateCharacter(){ string sName; cout << "\nCharacter Creator" << endl; cout << "\nEnter name: "; getline(cin, sName); cout << "Name: " << sName << endl;}void prompt(){ cout << "\n> ";}
If I recall correctly, cin.clear() clears the stream''s error state but has nothing to do with the buffer. You want cin.ignore().
Thanks, that worked. Now I'm facing a new problem
Now I enter a name, and press enter. Then I have to press enter AGAIN for the name to show up.
[edited by - DesCr on November 9, 2003 7:46:03 PM]
void CreateCharacter(){ string sName; cout << "\nCharacter Creator" << endl; cout << "\nEnter name: "; cin.ignore(); getline(cin, sName); cout << "Name: " << sName << endl;}
Now I enter a name, and press enter. Then I have to press enter AGAIN for the name to show up.
[edited by - DesCr on November 9, 2003 7:46:03 PM]
Thats a common bug in the string files that come with MSVC++ 6.0. It can be fixed though, try the online MSDN or google.
Wizza Wuzza?
[edited by - Chris Hare on November 9, 2003 7:51:30 PM]
Wizza Wuzza?
[edited by - Chris Hare on November 9, 2003 7:51:30 PM]
quote:Original post by DesCrAs stated, clear() resets the state of the stream.
Even a cin.clear(); didn''t work.
quote:This is you problem. It will just extract the number entered, and leave the new-line. And this...void DisplayMenu(){ int choice; cout << "\n\n1. Create a new character." << endl; cout << "2. Display a saved character." << endl; cout << "3. Edit a saved character." << endl; prompt(); cin >> choice;
quote: getline(cin, sName);will only read (and consume) the new-line.
I think just adding a cin.ignore(); after cin >> choice; will do.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement