cin being annoying

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int choice = 3;
	Search search;
	string line;
	string temp;
	ifstream fyle ("actress.txt");
	while(choice != 1 && choice != 2)
	{
		cout << "Press 1 to search by Movie or 2 to search by co-stars : ";
		cin >> choice;
	}
	if(choice == 1)
	{
		cout << "Please enter your movie name: ";
		getline(cin, temp);


Sorry if it looks bad its my first post but thats how my code looks, or at least the beginning of it and it does this weird thing where when i enter the value 1 at the start it goes to ask me for the movie name and then opens up the 'cin' thing for like a split second then my program closes without me having entered anything in.. can anyone help?
Well at the end of all programs you need to do something to make it pause otherwise it's just going to close it because it is finished executing (which doesn't take long for computers). If you put cin.get() that will leave it open until you hit enter.
But shouldn't it wait for my entry either way? And how would I implement the cin.get() function? Would it be (in this case) cin.get(temp)? I'm trying to capture a whole string (including spaces)
cin.get() doesn't need to be implemented. I'd have to see more than that portion of the program though because a lot of details are missing for me to tell what's happening.
Oh I meant how do I use it, and For the most part [from what i can see myself] the rest of the program wouldnt make a difference because it stops before it reaches the rest of that if statement, i dont get a chance to make an input, but if you still need to see it then i will show
Before return 0 or whatever your last line of code is that's going to be executed you put cin.get() before it. Maybe someone else will see what's wrong but for me I think I would need to see all of the code assuming it isn't extremely long.
The problem is that std::cin>> and std::getline don't mix well.
When std::cin>> gets input it reads the entire line including the newline character (the character when you hit enter/return on the keyboard.)

Inside the buffer would contain all the characters for your input as well as the newline character. So if you entered the number 1 for example inside the input buffer would be:
1\n
std::cin>> then strips the all the values upto but not including the first white space character which in this case is the newline.

So std::cin>> places "1" into the variable, and the rest of the buffer remains for the next std::cin call.
When std::getline() is called with std::cin it first checks the input buffer for remaining characters up to and INCLUDING the newline character. Now take that into context with my previous example. In the buffer there is still a newline character! std::getline sees that, and immediately places nothing into the string and ends its operation. To avoid this issue you need to make sure the buffer is empty before running a std::getline() operation. The way to do this is with a cin.ignore() call before std::getline().

Place this before the std::getline() in your program:
cin.ignore(0x7FFFFFFF, '\n'); // This removes everything in the buffer up to the last '\n' character.
That will fix that std::getline() closing out without any input.
Wow there's so much i DON'T know.
Thanks so much, it worked!
Topic archived. No new replies allowed.