If statement not executing completely

I have an if statement that is in a program I created but it is hit or miss weather or not it will execute all the way. Below I have added a copy of the if function. Sometime you choose no for the first if statement it will exit the program. Sometimes it will accept the letter n and continue to the next one. All variables are declare as char (choice, choice2, Y, N). Any suggestions. Thanks in advance.

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
if (toupper(choice) == 'Y') { 
	{
	system("cls");
	rectangle();
	}
		if (toupper(choice) == 'N') { 
		{	
			system("cls");
			cout << "Would you like to go back to main?  Y or N" << endl;
			cin >> choice2;
		}		
				if (toupper(choice2) == 'Y') {
				{
				system("cls");
				main();
				}
					if (toupper(choice2) == 'N'){  
					{
					system("cls");
					cout << "Thanks for visiting" << endl;
					}
				}
			}
		}
	}
Last edited on
I am guessing you have too many braces {} per if statement. If you make your indentation more consistent and regular you may be able to see the problem better.
I made my indentation more consistent and then checked all my braces but still does same thing. I tried previously doing this with an if and else statement but it wouldnt accept the else just underlined it and states expected a statement.

Thanks again
if (toupper(choice) == 'N') is inside the if block of (toupper(choice) == 'Y').The only way it could be executed would be for choice to be n and y at the same time. Which, of course, is impossible.

As Galik says, you've messed up your braces. Here's your code better laid out so you can see it. I've not added or removed any braces. Just changed the layout so you can see your mistake.

Put simply, every one of your if blocks seems to have TWO opening { but only ONE closing } , and then at the end you've added a bunch of } to balance it out.

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
if (toupper(choice) == 'Y') 
{ // ONE opening {...
	{  // SECOND opening { - why is this here?
	system("cls");
	rectangle();
	} // Only ONE closing }, so we're still inside the first if block
		
        // See how we're still inside the first if block?
        if (toupper(choice) == 'N') 
        { 
	        {	
			system("cls");
			cout << "Would you like to go back to main?  Y or N" << endl;
			cin >> choice2;
		}		
		if (toupper(choice2) == 'Y') 
               {
			{
				system("cls");
				main();  // NEVER EVER CALL main. It's incorrect C++
			}
			if (toupper(choice2) == 'N')
                       {  
				{
					system("cls");
					cout << "Thanks for visiting" << endl;
				}
			}
		}
	}
}


Last edited on
Thank You very much. I have just started c++ and was confused on the amount of brackets.
Thank you again.
@mustang66

If your IDE (Integrated Development Environment) doesn't do braces for you automatically, you could try this:

When you type anything that comes in pairs () {}[]<>"" ' ' , type both of them, then go back and insert what should go inside. That way you will always have balanced pairs :+)

Your IDE should do indenting automatically too.

If you are one of these people who use a basic editor, then you might try strategies like the one above. There is nothing wrong with using a basic editor (for lots of people it is necessary), although it does seem a little hardcore in the face of the ubiquitous availability of IDE's on many systems.

Another thing: if you format your file to replace tabs with say 2 or 3 spaces (IDE can do this too) it will display better on this site. This site has 8 spaces per tab, so there is excessive indenting.

Good Luck !!
Do you know if visual studio 2012 does this?
I have never used VS much, but I gather that it does do these things.

Yes. Highlight the code and look under the file menu.. I think there is a code:format (there is also a code:comment selected and uncomment selected etc here). Hopefully this is correct for 2012, I get versions mixed a little.

I tend to get confused and I have coded for decades. My self defense against this is to always type both when I type one.. that is, if I type { I immediately type } and back up to fill in between. Also I insist that { and } are on a line by themselves at the correct indentation mark, and finally I comment any confusing (deep nesting) }s with a note as to what it ends. You get a little less code on a page or printout if you do this, but it really helps me. looks like

if(cond)
{
statement;
} //end if cond

instead of
if(cond){
statement
}//misaligned visually






IN VS 2012 the format command is under EDIT->Advanced.
Short cuts:
Ctrl+E+D to format the whole file
Ctrl+E+F to format the selection
Topic archived. No new replies allowed.