Testing for a specific letter in an array of characters

I prompt them for a code (will be in the format 'a1111') and I need to test the first letter. If the first letter is a 'C', a 'D', or an 'H', the program will output the term "qualified expense," alongside the name of the item. (this is part of a slightly larger program).

If the code does start with one of those three letters, then the program only outputs the name of the item.

I just don't know how to test for what letter the code starts with. I've marked the places I need help with /*HELP*/.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "Please enter the item name: ";
cin >> name;
cout << "Please enter the code for the item: ";
cin >> code;
	if /*HELP*/ { //Test for letter "C"
		cout << item << "Qualified expense";
	}
		else if /*HELP*/ { //Test for letter "D"
			cout << item << "Qualified expense";
		}
		else if /*HELP*/ { //Test for letter "H"
			cout << item << "Qualified expense";
		}
		else {
			cout << item;
		}
Last edited on
You haven;t shown the definition of code, but regardless of whether it is a char array or a std::string, the following should do what you want.
1
2
3
4
5
if (code[0] == 'C')

if (code[0] == 'D')

if (code[0] == 'H')


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
That worked perfectly, thanks!
And sorry, I didn't know how to do that. But i fixed it and will use that feature from here on out.

Thanks again!
Topic archived. No new replies allowed.