Is it possible to use getline(cin, str) without having to press enter twice on input?

So I use this code to accept user input that is more than one word:

string str;
getline(cin, str);

The problem is, at runtime, when the user enters the input, he/she has to press the enter twice. I want the users to press enter once only?

is there any other way to do it??

Thanks.
Last edited on
No, he/she does not. Compile this:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main(){
   std::string str;
   std::getline( std::cin, str );
   std::cout << str;
   std::cin.ignore();
   return 0;
}
Thanks. I compiled this. The user still has to press the enter key twice before the output appears.
I want users to be able to press the enter only once.
like when i input

word1 word2

and then press enter, i don't have to press enter again.

Is that possible?
This is a sample of my code so that I can explain my question further:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

void main()
{
	std::string str1;
	std::string str2;
	std::getline( std::cin, str1 );
	fflush(stdin);
	std::getline( std::cin, str2 );
	fflush(stdin);
	std::cout << str1<<endl;
	std::cout << str2;
}


OK. So the problem is. When I run this, by reading the code, I know that I have to enter my input, so what I do is I enter my input and press enter once.

Then, I immediately enter my second input, then I press enter once again.
Then nothing happens, so I press enter again. NOthing happens again. So I press enter again. The output finally appears. But I can see that only the firs word is being displayed. So when my input is something like this

(spaces means endlines, I mean spaces after word 1 and word 2 means I pressed enter)

word 1
word 2


word1


However if I input something like this

word 1

word 2

word1
word2


Is this clear? I have to press enter twice. I only want to have to press the enter once and the next code will be executed. Is that possible?
I Solved it actually. Its because of the bug in Microsoft Visual C++ 6.0 compiler. But you can fix it.

Refer to this.

http://support.microsoft.com/default.aspx?scid=KB;EN-US;q240015&ID=KB;EN-US;q240015
Topic archived. No new replies allowed.