Archived

This topic is now archived and is closed to further replies.

getline problems.

This topic is 5146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having a problem with the following code. I''ve tried it multiple different methods to no avail.
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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> ";
}

Share this post


Link to post
Share on other sites
Thanks, that worked. Now I'm facing a new problem


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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
quote:
Original post by DesCr
Even a cin.clear(); didn''t work.
As stated, clear() resets the state of the stream.
quote:

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;

This is you problem. It will just extract the number entered, and leave the new-line. And this...
quote:
getline(cin, sName);
will only read (and consume) the new-line.

I think just adding a cin.ignore(); after cin >> choice; will do.

Share this post


Link to post
Share on other sites
I hate to be such a hassle, but it''s still not working. No matter where I put the cin.ignore(), I have to press enter twice to see the output.

Share this post


Link to post
Share on other sites
FINALLY! Thanks so much for the help. That was even fun, going into scary territory like a standard header file, but it worked. Looks like I should get .NET . Anyone know if it''s worth it, or is this the only fix .NET provides?

Share this post


Link to post
Share on other sites
The Mingw compiler and Borland''s free commandline compiler both doesn''t have the problem you just ran into. Considering that they both can be obtained free of charge and, based from the looks of this thread, that you just need a compiler for educational purposes either of those two should fit you fine. There''s even a free ide for Mingw -- Dev-C++; a lot of people recommend it for a freeware solution so you can take a look at that as well.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Original post by Greatwolf
The Mingw compiler and Borland''s free commandline compiler both doesn''t have the problem you just ran into. Considering that they both can be obtained free of charge and, based from the looks of this thread, that you just need a compiler for educational purposes either of those two should fit you fine. There''s even a free ide for Mingw -- Dev-C++; a lot of people recommend it for a freeware solution so you can take a look at that as well.
Of them, use Dev-C++. It comes with the Mingw/GCC compiler. However, Dev-C++ is not freeware.

Share this post


Link to post
Share on other sites