Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am compiling this code with G++ 4.7.2, and I am having a hard time continually reading input from the console. The code looks like this.

C++
//Ensure the CIN buffer is clear & empty from previous reads
std::cin.clear();
std::cin.ignore(INT_MAX, '\r');

std::string uInpt;
std::getline(std::cin, uInpt);

while (uInpt.compare("quit") != 0)
{
  std::cin.clear();
  std::cin.ignore(INT_MAX, '\r');
  std::cout << "You entered: " << uInpt << std::endl;
  std::getline(std::cin, uInpt);
}


The problem I am having is:
1. It seems like the CIN buffer is not cleared at all, the reason being is because, I can only ever input 1 character before the loop starts, Then it seems like there is a carriage return already in place.
2. I never seem to get prompted for the read while in my loop.

Thoughts?
Posted
Comments
enhzflep 1-Aug-14 12:56pm    
Comment-out the 4 lines that include cin.clear and cin.ignore
Naturally, that's not a viable solution. A far better one is to swap the instnaces of \r to \n

Even better again, is to comment-out both calls to cin.ignore and call it a day. :)

1 solution

Yea....That doesn't really work. The problem came from having this code in a fork'd process and letting the parent process go away.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Aug-14 1:27am    
Sorry, this is not an answer; such posts are considered as abuse. Please use comments, as in this case, and/or "Improve question".
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900