Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple conditions in a do...while loop

Tags:

c++

So I'm making a program that asks a user if they want to do something. The answer is as simple as Y/N. I would like the program to be able to accept both capital and lowercase "Y". Problem is, when I type while (answer == 'Y', answer == 'y') only the lowercase "Y" is accepted. If I type while (answer == 'y', answer == 'Y')

What am I doing wrong?

(More info: "answer" is the name of my "char" variable, and I'm using the "iostream", "cstdlib", and "string" libraries)

like image 212
h3half Avatar asked Dec 22 '22 08:12

h3half


2 Answers

You need to use the 'logical or' operator ||

So your code would become while (answer =='Y' || answer == 'y')

like image 145
MahlerFive Avatar answered Jan 09 '23 09:01

MahlerFive


You should be using the logical operator for or ("||"):

while( answer=='Y' || answer=='y' ){  
       //code
}

Also, FFR:

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

like image 28
element119 Avatar answered Jan 09 '23 10:01

element119