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

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

P: n/a
Okay, I have a problem.
I just started programming, and made my first "calculator" I then started making it more advanced, so it wouldn't crash if you tried to divide by 0.
Then I wanted to make it more advanced, so it wouldn't even give me the choice of dividing, if one of the two numbers I had typed in was 0. And now I get this erroe when I try to compile it: calculator.cpp 58: Unreachable code in function main() and I cannot find a way to fix it. Here is the code, if anyone is willing to help with this problem:
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
You have a return on the line above 58.

There is no way execution can get past that return.
Oct 24 '10 #2

Post your reply

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