My program is exitting before it is complete. Please Help :(

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.

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
#include<iostream>
using namespace std;


int main()
{
  double float 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;
}
double float C1, C2, R1, R2, length, pi;

compiling it might help give you an idea of errors.
Last edited on
if i remove that incorrect float keyword i do indeed get about 16cm.
not sure what your issue is?
I'm not quite sure what your problem is with "the final answer" but this code shouldn't even compile.
1
2
3
4
5
 double float C1, C2, R1, R2, length, pi;
//Should be just:
 double C1, C2, R1, R2, length, pi;
//or:
 float C1, C2, R1, R2, length, pi;


once you fix that it should work... it got .159155 for me.

Sean
- Line 7: double or float?
- What's the problem?
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.
1
2
cin.ignore();//add this
cin.get();//and this before return 0; to prevent the program to exit. 
Last edited on
closed account (3qX21hU5)
http://www.cplusplus.com/forum/beginner/1988/

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.
Last edited on
btw first you need the earth's diameter as a reference for the height above the earth. 12,715.43 km
Indeed, your professor is right. After changing line 7 to float the output is:

The band is 0.159155meters above the earth

Happy programming!
You can just simply add : system("pause");
Like this:
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
#include<iostream>
using namespace std;


int main()
{
  double float 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
}
@Zereo sorry I thought dkarayof is a beginner so I keep things simple ;)
closed account (3qX21hU5)
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.

@Chriscpp

True I guess I did go a little overboard ;p
Last edited on
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...
closed account (3qX21hU5)
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.
Last edited on
btw first you need the earth's diameter as a reference for the height above the earth. 12,715.43 km

i dont even understand what this means, but anyway, you dont need any further information.
Thank you, all is well. :)
Topic archived. No new replies allowed.