Kevin Kloet February 2016

how to get a specific character from input?

i done a tutorial about getchar: getChar - c++ tutorial

this all worked till i wanted to make some modifications.

char c;
puts("enter . to exit");
do{
    c=getchar();

} while (c != '.'); {

    if(c == 's'){
        upgradeOne();
        cout << "upgrade1 is done" << endl;
}

    if (c == 'a'){
        upgradeTwo();
        cout << "upgrade2 is done" << endl;
    }
}

while (total < 999){
    total += i;
    cout << total << endl;
}

i want to use specific methods when the user input equels an specific character. if the user types a the method upgradetwo() must be activated. how am i supposed to do this?

Answers


Christophe February 2016

The repetitive processing of your input takes place in the do...while loop. The loop is left only when '.' is pressed.

If you want to react on specific chars, you have to move your if(c==...) statements into this loop (i.e. Between the getchar() and the while).


kiran February 2016

If you want to run the loop until the user enters period(.), then you can use some bool flag which will be set to true initially. Then write a while which will run until the flag is false. In the while read the character and if it is a period then set the flag to false.

or you can run the while loop infinitely and break the loop if the char is a period else continue.

Post Status

Asked in February 2016
Viewed 1,253 times
Voted 7
Answered 2 times

Search




Leave an answer