Expression Error

so I'm getting an error message that says expression must be an integral value or unscoped enum type, and also that ^ is an illegal operator, I'm still a bit new to c++ and I don't understand what the problem is.
(code isn't finished btw)

1
2
3
4
5
6
7
8
9
10
double pi = 3.14;
	int radius = 1;
	double area;

	while (radius >= 1 && radius <= 8) {
		cout << "The area is: " << area << endl;

		area = pi * radius ^ 2;

	}
Last edited on
1.) Lines 5 - 10 will execute indefinitely, because they are inside of a while loop. The condition of the while loop will never not be satisfied, which is why it will not stop. Did you perhaps intend to write "if" instead of "while"?

2.) On line 6, you're attempting to print 'area' which hasn't been initialized - it contains garbage. This is undefined behavior. You should move line 8 before line 6 so that area has a definite value. It also makes sense to initialize 'area' with some value just to be safe.

3.) The ^ operator isn't what you think it is. It's actually the bitwise XOR operator.
To take powers of numbers, use pow found in <cmath>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>

int main() {

	const double pi = 3.14159265;
	const int radius = 3;
	double area = 0.0;

	if (radius >= 1 && radius <= 8) {
		area = (pi * pow(radius, 2));

		std::cout << "Area:\t" << area << std::endl;

	}

	std::cin.ignore();
	return 0;
}
Topic archived. No new replies allowed.