dimanche 1 février 2015

Why am I Stuck in a Loop?

I am asking the user to enter in the weight and radius of a sphere. If the weight is more than the buoyant force, then it will sink. Otherwise, if the weight is less than the buoyant force it should float.


This is not my first language. I have a introductory understanding to coding in Python and Java.


I am dealing with two issues:


1.) The while loop is being initiated even when the user enters "0" for the recalculate. I tested what the system is even seeing, which is still 0, but it still goes into the loop.


2.) I cannot exit the loop. Even whenever I am inside the loop, if I enter 0 for recalculate, I still go through the while loop.


I am entering into the loop when I shouldn't be entering, and I cannot break out of the loop.


I have even made an if statement:



if (recalculate = 0) {
break;
}


And this didn't work!


However, I cannot for the life of me figure out why I cannot break out of the loop.


What am I missing? What is different in C++ that is not allowing me to do this?


For some reason even when the user enter's "recalculate = 0" it still goes into the loop!? I do not know or understand why this is happening.



/* Defines the entry point for the console application.

Buoyant Force program.


*/

#include "stdafx.h"
#include <cstdlib>
#include "iostream"
using namespace std;


/*

Set up the program to look like what he wants.

Enter the radius of the sphere:
You entered:
enter the weight of the sphere
You entered:

Buyoant Force = Volume * Water Weight (62.4)

Decide whether or not the sphere will float or sink in the water.

Volume = (4/3) * pi * radius^3.



*/



int main()
{
double const PI = 3.141592;
double const waterWeight = 62.4;
int recalculate, redo;
double radius, weight, volume, bForce;
cout << "This program computes Buoyant Force in water given sphere radius." << "\n"
<< "Based on the weight of the sphere it will determines whehter the sphere floats or sinks.\n";

cout << "\nEnter in the radius of the sphere: ";
cin >> radius;
cout << "\nEnter in the weight of the sphere: ";
cin >> weight;
cout << "\n";


// Perform calculations.
volume = ((4.0 / 3.0) * PI * (pow(radius,3)));
bForce = (volume * waterWeight);

//Determine if floats or sinks.
if (bForce > weight)
{
cout << "\nEgads, it floats!\n";
}
else {
cout << "\nIt sunk...\n";
}

cout << "\nRecalculate? (1 = yes, 0 = exit)\n";
cin >> recalculate;

cout << "\n" << recalculate;

while (recalculate = 1){

cout << "\nEnter in the radius of the sphere: ";
cin >> radius;
cout << "\nEnter in the weight of the sphere: ";
cin >> weight;
cout << "\n";

if (bForce > weight)
{
cout << "\nEgads, it floats!\n";
}
else {
cout << "\nIt sunk...\n";
}

cout << "\nRecalculate? (1 = yes, 0 = exit)\n";
cin >> recalculate;
// See what recalculate really is.
cout << "\n" << recalculate;

}

//Continue to ask for inputs, and determine float for as long as user wants.

system("PAUSE");
return 0;
}

Aucun commentaire:

Enregistrer un commentaire