I have to write a program where a person must input answers "a", "b", "c", or "d" and compare them against the correct answers (comparing answers application type deal). One requirement is that I have a condition so that the array ONLY accepts characters and not digits, meaning, when digits are imputed into the array manually they should get and error message. How do I do that? I want to try and "if" condition but I don't know what expression to put in so that it only accepts "a", "b", "c", and "d". Thanks in advanced.
constunsignedint size=21;
char myArr[size];
cout<<"Enter a, b, c, or d as answer:"<<endl;
for (unsignedint i = 0; i < size; ++i)
{
char Answer = cin.get();
if(Answer != 'a' && Answer != 'b' && Answer != 'c' && Answer != 'd')
{
cout << "Error! You may only type 'a', 'b', 'c', or 'd'." << endl;
cout << "Please type it in again:" << endl;
--i; //Try again
}
else
{
myArr[i] = Answer;
cout << endl;
}
}
It worked but now I get an error message for either a letter or integer. I receive both errors twice and when I try to put in a character I get the doubled errors. But i'll try ending the program if characters are not entered to see if that works better.
It means the variable should be unsigned, meaning no sign eg + or -.
Example:
1 2 3
char MyChar; //Range depnds on your compiler, it may be signed or unsigned
signedchar MySignedChar; //Range is -128 to +127
unsignedchar MyUnsignedChar; //Range is 0 to 255
The number of values you can have doesn't change, but the values you can represent does change. Signed types can handle both negative numbers and positive numbers, but at the price that there are half as many positive numbers as unsigned types. Unsigned types do not handle negative numbers; all numbers are positive.