help correcting code

The task was to write a program that reads two numbers and stores them into variables double x,y; Then the code finds the values of the sum and difference of the two numbers and stores them in double sum, diff; Then the code determines whether the sum is larger/equal/smaller than the difference.

I don't know what I did wrong. Please help. Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main () {
double x, y;
cout << " Enter two numbers. ";
cin >> x >> y >> endl;
double sum, diff;
sum = x+y;
cout << x << " + " << y << " = " << sum << endl;
diff = x-y;
cout << x << " - " << y << " = " << diff << endl;
if (sum>diff) {
cout << sum << " is larger than " << diff << endl;
}
else if (sum<diff) {
cout << sum << " is smaller than " << diff << endl;
}
if (sum==diff) {
cout << sum << " is equal to " << diff << endl;
}

return 0;
}
Last edited on
I don't know what I did wrong.

The extraction operator (>>) has no overload for operands of types std::basic_istream<char> and a function pointer to std::endl.

http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
How do I fix it?
mikey108 wrote:
How do I fix it?
1
2
3
4
std::cout << "Enter x: ";
std::cin >> x;
std::cout << "Enter y: ";
std::cin >> y;


admkrk is right sorry for the extras...
Last edited on
While in my opinion it's better to ask for the numbers separately, there is no reason to add
std::cin.ignore();
to any of it.

All he has to do is remove what was pointed out at first.
I got it, thanks to all!
Topic archived. No new replies allowed.