Thread: Unreachable code in function main()

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Unreachable code in function main()

    I just started programming, and I followed a tutorial to make a calculator. Once I was done, I wanted to make it more advanced, so it wouldn't crash if I try to divide by 0, but instead not giving me the choice of dividing. I thought I was done, but then it simply just skipped the part where it actually shows the result. I tried to fix it, but now I get the calculator.cpp 58: Unreachable code in function main() error.
    This is my code. Anyone who can solve the problem? Okay, it seems that it works anyway, and that it is just a warning when I compile. It does not stop it from compiling.
    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main()
    {
    
      float num1;
      float num2;
      int num3;
      float num4;
    
      cout << "Enter a number." << endl;
      cin >> num1;
    
      cout << "Enter a second number." << endl;
      cin >> num2;
      
      if (num1 == 0 || num2 == 0)
      {
        {
        cout << "Choose an operation." << endl;
        cout << "Type 1 to add, 2 to subtract or 3 to multiply." << endl;
        cin >> num3;
        }
      
        if (num3 >3 || num3 <1)
        {
          cout << "You operation choice isn't valid! Please run the program again." << endl;
          cout << "Press Enter to end the proogram." << endl;
          getchar();
          return 0;
        }
        else
        {
          if (num3 == 1)
          {
            num4 = num1 +  num2;
            cout << "Result is: "<< num4<< endl;
          }
          else if (num3 == 2)
          {
            num4 = num1 - num2;
            cout << "Result is: "<< num4<< endl;
          }
          else if (num3 == 3)
          {
            num4 = num1 * num2;
            cout << "Result is: "<< num4<< endl;
          }
    
        }
      }
    
      cout << "Press Enter to end the program." << endl;
      getchar();
      return 0;
    
      if (num1 != 0 || num2 !=0)
      {
        {
        cout << "Choose an operation." << endl;
        cout << "Type 1 to add, 2 to subtract, 3 to multiply or 4 to divide." << endl;
        cin >> num3;
        }
    
        if (num3 >4 || num3 <1)
        {
          cout << "You operation choice isn't valid! Please run the program again." << endl;
          cout << "Press Enter to end the proogram." << endl;
          getchar();
          return 0;
        }
      }
      else
      {
        if (num3 == 1)
        {
          num4 = num1 +  num2;
          cout << "Result is: "<< num4<< endl;
        }
        else if (num3 == 2)
        {
          num4 = num1 - num2;
          cout << "Result is: "<< num4<< endl;
        }
        else if (num3 == 3)
        {
          num4 = num1 * num2;
          cout << "Result is: "<< num4<< endl;
        }
        else if (num3 == 4)
        {
          num4 = num1 / num2;
          cout << "Result is: "<< num4<< endl;
        }
    
      }
    
      cout << "Press Enter to end the program." << endl;
      getchar();
    
      return 0;
    }
    Last edited by DavidSS; 10-24-2010 at 06:59 AM. Reason: Found a solution.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have a premature return 0; in your main function, presumably a by-product of careless copying and pasting.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Last edited by DavidSS; Today at 08:59 AM. Reason: Found a solution.
    Yeah, I'm guessing in this cross-post?

    Cross-posting is highly frowned upon and shows a lack of consideration on your part for the time spent by people willing to help you for free.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Quote Originally Posted by rags_to_riches View Post
    Yeah, I'm guessing in this cross-post?

    Cross-posting is highly frowned upon and shows a lack of consideration on your part for the time spent by people willing to help you for free.
    Yes and no. Yes I did ask on three forums, because I know from past experience, that I am likely not to get the answer I am looking for on just a single forum, so I posted on three forums, because I really want to find a solution to my problem.
    Then no part, is because I found out my self, that the message was just a warning and not an error, so it did not stop the compiling.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DavidSS
    Yes I did ask on three forums, because I know from past experience, that I am likely not to get the answer I am looking for on just a single forum, so I posted on three forums, because I really want to find a solution to my problem.
    Be patient: ask on one forum, and if after some reasonable length of time you do not get an answer, ask on another forum, while keeping both forums informed of the duplication.

    Quote Originally Posted by DavidSS
    Then no part, is because I found out my self, that the message was just a warning and not an error, so it did not stop the compiling.
    In this case, the warning is legitimate and useful: it tells you that you have this part of your code that will not be executed, regardless of the input. Control cannot reach it, hence it is unreachable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by DavidSS View Post
    Yes and no. Yes I did ask on three forums, because I know from past experience, that I am likely not to get the answer I am looking for on just a single forum, so I posted on three forums, because I really want to find a solution to my problem.
    Then no part, is because I found out my self, that the message was just a warning and not an error, so it did not stop the compiling.
    Right. You got an answer in both places within 20 minutes ON A WEEKEND. Please don't try to justify your inconsiderate behavior.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change it into one main() function code..
    By transgalactic2 in forum C Programming
    Replies: 10
    Last Post: 12-13-2008, 10:02 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

Tags for this Thread