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.
#include <iostream>
#include <iomanip>
#include <string> //firts you need to include string wich is a symbol type like char
usingnamespace 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;
}
elseif(comand=="Talk")
cout << "Hello world, this is a nice day";
//hope i could help
return 0;
}
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!
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 ;)
//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.
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.
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 :
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).