0

Possible Duplicate:
Program is skipping over Getline() without taking user input

Alright, so I have a program that is running and at the start it prompts you for the data fill the data members. The program does this for 3 different objects.

My problem is that, at run time, after inputting data for the fist object the program proceeds to skip input for the second name and goes straight to the next option. It does the same thing for the third option's name. It also does this when you get the chance to change the data.

"Enter CD Name: Microsoft Word

1-Game

2-Word

3-Compiler

4-Spreadsheet

5-Dbase

6-Presentation

Enter the number that corresponds with the CD's Type: 2

Input CD Cost: 15.23

Enter CD Name: 1-Game <- ((Skips the input part and takes you directly to the menu!))

2-Word

3-Compiler

4-Spreadsheet

5-Dbase

6-Presentation

Enter the number that corresponds with the CD's Type:"

The issue is most likely in my member function, but I'm not sure what the issue is.

Here's my member function code:

void CDX::LoadInfo() //Prompts, validates and sets data members
{
cout << "Enter CD Name: ";
getline(cin, Name);

int choice=0;
do
{   cout << "1-Game\n2-Word\n3-Compiler\n4-Spreadsheet\n5-Dbase\n6-Presentation" << endl;
    cout << "Enter the number that corresponds with the CD's Type: ";
    cin >> choice;
} while ((choice <1)||(choice>6));

switch(choice)
//Code for case-switch statement goes here)

So what am I missing? Is this a buffer issue or am I prematurely ending the code in some way that causes it to skip?

Community
  • 1
  • 1
Rodekki
  • 25
  • 3

3 Answers3

1

The conversion of a number stops when it finds a character that can't be converted. In that case, the character is '\n'

When you use getline to read a line, this character is read and discarded, but when you read a number, it is read (to know if the number continues or not) and if it is not part of the number, it is left in buffer for the next read.

Example: If you write: "29312" and press enter, your buffer will be filled with "29312\n".

If you use cin >> number, to read stdin, it will consume the digits, but will left in the buffer the "\n". In the next time you call getline, it will read an empty line that was left in the buffer.

MasterID
  • 1,465
  • 1
  • 11
  • 15
0

I think it's because the first 'getline(cin, Name)' gobbles up the last newline keypress. When you enter the cost and press ENTER, the call to getline is completed.

You can keep an extra getline after taking the cost so that it consumes the newline. Then, I think, it will run correctly.

Sufian Latif
  • 12,106
  • 3
  • 31
  • 68
0

You have read the "CD Cost", but the newline remains in the input buffer. Skip whitespace before reading the CD name:

ws(cin);
Olaf Dietsche
  • 66,104
  • 6
  • 91
  • 177
  • I understand, I'm confused though where I should be implementing this. Should it be done directly after I've gotten my Cost or right before CD Name is called (IE before I ask for a name to be entered?) – Rodekki Oct 26 '12 at 19:17
  • I guess, if you ask three people, you'd get at least four answers ;-) It depends, wether you have further input after CD cost. At first sight, it would be easiest to skip whitespace right before reading the CD name: `ws(cin); getline(cin, Name);`. The downside to this is, it also skips whitespace at the beginning of the line, i.e. your CD name. But that shouldn't matter in this case. – Olaf Dietsche Oct 26 '12 at 19:27