Array that accepts only characters

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.
Like this?
1
2
3
4
5
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;
}
Last edited on
You could use isalpha from the cctype library. Just make sure to #include <cctype>.
http://cplusplus.com/reference/clibrary/cctype/isalpha/

Edit: Wow I totally read that question wrong... Go with L B's answer.
Last edited on
LB, how would I get Answer to =cin.get() if Im running it through a loop? Here is part of my code:

1
2
3
4
5
const int size=21;
char myArr[size];
cout<<"Enter a, b, c, or d as answer:"<<endl;
for (int i=0; i<=(size-2); i++)
      cin>>myArr[i];


So how would I do it so it would read what the person put in? Would I have to put the if condition in the loop or before?
What are you trying to store in myArr ?
Im trying to store 20 characters that are either a, b, c, or d.
I know, but are they the answers that the user gives, or the answer key?
The answers the user gives. The answer key is already initialized in the previous part of the program.
OK, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const unsigned int size=21;
char myArr[size];
cout<<"Enter a, b, c, or d as answer:"<<endl;
for (unsigned int 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;
	}
}
Last edited on
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.
Ok, I tweaked it a bit and I finally got it to work! Thank you so much!

One quick question though. What does the "unsigned" command do?
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
signed char MySignedChar; //Range is -128 to +127
unsigned char 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.
Last edited on
Ooh ok, got it. Well, again, thanks!
Topic archived. No new replies allowed.