I am trying to write a code for a project and I am trying to make a while loop but it keeps repeating when I run it. I am also trying to get it to make a table but it wont show up either. Here is what I have so far. Here is also the steps to see what I am trying to go for:
https://i.stack.imgur.com/6jFPR.png
#include<iostream> //Required for cout
#include <cmath>
using namespace std;
const double h=1.3;
const double k= 0.3;
const double pi = 3.141593;
int main()
{
// Declare and initialize objects.
int time(0);
double t, SurfaceArea, Volume, Radius,InitialRadius , IR, R;
cout << "Hello user! please put in the Initial Radius ";
cin >> IR;
InitialRadius = IR;
cout << "Now please put in the time:";
cin >> t;
while (time <= 5.4)
{
cout<<"Time(sec) Radius (m) Surface area (m2) Volume (m3)";
cout << time << " " << Radius << " " << SurfaceArea << " " <<
Volume << endl;
Radius = k * InitialRadius*t;
SurfaceArea = pi*(pow(Radius*pow(t, 2) pow(h,2),0.5));
Volume = 0.33 * pi*pow(Radius,2)*h;
time = 10;
}// end loop
// Exit program.
return 0;
} //end main
CodePudding user response:
Things that are wrong (my machine refuses to compile it)
double t, SurfaceArea, Volume, Radius,
InitialRadius , IR, R, pi(2 * acos(0.0));
......
while (t <= 5.4)
{
cout << "Time(sec) Radius (m) Surface area (m2) Volume (m3)";
Radius = k * InitialRadius * t; <<<== you never give InitalRadius a value
SurfaceArea = pi * (pow(R * pow(t, 2) pow(h, 2), 0.5)); <<< likewise R
Volume = 0.33 * pi * pow(R, 2) * h;
Time = Time 0.19; <<<<<<==== TIme is an integer adding 0.19 to it wont do anything
}
Fix those things for a start
CodePudding user response:
It appears that 't' in your while loop never changes once it enters the loop. This will cause a infinite loop unless 't' is updated inside the loop. If 't' is supposed to change by 0.19, do
Time = Time 0.19; //change time to a double format, as integers do not allow decimals
t = time;
I believe IR is supposed to be InitialRadius, but it is not initialized to such. Do this after you input IR:
cin >> IR;
InitialRadius = IR;
R is not set to anything. If R is supposed to be Radius, make sure you set it as such:
R = Radius; //or just change the R's in your program to Radius
Are you trying to print out Time, Radius, Surface area, and Volume in table format? And are they supposed to be printed out each time the loop runs through? If so, you need to cout these as well.
cout << Time << " " << Radius << " " << SurfaceArea << " " << Volume << endl;
This will look something like:
Time(sec) Radius(m) Surface area (m2) Volume (m3)
# # # # //numbers here