Skip to main content
gamedev.net
🔒 Locked

getline not being recognized

Started by bioxx Aug 14, 2006 at 11:49 AM 9 replies 800+ views
Original Post
bioxx
bioxx
ok here is the problem that I am having. I am using VC++ 2005 express jsut so you know. i have having the user input teh name of thier character for my simple rpg but when I use "getline(cin, mName);" it simply skips over it acting as if the line isn't even there. I would just settle for a cin but I need to allow a first and last name if the user wishes to have both. from there I am saving the name along with alot of other stats into a savegame file for later use. **************************************************************************** cout << "CHARACTER CLASS GENERATION" << endl; cout << "==========================" << endl; // Input character's name. cout << "Enter your character's name: "; string mName; getline(cin, mName);///<-----skipping here cout << mName << endl; // Character selection. cout << "Please select a character class number..." << endl; cout << "1)Fighter 2)Wizard 3)Cleric 4)Thief : "; int characterNum = 1; cin >> characterNum; cout << endl; **************************************************************************** it skips from asking you to enter a name, down to please select a class number. Any idea why it would be doing this? there is obviously alot more code but I don't want to paste it all.
ToohrVyk
ToohrVyk
Have you tried stepping through your code and see how the string variable is modified?
Zahlman
Zahlman
Did you previously read from cin with the operator>>? Doing so will NOT skip the newline that follows the user's input, so that will be picked up right away by the getline() call.
ToohrVyk
ToohrVyk
Quote:
Original post by Anonymous Poster
first off thanks for the replies. yes i have tried adding a cout in there to show me the name after i call the getline and nothing changes.


Good, but have you stepped through the code to see how the string variable is modified? And, also, what was the state of the various implicated objects when the getline function is called?

bioxx
bioxx
sorry for the above post, my login timed out and it cut off the last part of my message for some reason. anyways it works in the new project but not in there, no matter what the variable does, it's like that.
bioxx
bioxx
Quote:
Original post by ToohrVyk
Quote:
Original post by Anonymous Poster
first off thanks for the replies. yes i have tried adding a cout in there to show me the name after i call the getline and nothing changes.


Good, but have you stepped through the code to see how the string variable is modified? And, also, what was the state of the various implicated objects when the getline function is called?


depends what you mean by stepped through. sorry i'm still learning. if you mean debugging then no i haven't got the knack of running the debugger yet.

ToohrVyk
ToohrVyk
Place a breakpoint (F9 key) on the line right above the getline call. Move one instruction at a time with F10, dive into a function call using F11. Use the stack variable listing to see the value of the variables as they are modified.
Zahlman
Zahlman
Quote:
Original post by Anonymous Poster
first off thanks for the replies. yes i have tried adding a cout in there to show me the name after i call the getline and nothing changes. As far as calling cin goes, do you mean w/o the getline code? if so, yes that works beautifully except that it of course is restricted to only 1 word. I have tried testing the getline in a new project, for instance:

int main()
{
string s;
getline(cin, s);
cout <


That's not what I said. What I asked was, do you *currently* do something like this:

int main() {  int x;  string y;  cin >> x;  getline(cin, y);}


If you do this, 'y' will be empty, because 'cin >> x' does NOT remove the user's newline input from the stream. The user input goes like

42<Enter key> { Now the program can see the data on cin. As a result, it reads out the   characters '4' and '2', and calculates the value 42 and stores it in x. But  the Enter key also puts a newline into cin. }{ Before the user - who is much much slower than the computer :) - can input  any more, the code gets to the std::getline call. std::getline looks at cin's  buffer and sees a '\n' at the front, and says "ok, that's my line - zero  characters followed by a '\n'." }bioxx<space>teh<space>noob<Enter>{ Oops, the program has already moved on. }
bioxx
bioxx
I'm sorry, I misunderstood what you were asking. and yes my program was working that way. i had it ask the user if they wanted to load a game, if not then run the character creation function. I have since fixed the problem using cin.ignore(1, '\n'). Thank you very much for your help.

PS I like your example program..... Bioxx most definately is a noob. lol

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.