Help with or statement in while loop


                  //Validating calculation type
		while(userChar != false)
		{
			if(type == 'a')
			{userChar = false;}

			else if (type == 's')
			{userChar = false;}

			else
			{
			userChar = true;
			cout << "You have entered an invalid character, a or s in lowercase only." << endl;
			cin >> type;
			}
		} 


I had incorporated this code into my first C++ tutorial and it works well, but I believe there is a shorter way to represent these line of codes. I am thinking of using 'or' statement, but I could not get the syntax right. I tried "while(type != 'a'||'s')" but to no avail. Any help would be appreciated, thank you!
1
2
3
4
5
while(type != 'a' && type != 'n')
{
  cout << "You have entered an invalid character, a or s in lowercase only." << endl;
  type = cin.get();
}
if(type!='a' || type!='s')

also, you can shorten your code by putting while (userChar) instead of while(userChar != false)
Last edited on
here's the whole code and ive also solved the problem that your letters can only be lowercase.
1
2
3
4
5
6
7
8
9
10
11
12
while(userChar)
		{
			if (type != 'a' || type != 's' || type!='A' || type!='S')
			{userChar = false;}

			else
			{
			userChar = true;
			cout << "You have entered an invalid character, a or s  only." << endl;
			cin >> type;
			}
		} 
Last edited on
Topic archived. No new replies allowed.