Thread: What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Unhappy What am I doing wrong?

    Hi there!

    I am new to C++ and well.. I was experimenting on my own with a calculator program.. Just trying to get my feet wet.. I don't know what is wrong with this program, but I keep getting parse errors before the else keywords.. Here is the code snippet that is giving me trouble:

    if (operationChoice > 1)
    {
    if (operationChoice == 2)
    answer = Subtraction(firstNumber, secondNumber);
    std::cout << "The answer is: " << answer << " .\n\n";
    else
    {
    if (operationChoice == 3)
    answer = Multiplication(firstNumber, secondNumber);
    std::cout << "The answer is: " << answer << " .\n\n";
    else
    answer = Division(firstNumber, secondNumber);
    std::cout << "The answer is: " << answer << " .\n\n";
    }
    }
    else
    answer = Addition(firstNumber, secondNumber);
    std::cout << "The answer is: " << answer << " .\n\n";

    I have all the variables declared and the functions prototyped.. Cases all match, no typos or anything.. I will also upload the .cpp file so if this code snippet is not enough.

    Any suggestions would be appreciated.

    Wanderin Weezard
    [email protected]

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    Your else are 'dangling'. Try putting all the if-then-else conditions in braces. Like this:
    Code:
    if ( some condition )
    {
       statement_1;
       statement_2;
    }
    else
    {
       statement_3;
       statement_blah;
    }
    This should take care of compile time errors.

    A better thing here would be to use switch instead of multiple ifs.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Talking Thanks!

    Thanks for your help!!! I looked up switch and my program works fine now!!

    WW

  4. #4
    Registered User geekfrog's Avatar
    Join Date
    Apr 2002
    Posts
    14
    EDIT: Forget what i said. Didn't read whole post...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM