ending a loop correctly

I need some assistance on how to make this loop end with this
"Sorry no good" or "See you later"

It has several function calls in the middle (that I have taken out) to save
some space.


Every thing I try it doesn't end the loop but goes right back into it...


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
39
40
41
42
int main()
{
		// declare variables
	char something' ';
	string something = "";
	int days = 0;
	double something = 0.0;
	
	cout << "Do you want to do this?: Y/N " << endl;
	cin >> doThis;	

		{	   	      
 	 if  (doThis!= 'Y' && doThis != 'N')           
		 cout << "Sorry no good" << endl;
    }
	
	while (doThis == 'Y') 
	{
	// declare variables
	char something = ' ';
	string something = "";
	double something = 0.0;


					
		 function calls;		
	
	
	 
	cout << "Do you want to something else?: Y/N " << endl; 
	cin >> doThis;
	{	   	      
 	 if  (doThis!= 'Y' && doThis != 'N')           
		 cout << "Sorry no good" << endl;
    }
} // end while
          cout << "See you later" << endl;
    
 
 system("pause");

	}  //end of main 


Try
1
2
3
4
5
if(doThis != 'Y' && doThis != 'N')
{
    cout << "Sorry no good" << endl;
    break;
}

that will end the while loop, if that's what you were aiming for.
that did work but now it goes to that and doesn't allow the loop to continue if the
cout << "Do you want to do this?: Y/N " << endl; equals = Y

I want the loop to continue if Y, to go to "sorry no good" if !=Y && !=N
and "see you later" if N.

Make sense?

That's where I'm having the issues
I think I got it but now I think I have un-neccessary code...
but I did this at the end


1
2
3
4
5
6
7
8
9
10
11
if(doThis == 'Y')
continue;

else if (doThis != 'Y' && doThis != 'N')
{
    cout << "Sorry no good" << endl;
    
else if    (doThis == 'N' )
cout << "later" << endl;
    break;
}
Your brackets are wrong, it should be:
1
2
3
4
5
6
7
8
if(doThis == 'Y')//one statement, no brackets
    continue;
else if (doThis != 'Y' && doThis != 'N')//one statement, no brackets
    cout << "Sorry no good" << endl;  
else if (doThis == 'N' ){//two statements, use brackets!!
    cout << "later" << endl;
    break;
}

Some people even use brackets for one statement, because it saves them work later on when they add more.
Last edited on
Topic archived. No new replies allowed.