char inputs

Hello, I want to ask how to properly get char inputs from the user. Im having a hard tim with this part of my program.

What I wanted to do was the program will ask the user to press 'y' or 'Y' if he/she wants to repeat the process again. But in my case it still loops even though I didn't press 'y' or 'Y'

here's the function I'm using
1
2
3
4
5
6
7
  char choice()  //user decides if he/she wants to continue
{
	char c;
	cout << "Press y or Y to continue: ";
	cin>>c;                            
	return c;
}

And the supposed loop
1
2
3
4
5
6
do
{
//I don't think its necessary to writ my whole program since it will just be clutter.
loop=choice();
}
while (loop='y' || 'Y');
Last edited on
you misuse syntax. it should be
 
while (loop == 'Y' || loop == 'y')


you instead wrote incorrect condition which is always true
Last edited on
You can maake use of the tolower or toupper functions - (read about them in the reference section top left of this page) to make your logic easier - you don't need to test everything twice for upper or lower case.

I have a personal hatred dislike for do loops. I am not the only one who isn't fond of them - Kerninghan & Ritchie (the inventors of C) and Stroustrup (Inventor of C++) aren't fans either, because they can be error prone.

All 3 types of loops can be rewritten as 1 of the others, so I prefer to use a while loop, even if it means using another variable.

Hope this helps.
Thank you, it worked. It's like magic when you suddenly know something you didn't
Topic archived. No new replies allowed.