473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
1 1604
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

1
by: SteadySteps | last post by:
Hi I migrated a project which compiles correctly on VC 6.0 to VS 2002. However now all I get several warning that all the statements within catch blocks are "unreachable code". How can I correct...
2
by: Hovik Melikyan | last post by:
This code produces a 'unreachable code' warning at line 16 (throw new X ...) with no visible reason: #include <string> class X { std::string msg;
8
by: Rody Reulen | last post by:
I made an console application with Visual Basic .net. Visual Basic will automatically create the sub procedure main for you. I try to convert the main procedure to a function. During the...
6
by: dfetrow410 | last post by:
Can I do this in an if statement? public string getClass() { counta = counta + 1; if (counta < 2 ) {
4
by: Gernot Frisch | last post by:
I get informed that parts of my code cannot be reached. Did I do something wrong? try { if (login(progstring) > 0) { return 1; // OK } }
1
by: tam | last post by:
when i am using session variable i found one error that unreachable core error. how to rectify this error
1
by: web20tester | last post by:
Hi all, I am new to javascript. I have this script working fine in IE but cannot work in firefox. Really appreciate on any guidance. search0.js function Search(str) { var...
3
by: cie | last post by:
Hi, anyone please help me when I try to close a connection to the database from a static method, "Unreachable code detected" error occured. What's the problem actually? Thank you before
2
by: alireza6485 | last post by:
Hi, I am getting "Unreachable code dected " for my cariable n.Can you please let me know how to fix the problem.Thanks Console.WriteLine(); Console.Write("Decoded word :"); for...
1
by: DavidSS | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.