1 error within Calculator

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #include <iostream>

using namespace 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);
    }
}
hello again.

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.").

And using global variables is very bad.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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
}
Last edited on
On len 18: cout >> "The answer is" <<result<< endl; // Wrong direction
I am now wondering how can I ask the user if they want to repeat the program?
then you need to sandwich lines 17 to 20 in a while loop and add some logic to check if the user wants to go again.

edit: that's lines 17 to 20 in my modified example sorry.
Last edited on
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?
Topic archived. No new replies allowed.