Thread: having syntax error with closing braces

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    having syntax error with closing braces

    Hello every1 i`m new here and also new to C++, i really need some1 to help me spot what went wrong in my code , at the end of the code the braces dont close properly. I have been scanning through my code for over an hour and still cant find what i did wrong.

    i know this is a very dumb problem and i should find the problem eventually if i continue staring at my code. But my assignment deadline is coming i really cant afford to waste precious time trying to figure out what went wrong.

    any helpful reply is very much appreciated C:

    PS:i know my code is ridiculous, please dont laugh...

    Code:
    	while (loop3 == 1)
    	{
    		if (room == 1)
    		{
    			while (northwingloop == 1)
    			{
    				system("cls");
    				northwing();
    
    				cout << "\n\n[1] - examine bookshelf\n\n" << "[2] - examine table\n\n" << "[3] - examine room\n\n" << "[4] - move forward\n\n";
    
    				cin >> i;
    
    				if (i == 3)
    				{
    					system("cls");
    					northwing();
    					cout << "There is nothing suspicious here.\n\n";
    					cin.ignore();
    					cin.get();
    				}
    
    				if (i == 2)
    				{
    					if (tablestatus == 8)
    					{
    						system("cls");
    
    						torchlight();
    
    						cout << "\n\nYou found a torchlight on the table.\n\n" << "[1] - Take the torchlight\n\n" << "[2] - Do nothing\n\n";
    
    						cin >> a;
    
    						tablestatus = itemstatus();
    					}
    					else if (tablestatus == 9)
    					{
    						system("cls");
    						northwing();
    						cout << "\n\nJust a regular table.\n\n";
    						cin.ignore();
    						cin.get();
    					}
    				}
    				if (i == 1)
    				{
    					system("cls");
    					northwing();
    					cout << "\n\nJust a few boring game programming books.\n\n";
    					cin.ignore();
    					cin.get();
    					
    				}
    				if (i == 4)
    				{
    					northwingloop = 2;
    					room = 2;
    				}
    			}
    		}
    		if (room == 2)
    		{
    			while (loop == 1)
    			{
    				loop2 = 1;
    				system("cls");
    				hallway();
    
    				cout << "\n\n[1] - examine left door\n\n" << "[2] - examine right door\n\n" << "[3] - examine hallway\n\n" << "[4] - move forward\n\n" << "[5] - go back\n\n";
    
    				cin >> i;
    
    				if (i == 3)
    				{
    					if (batterystatus == 8)
    					{
    						system("cls");
    
    						battery();
    
    						cout << "\n\nYou found a battery on the floor.\n\n" << "[1] - Take the battery\n\n" << "[2] - do nothing\n\n";
    
    						cin >> a;
    
    						batterystatus = itemstatus();
    						
    						loop = 1;
    					}
    					if (batterystatus == 9)
    					{
    						system("cls");
    						hallway();
    						cout << "\n\nThere is nothing suspicious here\n\n";
    						cin.ignore();
    						cin.get();
    						loop = 1;
    					}
    				}
    				if (i == 1 || i == 2)
    				{
    					while (loop2 == 1)
    					{
    						system("cls");
    						door();
    
    						cout << "\n\n[1] - open door\n\n";
    						cout << "[2] - dont open door\n\n";
    						cout << "[3] - go back to hallway\n\n";
    
    						cin >> i;
    
    						if (i == 3)
    							loop2 = 2;
    
    						else if (i == 2)
    							loop = 1;
    						else if (i == 1)
    						{
    							loop = 2;
    							loop2 = 2;
    							system("cls");
    							dooropen();
    						}
    					}
    				}
    				if (i == 5)
    				{
    					northwingloop = 1;
    					room = 1;
    				}
    			}
    		}
    		}
    		}

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You need to use a good IDE to spot these errors .

    A text editor, called Kate, pointed out by allowing me to fold blocks, that you've an extra '}' at the end.
    Beware that it may not be extra but also that you've something missing in between.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    How do you mean they don't close properly? If it's just indentation you can simply delete the white space in front of the braces at the end until they align properly. Does the code compile and run?

    I noticed that you do have one extra brace at the end of your code, but I'm guessing that's the closing brace for your main() function - and remember to make sure that it is int main(), not void main().

    Edit: Also, if it doesn't compile, post the errors your compiler spits out.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    First off, I congratulate you for your indentation. I'm not a huge fan of eight spaces, but it is clean and consistent (except the last two lines) and it makes it really easy to spot any brace errors you have. The idea is: every time you move backwards, or intend to move backwards, there should be a close-brace on that line, and you've done that -- the braces match your indentation perfectly. The last line of code has a brace that doesn't match anything you posted (presumably it's the block that this code is in). More than that, we can't really say. If the compiler is complaining about extra close braces or unclosed open braces, then you need to look a little wider. If the logic isn't working out, then use the visual tools that the indentation gives you to make sure that you are closing things off when you should.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    thats the problem , at the end it doesnt align properly the code runs fine only a part where its supposed to loop it doesnt.

    the end should have been

    Code:
    		}
    	}
    }
    instead of

    Code:
    		}
    		}
    		}

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So in that case the only left for you to do is think. We don't know exactly what you intend the logic to do, so you need to look at all your blocks and ask yourself: is this the amount of code that should be repeating here? If not, then you need to adjust your blocks.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    All you need to do is delete the extra white space in front of the braces using backspace or delete. It's just a text editor, and the white spaces are just tabs. It's simple enough to correct the indentation here:

    Code:
    		}
    			} // Put the cursor before the brace and press the TAB key
    			   // to add another "level" of indentation
    	} // Put the cursor before the brace and press
               // the BACKSPACE key to remove a "level" of indentation

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    maybe i`m wrong but visual studio 2008 always automatically close my braces properly and if it doesnt it means something in my code went wrong

    etc i forgot to close a bracer somewhere.

    anyway i found my error it was something stupid and easy but i just couldnt find it before.

    thanks a lot guys a really appreciate the help and friendliness of the community here C:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  2. error C2143: syntax error : missing ')' before ';'
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:06 AM
  3. Replies: 2
    Last Post: 02-28-2008, 11:51 PM
  4. error: braces around scalar initializer...
    By Osiris990 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2008, 03:22 PM
  5. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM