do-while loop for input with char

Hi, I am writing a 2D array program. The program starts by asking for 2 inputs, first an integer number, the second is a character (M, A or N). I am using a do-while loop to validate both inputs. If the input is invalid, the program will perform a do-while loop and re-prompt for the input.

The first input (for the integer) works fine, but I am stuck at the second input. I appreciate any assistance here, so thanks in advance!

Here's what I have done so far

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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	const int Day = 7;
	const int Rate = 3;
	int DayInput;
	char SessionInput;
	char M, A, N;
	int Array[Rate][Day] = {
		{0.50,0.50,0.50,0.50,0.50,0.75,0.75},
		{0.75,0.75,0.75,0.75,0.75,1.00,1.00},
		{1.00,1.00,1.00,1.00,1.00,1.25,1.25}
	};


		do{
			cout << "Enter Day. (1 for Mon, 2 for Tue, 3 for Wed, 4 for Thurs, 5 for Fri, 6 for Sat, 7 for Sat)." << endl;
			cin >> DayInput;
			cin.clear();
			cin.ignore(INT_MAX, '\n');
		} while(DayInput < 1 || DayInput > 9);

		do{
			cout << "Enter Session. (M for Morning, A for Afternoon, N for Night)" << endl;
			cin >> SessionInput;		
			cin.clear();
			cin.ignore(INT_MAX, '\n');
		} while(SessionInput != M || SessionInput != A ||SessionInput != N);

		cout << "Validation passed." << endl;

		system ("PAUSE");
	return 0;
}
The three char variables M, A and N are uninitialized, but I don't think you want to use these variables at all. If you want the letter M then simply write 'M'.

You also have a logical error. SessionInput can not be equal to all 'M', 'A' and 'N' at the same time so at least two of the expressions SessionInput != 'M', SessionInput != 'A' and SessionInput != 'N' has to be true. You are using the OR-operator so it's enough that at least one of the expressions are true for the whole expression to be true. That is not what you want. What you want is to give true if all expressions are true and in that case you should use the AND-operator instead.
Last edited on
Thanks for the advises!
Topic archived. No new replies allowed.