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