Error Message

This is a discussion on Error Message within the C++ Programming forums, part of the General Programming Boards category; I need the user to input a number, but the number must be above 1 and below 50 and it ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Error Message

    I need the user to input a number, but the number must be above 1 and below 50 and it must be a number, not a symbol or letter. The input will then show up as asterisks, such as if they input 5 they would then see *****. I need a function that outputs an error message if the input is invalid. How do I ensure that the input will be correct and let the user know when it isn't?

    Any help would be appreciated

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Sure, this can be easyly done:

    #include <iostream.h>

    int main()
    {
    int input;
    unsigned short int i;

    cout << "Enter a number 1-50\n";
    cin >> input;
    if(input > 50 || input < 1){
    cout << "Invalid\n";
    return 0;//exit
    }
    for(i=0; i<input; i++){
    cout << "*";
    }
    return 0;
    }

    hope this helps you

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21