By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

Unreachable code in function main()! cannot make it work!

P: 1
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 it works anyway, the warning message is just displayed when I compile, but does not stop me from compiling or running the program.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <iostream.h>
  3.  
  4. int main()
  5. {
  6.  
  7.   float num1;
  8.   float num2;
  9.   int num3;
  10.   float num4;
  11.  
  12.   cout << "Enter a number." << endl;
  13.   cin >> num1;
  14.  
  15.   cout << "Enter a second number." << endl;
  16.   cin >> num2;
  17.  
  18.   if (num1 == 0 || num2 == 0)
  19.   {
  20.     {
  21.     cout << "Choose an operation." << endl;
  22.     cout << "Type 1 to add, 2 to subtract or 3 to multiply." << endl;
  23.     cin >> num3;
  24.     }
  25.  
  26.     if (num3 >3 || num3 <1)
  27.     {
  28.       cout << "You operation choice isn't valid! Please run the program again." << endl;
  29.       cout << "Press Enter to end the proogram." << endl;
  30.       getchar();
  31.       return 0;
  32.     }
  33.     else
  34.     {
  35.       if (num3 == 1)
  36.       {
  37.         num4 = num1 +  num2;
  38.         cout << "Result is: "<< num4<< endl;
  39.       }
  40.       else if (num3 == 2)
  41.       {
  42.         num4 = num1 - num2;
  43.         cout << "Result is: "<< num4<< endl;
  44.       }
  45.       else if (num3 == 3)
  46.       {
  47.         num4 = num1 * num2;
  48.         cout << "Result is: "<< num4<< endl;
  49.       }
  50.  
  51.     }
  52.   }
  53.  
  54.   cout << "Press Enter to end the program." << endl;
  55.   getchar();
  56.   return 0;
  57.  
  58.   if (num1 != 0 || num2 !=0)
  59.   {
  60.     {
  61.     cout << "Choose an operation." << endl;
  62.     cout << "Type 1 to add, 2 to subtract, 3 to multiply or 4 to divide." << endl;
  63.     cin >> num3;
  64.     }
  65.  
  66.     if (num3 >4 || num3 <1)
  67.     {
  68.       cout << "You operation choice isn't valid! Please run the program again." << endl;
  69.       cout << "Press Enter to end the proogram." << endl;
  70.       getchar();
  71.       return 0;
  72.     }
  73.   }
  74.   else
  75.   {
  76.     if (num3 == 1)
  77.     {
  78.       num4 = num1 +  num2;
  79.       cout << "Result is: "<< num4<< endl;
  80.     }
  81.     else if (num3 == 2)
  82.     {
  83.       num4 = num1 - num2;
  84.       cout << "Result is: "<< num4<< endl;
  85.     }
  86.     else if (num3 == 3)
  87.     {
  88.       num4 = num1 * num2;
  89.       cout << "Result is: "<< num4<< endl;
  90.     }
  91.     else if (num3 == 4)
  92.     {
  93.       num4 = num1 / num2;
  94.       cout << "Result is: "<< num4<< endl;
  95.     }
  96.  
  97.   }
  98.  
  99.   cout << "Press Enter to end the program." << endl;
  100.   getchar();
  101.  
  102.   return 0;
  103. }
Oct 24 '10 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
The code is unreachable because you have:

Expand|Select|Wrap|Line Numbers
  1. return 0;
on the line above.

There is no way the execution can get past that return.

The warning can be removed just by deleting all the code after that return.
Oct 24 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.