controlling user input

I'm writing a game. A very symple game that doesn't use objects or anything like that. (I'm relatively new to prgramming and haven't gotten that far yet.)

Over and over again the program asks the user for input.

Some examples:

cout << "Play (or try, failures) again, Yes(1) or No(0) "; cin >> pg;

cout << "Show attack score, Yes(1) or No(0) :"; cin >> showatk;

Since these questions only have two anwsers, the variables pg and showatk are of bool type.

This all works fine, until the user tries to answer with anything besides a one or a zero. That messes up the program.

I tried to do this:

cout << "Show attack score, Yes(1) or No(0) :"; cin >> showatk;

if (showatk != 0 || showatk != 1){

cout << "Trying to outsmart me, ey " << endl;

showatk = 0;

}

It didn't work. It appears that as soon as bool stores a non-bool value value, chaos breaks loose and the program doesn't bother to check anything.

I've thought of:

if (showatk != 0 || showatk != 1){

cout << "Trying to outsmart me, ey " << endl;

showatk = 0;

return0;

}

This works but it ends the program.




Answer this question

controlling user input

  • DataSort Software

    I haven't been able to get your example to work in my code.

    I modified it into this:

    // After printing the question, this function is called.

    void collect_bool(bool *question){

    string input;

    do{

    getline(cin, input);

    if (isdigit(input[0]))

    *question = atoi(reinterpret_cast<char *>(input[0]));

    else

    cout << "Invalid input, please press 0 or 1 and then press ENTER ";

    }while (*question != 0 && *question != 1);

    }



  • bbanks

    if (showatk != 0 || showatk != 1){

    This will always be true. If showatk == 1, it != 0, does it

    You've hit the world of user input validation. You can't guarentee what the user inputs will be valid, so you need to check, and keep asking until you get a valid reply. For example, ask for input within a while loop that exists when the input is valid.

    do

    {

    cout << "Tell me a number between 0 and 1";

    cin >> myVal;

    }while ( myVal < 0 || myVal > 1)



  • JonPen

    Does that work I wouldn't expect it to, you're turning a char into a char * with a cast, instead of using &.



  • R-man

    They didn't display properly. Here are the examples:

    cout << "Show attack score, Yes(1) or No(0) :";

    cin >> showatk;

    cout << "Play (or try, failures) again, Yes(1) or No(0) ";

    cin >> pg;



  • Apek

    Thanks.

    Before I come back here, I definitely have to learn more of this language.



  • flo ben

    Yes!!!!!!!

    I made it work with with this, a switch loop:

    // After asking a question, this function is called.

    void collect_bool(bool *question){

    string input;

    bool success;

    if(success)

    break;

    while(1){

    getline(cin, input);

    switch(input[0]){

    case '1': {

    *question = true;

    success = true;

    break;

    }

    case '0':{

    *question = false;

    success = true;

    break;

    }

    default:{

    cout << "Invalid Input, please press 1 or 0 followed by ENTER. ";

    success = false;

    break;

    }

    }

    }

    }



  • Mary Potapova

    No it doesn't work. To tell you the truth, I have no idea what I'm doing here. I want the program to test the first digit of the user's input. If it is a one, the program should set the bool to true, if it is 0, the bool is set to false.

    I don't think I'm using the string class the right way.

    If you ave an answer, could you fix my code rather than giving an example. (Sorry if that sounds rude, I reallly do mean it in a polite way.) The way I see it, C++ allows different programers to write in their own styles, and maybe I'm just a novice, but I can't understand some of the syntax you use. (Such as:


    using std::cin;

    using std::cout;

    using std::string;

    rather than

    using namespace std;)

    I really do apprieciate your help.



  • Raj Chidipudi

    Try accepting a char instead of an int. Then use IsDigit ( from memory ) to find out if it's a number, and if it is, use atoi to turn it into an int.



  • Googhum

    atoi seems to only take a char *. try something like this

    #include <string>

    #include <iostream>

    using std::cin;

    using std::cout;

    using std::string;

    int main(int argc, char* argv[])

    {

    string input;

    int parsedInput = -1;

    do

    {

    cout << "Enter a number between 0 and 1";

    cin >> input;

    if (::isdigit(input[0]))

    {

    parsedInput = ::atoi(input.c_str());

    }

    }while (parsedInput < 0 || parsedInput > 1);

    return 0;

    }



  • Gisela

    Can you show me how to do that with an example

    I can't seem to figure out how to use atoi.



  • bpatrick

    C++ is definately complex. However, while I would generally recommend buying a book ( The C++ Language by Bjarne Stroustrup is one you must own if you're doing C++ ), I would say you seem to be working at an appriopriate level for your skill set, and you should keep on asking questions here as well as doing some reading to help you learn.



  • Grouchypb

    That is an effective loop, thanks. It works if the user inputs, say 5.

    However, it doesn't solve my problem.

    The line " cout << "Show attack score, Yes(1) or No(0) :"; "

    looks like this inside tha application :

    Show attack score, Yes(1) or No(0) :

    Now what if the user inputs the letter a and presses enter,

    Show attack score, Yes(1) or No(0) : a

    The program closes abruptly.



  • controlling user input