using std::cin.ignore

I have a bit of code that works but give me a warning and doesn't "feel" right.

Here is the line:

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

I get a warning for using '>1'. How wrong is this?

I want the user to input only one character. If they put in more, I want to just read in the first entry and throw away the rest.

Is there a better way to do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
char inputKey (){
    
    char moveDir[1];//Limits number of character user can input
    bool validInput = false;
    
    //Ask for valid input; WASD Loop until received
    do  {
        std::cout << "Your move; WASD:";
        std::cin >> moveDir[0];
        std::cin.ignore('>1','\n');
        moveDir[0] = toupper(moveDir[0]);
        switch (moveDir[0]) {
            case 'W':
                validInput = true;
                break;
            case 'A':
                validInput = true;
                break;
            case 'S': 
                validInput = true;
                break;
            case 'D':
                validInput = true;
                break;
            default:
                std::cout << "WASD only!\n";
                break;
        } 
        
    } while (validInput == false);


Thanks!
How wrong is this?


Roughly as wrong as you could possibly be.

http://cplusplus.com/reference/iostream/istream/ignore/

read what it actually does.
I started with this example but was put-off in reading that:

int get();
Extracts a character from the stream and returns its value (casted to an integer).

Extracts a character (singular character, cool)
from the stream and returns its value (also cool)
casting to an integer (wait what?)

Copying from the example works, but I'm confused by the cast to integer bit.

Here is the fixed code. I've learned how but not why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
char inputKey (){
    
    char moveDir;
    bool validInput = false;
    
    //Ask for valid input; WASD Loop until received
    do  {
        std::cout << "Your move; WASD:";
        moveDir = std::cin.get();
        std::cin.ignore(256,'\n');
        moveDir = toupper(moveDir);
        switch (moveDir) {
            case 'W':
                validInput = true;
                break;
            case 'A':
                validInput = true;
                break;
            case 'S': 
                validInput = true;
                break;
            case 'D':
                validInput = true;
                break;
            default:
                std::cout << "WASD only!\n";
                break;
        } 
        
    } while (validInput == false);
    
    //Print selection (for testing)
    //std::cout << moveDir << "\n";
    return moveDir;
};


casting means basically to force a conversion from datatype A to B. While chars aren't ints, they are integer values. Usually 8 bit (-127-128). Since a char is guaranteed by the standard to be of smaller or equal size than an int, you can always safely cast a char to an int without dataloss. The other way round data loss may occur if the int is larger than what fits into a char (obviously).
Topic archived. No new replies allowed.