Hi,

I am trying to run this code, but after the program runs, it is never asking me to enter something, instead, it just finishes.

#include <iostream>
	#include <string>
	using namespace std;

	int main () {
	      string str;
	      cout << "Please enter full name: ";
	      getline (cin,str);
	      cout << "Thank you, " << str << ".\n";
	}

The output is this:-

Please enter full name: Thank you, .
Press any key to continue . . .

Thanks

Hi,

I am trying to run this code, but after the program runs, it is never asking me to enter something, instead, it just finishes.

#include <iostream>
	#include <string>
	using namespace std;

	int main () {
	      string str;
	      cout << "Please enter full name: ";
	      getline (cin,str);
	      cout << "Thank you, " << str << ".\n";
	}

The output is this:-

Thanks

This program worked fine for me. It asked me for my full name, waited for my answer, then displayed the name I had entered. It's not pausing for input? I can't think of why there would be, but the only thing I can think of is if somehow the input stream isn't empty at the beginning of the program. I don't know how that would happen in the BEGINNING of the program. It's happened to me quite a few times in the MIDDLE of a program, but never at the beginning yet. Anyway, that's the only thing I can think of that might cause what you are describing. I'd rerun it. It didn't happen to me, but if it happens again, check out this post on how to clear the input stream.

http://www.daniweb.com/forums/thread90228.html

#include <iostream>
#include <string>

using namespace std;

int main () 
{
   string str;
   
   cout << "Please enter full name: ";
   getline(cin, str);
   cout << "Thank you, " << str << ".\n";
   
   cin.get();
   return 0;
}  

/* my output
Please enter full name: hello
Thank you, hello.
*/

Seems to work for me. What compile are u using. Try to place endl at the end cout and see if that makes any different. May be its something to do with just flushing!

ssharish

This article has been dead for over six months. Start a new discussion instead.