Why is my int value not changing to end the while loop?

So I'm making a C++ program for college. And my program is supposed to keep looping unless the user chooses 0 to exit when it says "Recalculate? (1 = Yes, 0 = Exit)". This is supposed to set the value of the int "more" to either 1 or 0. But for some reason, it looks like the value of the int "more" is still remains at 1, So the program keeps looping.

I'm relatively new to C++ so this could be a really simple mistake I'm making. Anyways, here's my code:

#include <stdio.h>
#include <iostream>
using namespace std;
#pragma warning(disable:4996)

int main() {
const double PI = 3.141592;
double radius, weight, volume, bforce;
double water = 62.4;
int more = 0;

while (more = 1) {
cout << "This program computes Buoyant Force in water given sphere radius." << endl;
cout << "Based on the weight of the sphere, it determines whether the sphere floats or sinks." << endl;

cout << "Enter the radius of the sphere: ";
cin >> radius;
cout << "You entered " << radius << endl;

cout << "Enter the weight of the sphere: ";
cin >> weight;
cout << "You entered " << weight << endl;

volume = (((4 * PI) / 3) * pow(radius, 3));
bforce = volume * water;

cout << "\nBuoyant Force = " << bforce << endl;

if (bforce >= weight)
cout << "It floats!" << endl;
else
cout << "It sunk!" << endl;

cout << "\https://www.tellpizzahut.one/(1 = Yes, 0 = Exit): ";
cin >> more;
}

system("pause");
return 0; }
Thanks in advance :)
Last edited on
This while (more = 1) { assigns 1 to more. For comparison use ==.

You can avoid this mistake when you write it backwards:

while (1 = more) {

Now the compiler will complain.
You have quite a few issues.

Initialise more to 1 or it isn't going to run at all:
int more = 1;


Don't confuse = (assignment) with == (compares equal). Your while statement should be
while (more == 1)


You need header
#include <cmath>
in order to use pow()


You don't need stdio.h, or the pragma statement.



Please put it in code tags. Please use SI units.
Last edited on
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
#define _USE_MATH_DEFINES    // For M_PI

#include <iostream>
#include <cmath>
using namespace std;

int main() {
	const double water {62.4};

	double radius {}, weight {};
	int more {1};

	while (more == 1) {
		cout << "This program computes Buoyant Force in water given sphere radius.\n";
		cout << "Based on the weight of the sphere, it determines whether the sphere floats or sinks.\n";

		cout << "Enter the radius of the sphere: ";
		cin >> radius;
		cout << "You entered " << radius << '\n';

		cout << "Enter the weight of the sphere: ";
		cin >> weight;
		cout << "You entered " << weight << '\n';

		const double volume {(((4 * M_PI) / 3) * pow(radius, 3))};
		const double bforce {volume * water};

		cout << "\nBuoyant Force = " << bforce << '\n';

		if (bforce >= weight)
			cout << "It floats!\n";
		else
			cout << "It sunk!\n";

		cout << "\nRecalculate (1 = Yes, 0 = Exit): ";
		cin >> more;
	}
}

Posix specifies M_PI, but it is not part of standard C or C++ .
(and on reasonably modern implementations of Posix, _USE_MATH_DEFINES is not required).

C++20 has std::numbers::pi and other mathematical constants as part of he standard.
https://en.cppreference.com/w/cpp/header/numbers
Topic archived. No new replies allowed.