Maybe dumb question... but really appreciate for help.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm currently using the following statements inside "Do.. While" loop

cout << "Please enter the file name: ";
cin.getline(FileName, 19, '\n')

On the first execution, everything works ok, but when the 'while' condition is met and it goes back to the beginning of the while loop to execute the statement again, it won't stop for user input any more. (That is before cin.getline(FileName, 19, '\n'); ) The code fails because it can't read the input when it's being executed at the second time. Why is it happening
Should I code differently when I ask for user input inside loop

Thanks in advance for your help!!
 
Ryan said:
I'm currently using the following statements inside "Do.. While" loop.

cout << "Please enter the file name: ";
cin.getline(FileName, 19, '\n');

On the first execution, everything works ok, but when the 'while'
condition is met and it goes back to the beginning of the while
loop to execute the statement again, it won't stop for user input
any more. (That is before cin.getline(FileName, 19, '\n'); )
The code fails because it can't read the input when it's being
executed at the second time. Why is it happening? Should I code
differently when I ask for user input inside loop?

It is likely something goes wrong with
your input. Then 'std::cin' enters a
bad state and will refuse to do any
input. You should check for it to be
good ('std::cin.good()') in your loop
as well.
Also, using 'std::string' would most
probably be easier:
std::string filename;
std::getline(std::cin,filename);
You can get an old-fashioned C style
string ('const char*') from it by
calling 'filename.c_str()'.
Thanks in advance for your help!!

HTH,

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