Hello, I have just finished writing up the code for a simple calculator however, I have gone to 'build and run' the program and I'm getting this error 'error: no match for 'operator>>' in 'std::cout >>. I was just looking for someone to look over this code and tell me what I have done wrong. I have tried to resolve this issue but it's not having it.
#include <iostream>
usingnamespace std;
void calc (int x, int y);
int result;
int x;
int y;
int sum;
char q,operation;
int main()
{
cout << "Calculator Version 5.0" << endl;
cout << "\nEnter two numbers then either press +, -, *, / to create\n a mathematical equation.";
cin >> x >> operation >> y;
calc (x,y);
cout >> "The answer is" <<result<< endl;
std::cin >> q;
return 0;
}
void calc (int _x, int _y)
{
x=_x;
y=_y;
switch(operation)
{case'+':
result = x + y;
break;
case'-':
result = x - y;
break;
case'*':
result = x * y;
break;
case'/':
result = x / y;
break;
default:
cout << "Please enter a valid mathematical operation." << endl;
cin >> x >> operation >> y;
calc (x,y);
}
}
line 18. you have your arrows the wrong way around.
btw: cin >> x >> operation >> y;
Does not match the order in which you've asked the user to enter stuff in.
I could enter "4 8 *" (as prompted), but you then read 4 and * in as your x and y, so you will always get your default message ("Please enter a valid mathematical operation.").
#include <iostream>
double calc(int x, int y, char operation);
int main()
{
// have moved variables into main
double result; // <-- needs to be double. integer division is bad
// initialise variables
int x = 0;
int y = 0;
char q;
char operation = '+';
std::cout << "Calculator Version 5.0" << std::endl;
std::cout << "\nEnter two numbers then either press +, -, *, / to create\n a mathematical equation.";
std::cin >> x >> y >> operation;
result = calc(x, y, operation); // <-- good to pass in the operator in here as well.
std::cout << "The answer is: " << result << std::endl;
std::cin >> q; // <-- not really sure what you are trying to do here??
return 0;
}
double calc(int x, int y, char operation)
{
double result = -1.0;
switch (operation)
{
case'+':
result = x + y;
break;
case'-':
result = x - y;
break;
case'*':
result = x * y;
break;
case'/':
result = x / static_cast<double>(y); // <-- must cast demoninator to double
break;
default:
std::cout << "Please enter a valid mathematical operation." << std::endl;
std::cin >> x >> y >> operation;
result = calc(x, y, operation);
}
return result; // <-- return the result
}
Hello again, I kind of understand the while loop however would you be able to sandwich lines 17-20 together, so I can understand the concept of looping?