I am trying to write code in C++ for a program that prompts the user for two numbers and finds the sum, difference, product, and quotient. The program should let you know that it cannot divide by zero. This is the code I have so far.
#include <iostream>
using namespace std;
int main() {
double a; //using double will allow user to input fractions
double b; //again using double will allow the user to input fractions
double sum, diff, prod, quot; //these variables will store the results
cout << "Enter a number: ";
cin >> a;
cout << "Enter another number: ";
cin >> b;
//Operations variables
sum = a + b;
diff = a - b;
prod = a * b;
//Conclusion
cout << "Sum is: " << sum << endl;
cout << "difference is: " << diff << endl;
cout << "product is: " << prod << endl;
if (b == 0) {
cout << "quotient is undefined";
}
else {
quot = a/b;
}
cout << "quotient is: " << quot << endl;
return 0;
}
This code compiles and runs. the only problem I seem to be having is the placement of my if else statement. I have tried multiple locations. The output I get if I let the second number = 0 is as follows
Enter a number: 12
Enter another number: 0
Sum is: 12
difference is: 12
product is: 0
quotient is undefinedquotient is: 0
How do I get it to to just say undefined if b is zero and give the answer if b is not zero.