Program Not Waiting for Cin

Cin not waiting for input despite cin.ignore()

The problem is for the user to exit the loop you need to put the cin in a failed state. That is why your

while(cin >> val){ .... }

is working.

If in a failed state cin is no longer in a position to supply you with input so you need to clear() the failed state. You also need to ignore() the previously non-integer response that triggered the failed state initially.

It would also be of merit to use

if(cin >> n){
cout << "You entered " << n;
}

This will assert that a proper input for n was provided.

Cin isn't waiting for input

You need to employ std::getline and std::string to read the various values. (You can then use functions like atoi to parse them.) Here is your code sample using the std::getline function.

#include <iostream>
#include <string>
#include <cstdlib>

int main(){

std::string strKingdom = "";
bool conquered_me;//see if was conquered, was going to use this on other program and true = game over.
int gold;
int food;
int citizens;
int soldiers;

std::string tString = ""; // Used to read and subsequently parse the string.

std::cout << std::endl <<"Name of kingdom: ";
std::getline(std::cin,strKingdom);

std::cout << std::endl << "were you conquered (true/false): ";
std::getline(std::cin,tString);
conquered_me = (tString == "true");

std::cout << std::endl << "How many gold do you have?:";
std::getline(std::cin,tString);
gold = std::atoi(tString.c_str());

std::cout << std::endl << "How many food do you have?:";
std::getline(std::cin,tString);
food = std::atoi(tString.c_str());

std::cout << std::endl << "How many citizens do you have?:";
std::getline(std::cin,tString);
citizens = std::atoi(tString.c_str());

std::cout << std::endl << "How many soldiers do you have?:";
std::getline(std::cin,tString);
soldiers = std::atoi(tString.c_str());

return 0;
}

Not waiting for std::cin.ignore()

Your problem is that the extraction operation (std::cin >> nrOfRows >> nrOfCols;) will leave delimiting whitespace in the stream, unlike getline(), which will consume the delimiter. This normally isn't a problem because the >> operator will also ignore leading whitespace, but the line break left in the stream will cause std::istream::ignore() to not wait for input.

To fix this, add a call to std::istream::ignore() to discard any whitespace before you output the Press enter to continue: message.

while loop that doesn't wait for cin

Not sure what fil is.

I think your problem is that you need to flush the stream with a cin.ignore() at the bottom of the loop (or else do a cin.getline() to get your input). Otherwise the newline at the end of the input (when you press enter to submit the input) gets saved for the next cin which is your cin >> ing. So the newline gets used up there and doesn't actually ask the user for new input.

Cin without waiting for input?

It's sad that there is no simple portable way to checking asynchronously if a key was hit. But I guess that the standard committee has carefully evaluated the pros and cons.

If you don't want to rely on third party event management libraries, and if multithreading would be overkill, one alternative could be to have your own version of kbhit(), with conditional compiling for the environments you want to support:

  • if your conio.h supports kbhit() just use it.
  • for windows, you can refer to _kbhit()
  • for linux and posix, you can use Matthieu's answer, or look here for Morgan Mattews's code

It's not the most academic answer, but it's pragmatic.

cin.getline not waiting for input and setting parameters on rand

This is a common problem of reading string input after an int/long/float/etc. The issue is the '\n' character remaining in the buffer after an integer has been read:

cin >> maximumSnowfall;

When you call getline the '\n' still remaining in the buffer gets read right away. The standard library interprets it as an empty string, and returns right away.

To avoid this issue call ignore after reading an int:

cin >> maximumSnowfall;
cin.ignore(1, '\n');


Related Topics



Leave a reply



Submit