help with yes no question in do while loop c++

Can someone please assist me with this portion of my code. When I enter n or N it still displays the contents of the else if statement before repeating the entire loop. I am unsure what I am doing incorrectly. Note** There are more lines of code above the original cout statement in the while loop that have been omitted, but I believe they have no bearing on the issue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  do
  {
    cout<< "Is the information displayed correct (Enter (Y)es or (N)o)? ";
    cin>> check;

    if (check == 'y' || check == 'Y')
    {
      break;
    }
    else if (check != 'N' || check != 'n')
    {
      cout<< Please enter Y or N: ";
      cin>> check;
    }
  }
  while (check == 'N' || check == 'n'); 
Last edited on
Here is a sample of my issue.

Is the information displayed correct(Enter (Y)es or (N)o)? n
Please enter Y or N:
Since you put in the while loop to check whether char check is N or n, then you don't need to use your first if statement if (check=='y' || check'Y') . You don't need an if statement at all. The code should be like this.

1
2
3
4
5
6
7
cout<<"Is the information displayed correct?"<<endl;
char check;
cin>>check;
do{
cout<<"Please enter Y or N: "<<endl;
cin>>check
}while(check=='N' || check=='n')

But I'm not sure this could be used anywhere. What is the full purpose of the program?
closed account (Dj60M4Gy)
So c++ cuts corners whenever possible.

You wrote > else if (check != 'N' || check != 'n') <.

It checks the first one, and the 'n' you typed is not equal to 'N'. Because you have that or symbol || there, it only cares if one thing is true, and because it saw true for the first thing, it executes the else if statement.

To get around that, use &&.
@Iloveores7

No, he mustn't use &&. From my understanding, this program reads some info from the user, then the user writes again the info until he decides it's correct. For example:

1
2
3
4
5
6
7
8
9
10
11
int x;
cin>>x;
cout<<"Is that the variable you want? Y(yes) N(no"<<endl;
char check;
cin>>check;

while (check=='n' || check=='N'){
cin>>x;
cout<<"Is that correct input?"<<endl;
cin>>check;
}

He uses || so it won't be needed to write it in small or capital letters, but both.
if check is Y, the while loop should stop.


Maybe a little more of my code will help.


do
{
cout<< "\n\nEnter amount of loan: ";
cin>> loan;
balance = loan;
cout<< "Enter interest rate: ";
cin>> rate;
rate = rate / 100;
cout<< "Enter monthly payment: ";
cin>> payment;
cout<< "\nIs the information displayed correct(Enter (Y)es or (N)o)? ";
cin>> check;

if (check == 'y' || check == 'Y')
{
break;
}
else if (check != 'N' || check != 'n')
{
cout<< "Please enter Y or N: ";
cin>>check;
}
}
while (check == 'N' || check == 'n');

Note: All the variables are already declared prior to this section.

What I was trying to accomplish was creating a failsafe to address if the user enters characters other than y or n, so it would prompt to only enter y or n. I also want to break out of the loop if the user enters y instead of say t, q, r or whatever. I am kind of scatter brained so I hope this makes sense. The code is also trying to repeat all the original questions if the user inputs an n indicating that the info entered was incorrect.
Last edited on
Sorry, I posted the last reply before seeing the response about &&. That seemed to fix my issue, thanks Iloveoreos7. I guess I will have to brush up on my logic.
Okay, so I thought this was fixed, but now if i input a value such as 'r', it will display the else if statement, but then when I input something like x or p or whatever it will break out of the loop. I am going to try another while loop instead of the else if statement and report back.
closed account (Dj60M4Gy)
@jgg2002

Let me first apologize, my previous statement about the if not checking all the statements was wrong.

But I was correct on the &&. He wants the if to run when it is either a y or Y, in this case he uses ||, because it can't be both at once. He wants the else if to run though when it is any other letter except for n or N. In this case both of those can be true. Q is not n or N at the same time. So because he needs to make sure check is both not n and not N, he needs the &&.

@newbie2047

The reason it repeats everything when you type n or N in is because your do ... while loop encompasses ALL of your code!
This is what I have now and it seems to work, but I have a repetitive if statement for checking for y or Y which I am told can always be avoided. Any ideas on how to clean this up, or is this the best i can do for the given situation I am trying to accomplish.

do
{
cout<< "\n\nEnter amount of loan: ";
cin>> loan;
balance = loan;
cout<< "Enter interest rate: ";
cin>> rate;
rate = rate / 100;
cout<< "Enter monthly payment: ";
cin>> payment;
cout<< "\nIs the information displayed correct(Enter (Y)es or (N)o)? ";
cin>> check;

if (check == 'y' || check == 'Y')
{
break;
}
while (check != 'N' && check != 'n')
{
cout<< "Please enter Y or N: ";
cin>>check;

if(check == 'y' || check == 'Y')
{
break;
}
}
}


@Iloveoreos7

I understand the repeat, as that is what I intended, sorry I was unclear on that part.
closed account (Dj60M4Gy)
@newbie2047

There is a little bit easier way to do that.

Try to have your while loop check to see if your variable is equal to y/Y also. So if it is either of those, it won't do the loop. Then you can remove both if statements that check for y/Y.

You'll also want a while at the bottom of your do loop to repeat when someone types in N.
@Iloveoreos7

I did what you said with the checks and it works great now. I already had the while at the bottom, apparently i need to proof read my posts better. Anyways, you have been a great help,
Thank you very much.
my topic in "coin toss repeat with different input" is similar concept to this but I couldn't get it to work in operate calculation from input 1 and input 2, etc, because at the end it just add the input 1 and input 2 then the percentage of heads and tails are just over 100%.... its like a mess after having to do a new input...
Topic archived. No new replies allowed.