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;
}