I need some homework help, I think I got most of it; I just need somebody to look it over and tell me if there are any problems and why my program is exiting before it outputs the final answer.
Program Development
Assume that the earth is a perfect sphere, with a radius of 6371 km, and the equator was hugged with an inelastic and weightless band. You cut that band and inserted an additional piece one meter long. If the band floated freely above the earth, how much space would be between the earth and the band?
Mathematics:
The circumference of a circle = 2 * pi * radius
1 km = 1000 m
Develop the program using pseudocode
Enter, debug, and save the program to the hard drive.
Don't be too surprised when the answer comes out to be about 16 cm.
#include<iostream>
usingnamespace std;
int main()
{
doublefloat C1, C2, R1, R2, length, pi;
double height;
// Input data
cout << "\n What is the original radius of the sphere in km? \n";
cin >> R1;
R1= R1 * 1000; // Convert to meters
cout <<"\n The radius of the sphere is " << R1 << " meters";
// Calculate
pi= 3.14159;
C1= pi * 2 * R1;
cout << "\n The circumference of the band is " << C1 << " meters";
cout << "\n Enter the additional length added to the band in meters\n";
cin >> length;
//Calculate more
C2 = C1 + length;
R2 = C2 / (2 * pi);
height = R2 - R1;
cout << "\n The band is " << height << "meters above the earth";
cout << "\n\n ";
return 0;
}
I changed it to float, but it still just exits after I type in "1" for the second input. I emailed my professor and he said that the problem had to do with how I set up the IDE, but nothing else. I'm not really sure what that means.
Actually you don't even need the cin.get() like Chriscpp said it will do nothing and the cin.ignore won't be enough in all cases.
The best solution is this
1 2
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
What it is doing is exacting the maximum amount of character possible from the stream until a newline is found '\n'.
Add that right before your return statement on the bottom and it will stay open until you press enter. Read the thread above to figure out why it is happening.
Or if you can say what IDE you are using I can hopefully direct your to the setting that keeps the console open for you after the program terminates.
#include<iostream>
usingnamespace std;
int main()
{
doublefloat C1, C2, R1, R2, length, pi;
double height;
// Input data
cout << "\n What is the original radius of the sphere in km? \n";
cin >> R1;
R1= R1 * 1000; // Convert to meters
cout <<"\n The radius of the sphere is " << R1 << " meters";
// Calculate
pi= 3.14159;
C1= pi * 2 * R1;
cout << "\n The circumference of the band is " << C1 << " meters";
cout << "\n Enter the additional length added to the band in meters\n";
cin >> length;
//Calculate more
C2 = C1 + length;
R2 = C2 / (2 * pi);
height = R2 - R1;
cout << "\n The band is " << height << "meters above the earth";
cout << "\n\n ";
return 0;
system("pause");//This will pause the cmd
}
Please please do not use system("pause"); it is unsafe and not portable and if you professor knows his stuff he should take some points off for using it ;p. Please refer to this http://www.cplusplus.com/forum/articles/11153/ for more information.
I really don't get why people recommend this even though 100 times a day on this forum it is said that beginners(And advanced users) should not use it.
I will not use system("pause"); lol, thank you for the heads up. @zereo, I'm using Microsoft Visual C++ express, not sure if you're familiar with it...
One easy way to do so is instead of pressing F5 to run your project press ctrl-F5 instead which will keep the console open with a "press enter to continue message".
Note also that when you run with ctrl-f5 you are running without a debugger so if you need to use the debugger you can just put a breakpoint on the last line.