simple question- not ruinning as expected

This is a discussion on simple question- not ruinning as expected within the C++ Programming forums, part of the General Programming Boards category; no, thats why i posted it here in the first place. when i try to compile this code, which is ...

  1. #16
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    no, thats why i posted it here in the first place.

    when i try to compile this code, which is how i want it to be, it says that there is an error on the line
    Code:
    else if (answer ==1)
    any ideas?

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    It's been said several times. You must pair an else if with an if. You cannot have a floating else if just sitting in the middle of the code.

  3. #18
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    yes i no this has been said already- also as i have said, and annotated in code, it is paired with an if statement.

    unless i have to do something special to pair it with an if statement i cant see wat you mean

  4. #19
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    here is wat i mean


    Code:
    	if (answer != 1)//this is the if that the else if shoud be paired with
    	{
    		std::cout << "\nWell if you're not going to believe me. . .\n";
    		std::cout << ". . .maybe I should add some more. Would you like that?\n";	
            
        std::cout << "\n\n1 = yes\n";
    	std::cout << "2 = no\n\n";
    	
    	std::cout << ">> ";
    	std::cin >> answer;   //you forgot to get the answer for the 2nd question
    	std::cin.get();            //catch the enter input
        }
    
       else  if (answer == 1)//this is the else if
       {
             for (int y = 0; y < 100000; y++)
             {
                 std::cout << y << std::endl;
             }
             std::cout << "\nGood....Thats very good. For i have now removed them all!\n";
             std::cout << "Press enter to quit\n";
             std::cin.get();
        }
        std::cout << "\n\nAnd again please....\n\n";
        std::cin.get();
    
        return 0;

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    That code is different than the code you posted earlier. In that code, the else if is correctly paired with the if above it, because the statement after the if is a block of code (the code between and including the braces) followed immediately by the else. In the earlier code that was wrong, there were several statements after the if and before the else if that were not enclosed in braces to form a single block, which is why the else if had nothing to pair with and got the error.

    If you still get an error with the above code exactly as is, you should start a new thread with this new error, the exact error message including line number, and the full code.

  6. #21
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    ah i see. the complete problem was me not putting the braces in the right place.

    thanks to everyone that has tried to tell me what i was doing wrong

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21