suggestions please

I'm trying to make this work. Seems simple enough but it's returning only the first integer input. The invalid operator function isn't working as yet either. That is supposed to occur before user is allowed to input their 2 integers. I'm not quite done with the return into the loop after the question at the end. Any suggestions on how to get this to function right would be appreciated. Thanks

//program calculates 2 integers based on user selected operation

#include <iostream>

using namespace std;

int main()
{

// enter variables
char operation = ' ';
int num1 = 0;
int num2 = 0;
int sum = 0;
int difference = 0;
int product = 0;
int quotient = 0;

cout << "Input operation type(A, S, M, D): ";
cin >> operation;
if(toupper (operation) == 'a', 's', 'd', 'm'){
}
else
cout << "Invalid operator entered" << endl;

cout << "Enter two integers: (using comma in between) ";
cin >> num1, num2;

if(toupper (operation) == 'A'){
sum = num1 + num2;
cout << "Sum = " << sum << endl;
}
else
if(toupper (operation) == 'S'){
if(num1 > num2)
difference = num2 - num1;
else
difference = num1 - num2;
cout << "Difference = " << difference << endl;
}
else
if (toupper (operation) == 'M'){
product = num1 * num2;
cout << "Product = " << product << endl;
}
else
if(toupper (operation) == 'D'){
if(num1 > num2)
quotient = num1 / num2;
else
quotient = num2 / num1;
cout << "Quotient = " << quotient << endl;
}
cout << "Enter another operation?: (Y/N)";





system("pause");
return 0;
} //end of main function
closed account (D80DSL3A)
This:
1
2
cout << "Enter two integers: (using comma in between) ";
cin >> num1, num2;

Won't work as expected. A simple fix is to have the values separated by a space, not a comma:
1
2
cout << "Enter two integers: (using a space in between) ";
cin >> num1 >> num2;


Next, you can't chain tests together like this: if(toupper (operation) == 'a', 's', 'd', 'm')
Try this instead:
1
2
3
4
5
6
cout << "Input operation type(A, S, M, D): ";
cin >> operation;
operation = toupper (operation);// just change it!
if( operation == 'A' || operation == 'S' || operation == 'M' || operation == 'D' ){
    cout << "A valid operator has been entered" << endl;
}
Topic archived. No new replies allowed.