newbie::getline + cout + cin problem

  • Thread starter Thread starter mel smith
  • Start date Start date
M

mel smith

Hi

I am having a problem with a simple program I have written. To start to
learn how to deal with the string class in C++ I created the the following
function as part of a larger exercise.

string pizzaNS::getName()
{
string itsName;
cout << "Please enter your name" << endl;
getline(cin, itsName)
return itsName;
}

The program that this is part of can be run over and over by selecting Y
after each calculation it performs. The program is fine when this function
is commented out. However when this is part of the program it works first
time, but on subsequent tries the program does not pause to allow the user
to input a name. For example

Enter your name.
XXXXXX
Enter the number of pizzas you require
XXXXX

The Second time you can't enter your name.

Enter your Name.
Enter the number of pizzas you require.
XXXXXX

It doesn't pause for input. Can the string still have the previous value in
it despite going out of scope. Is this why it is skipped the second time
around.

The program is an unmanaged program written in VS2003.

Thanking you in advance.

Nifsmith
 
The problem is, that after the stream contains
non-digits where you try to read in a number,
the stream ist set to a bad state.
You can find this out by checking for
!std::cin.good()

Schobi


mel smith said:
[...]
Nifsmith


Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
Back
Top