If I have a menu how do I display an error message for character inputs?

I want it so that the user can only input 1-5 and choose one of the cases, but if I input a letter the program just loops over forever and pretty much is broken.

Anybody know how I can 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
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include <string>  //firts you need to include string wich is a symbol type like char

using namespace std;

string Menu(){
    
    //this is how u can declare a string named comand
    string comand;
    //In your menu u say the user he can input 2,5, or 20 comand
    cout << "Input one of two comands\nExit if u want to exit the program\nTalk if u want the program to say something" << endl;
    cin  >> comand;
    
    //end here is how u make the user enter just those you have given him the right to enter
    while(comand!="Exit" && comand!="Talk"){//if there is bene entered something that you have not said it can be
        cout << "You have not entered a good comand, enter eather Exit or Talk"; //the program gives this error message
        cin.clear();               //after that you clear the buffer
        cin.ignore(1000,'\n');
        cin >> comand;             //and take a new command
    }

    return comand;
}


int main(){

    string comand(Menu());
    
    //depending on what the command is, the program will say by by and exit or "start talking"
    if(comand=="Exit"){
        cout << "Bye Bye";
        return 0;
    }

    else if(comand=="Talk")
    cout << "Hello world, this is a nice day";

    //hope i could help
    return 0;
}
Wrong answer.

I would recommend using ctype.h. Get one character from the stream, and check to see what it is. If it's not a digit, output an error. Else, use atoi() to store the character's digit value in an integer, and then do stuff with it!

-Albatross
the answer is not wrong you just have another way of solving the problem.

but if u really want to input just numbers, you could take a char as input data, then make an integer and a code like
(if i is the integer and c is the char)
i=c-48;

48 is the ASCII value of the digit 0, so by doing this you really get some number that could be betwen 1 and 5 (if the input data is correct)
if not, you need maybe an if(.....) to chek that and so on ;)
1
2
3
4
5
6
7
//justAbeginner's method
//c is a char in this case. i is an integer.
i = (int)c - 48
if (i < 0 || i > 5) {
cout <<"ERROR!";
}
//Insert code that uses i here. 

1
2
3
4
5
6
7
//Albatross's method
//c is a char * in this case. i is an integer.
i = atoi(c);
if (i == 0) {
cout <<"ERROR!";
}
//Insert code that uses i here. 


Your choice. They each accomplish approximately the same thing, however justAbeginner's method requires modification if you want to expand it to a range larger than 1-5 but still one digit, and even further modification if you want an even larger range. However, mine only requires modification if you are going to use the number 0, however depending on your wits there may be more of it required.

-Albatross
Last edited on
closed account (jwC5fSEw)
Your method also requires including another header file for the use of one function that you yourself just demonstrated can be done in an equivalent amount of code.
Nope. stdlib is included with iostream. Sorry. EDIT: :)

-Albatross
Last edited on
and my code can eaven get better.
you do the check like this

if(c-48<0 || c-49>5)
cout << "ERROR";

than u use your code for a new reading, when u are shure u have the right nuber
you just di i=c-48, u do not need the int(c) at all.

What i would do is put te min and max values as constants, so you do not need to change big parts of the code when u need new limits, u just change the min and max . it would look like this :
1
2
3
4
5
6
#include <iostream>
...
const int min(0),max(0)

using namespace std;
....

closed account (jwC5fSEw)
Nope. stdlib is included with iostream. Sorry. EDIT: :)


I don't believe this is true. Including iostream, the compiler reports atoi() as undefined; also it doesn't seem to be included in iostream or istream or ostream (though I didn't look any further back than that).

EDIT: This is with GCC 4.4.0, by the way.
Last edited on
1
2
3
4
5
int x;
if(!(std::cin>>x)) {
    std::cout<<"error";
}
//... 
Hmm... I'm using GCC 4.2.1, using only standard library for C++, and this returned no errors. EDIT: I need to upgrade...

1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
	char* i = "c";
	atoi(i);
}


The moment I put the include in a comment, it gave an error.

-Albatross
Last edited on
iostream is not required to include atoi(). GCC probably includes it for converting an int to a string for displaying purposes.
closed account (jwC5fSEw)
Well go check inside iostream. See if it's included.

firedraco has the answer. I guess it used to include atoi() and no longer does.
Last edited on
Now that really sucks, but I guess it is proper C++. If they didn't put it back in when GCC 4.5.0 came out...

-Albatross
Topic archived. No new replies allowed.