MatthewS
MatthewS

Reputation: 485

Limiting user input

I have written the following code and to a large extent it does what I want it to do. The issue is that when the user can enter something like "111" and the output is "You entered the number 1." I want to limit the user's input to 1 character. Any suggestions? I'm sure the solution is quite simple, but I can't figure it out. Also, the code must remain in the form of a switch statement. Thank you!

#include <iostream>
using namespace std;

int main()
{
    char i;

    cout << "Please enter a number between 1 and 9." << endl;
    cout << "Enter a number: ";
    cin >> i;

    switch (i)
        {
        case '1':
            cout << "You entered the number one." << endl;
            break;

        case '2':
            cout << "You entered the number two." << endl;
            break;

        case '3':
            cout << "You entered the number three." << endl;
            break;

        case '4':
            cout << "You entered the number four." << endl;
            break;

        case '5':
            cout << "You entered the number five." << endl;
            break;

        case '6':
            cout << "You entered the number six." << endl;
            break;

        case '7':
            cout << "You entered the number seven." << endl;
            break;

        case '8':
            cout << "You entered the number eight." << endl;
            break;

        case '9':
            cout << "You entered the number nine." << endl;
            break;

        default:
            cout << "You did not enter a valid number." << endl;
            break;
        }

    system("pause");
    return 0;
}

Upvotes: 0

Views: 4967

Answers (3)

Bismarck
Bismarck

Reputation: 96

You could use getchar(char) from the c standard io library.

    #include <stdio.h>
    ...
    char i;
    int j;

    cout << "Please enter a number between 1 and 9." << endl;
    cout << "Enter a number: ";
    getchar(j);
    i=(char)j;
    switch(i){
    ...

Upvotes: 1

Simon
Simon

Reputation: 2469

You can use getch() to get a single char from the screen, you can check if it's a number and otherwise ask again for a valid input.

Upvotes: 0

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17434

There is one way that would be easy: Just switch the char c to int n and replace the case '1' with case 1 etc. Try this until the user enters a valid number and then enter "a" (i.e. something that is not a number). As often, the easy way is also the wrong way. ;)

Now, what you could do instead is to use this bit of code:

std::string line;
while (getline(std::cin, line))
{
    if (line == "1") {
        std::cout << "You entered the number one." << std::endl;
    } else if (line == "2") {
        // ....
    } else {
        std::cout << "You didn't enter a valid number" << std::endl;
    }         
}

The difference here is that since the input is line-base without further interpretation, the streamstate isn't modified when the input can't be interpreted as a number. This is typically more robust when interacting with the user. If later you want to have the number numerically, check out stringstreams or lexical_cast for conversion.

Upvotes: 0

Related Questions