473,408 Members | 2,161 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,408 software developers and data experts.

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

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
1 1160
weaknessforcats
9,208 Expert Mod 8TB
You have a return on the line above 58.

There is no way execution can get past 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
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.