Program Closing Out When Specific Input is Used

Hi All,

This is my first post here - I've been struggling for most of the day trying to figure out what I'm doing wrong on a class programming assignment. I hate to have to come here for assistance (I'm pretty stubborn) but I'm not sure where else to go at this point.

The problem I'm having is with a program that outputs the position and velocity of an object. The issue is that when I input a velocity of -500 (terminal velocity for the purposes of this assignment), the program closes out on me. I haven't run any other test cases that cause this anomaly.

So far I have tried:
- Reviewing my code repeatedly
- Reworking loops, including trying both while and do...while loops
- Posting to the class forum (where code may not be posted for obvious reasons)

...all to no avail. I'm hoping by posting the code here someone can help me to figure out what the problem is.

//-----------------------------------------------------
#include <iostream>
#include <iomanip>

using namespace std;

// Declare Constants
const string TITLE =
"Program will calculate the position and velocity of a falling object";
const string HEIGHT_PROMPT =
"Enter the initial height in feet (above 1000): ";
const string VELOCITY_PROMPT =
"Enter the initial velocity in ft/sec: ";
const string INVALID_HEIGHT =
"Invalid answer. Must enter height over 1000.";
const string INVALID_VELOCITY =
"Invalid answer. Must enter velocity under -500 ft/sec.";
const string HEIGHT_OUT =
"From a height of";
const string VELOCITY_OUT =
"An initial velocity of";
const string OBJECT_OUT =
"An object reaches";
const string AFTER_OUT =
"After";
const string POSITION_OUT =
"At a final position of";
const string FINAL_VEL_OUT =
"With a velocity of";
const int MIN_HEIGHT = 1000; // Minimum Height
const int MAX_VELOCITY = -500; // Maximum Velocity

// Declare Functions
double position(int time, double startVelocity, double startHeight);
double velocity(int time, double startVelocity);

int main()
{
// Declare Variables
int time = 0; // Time, Incremented From 0
int timeFinal; // Final Time
double startVelocity; // Starting Velocity
double startHeight; // Starting Height
double pos = startHeight; // Position (Starts as = startHeight)
double vel; // Calculated Velocity

// Display Purpose of Program
cout << TITLE << endl << endl;

// Prompt for Height Input
do {
cout << HEIGHT_PROMPT;
cin >> startHeight;
cout << endl;
if (startHeight <= MIN_HEIGHT)
cout << INVALID_HEIGHT << endl;
}
while (startHeight <= MIN_HEIGHT);

cout << endl;

// Prompt for Velocity Input
do {
cout << VELOCITY_PROMPT;
cin >> startVelocity;
cout << endl;
if (startVelocity < MAX_VELOCITY)
cout << INVALID_VELOCITY << endl;
}
while (startVelocity < MAX_VELOCITY);

// Display Output Header
cout << "Time" << setw(12) << "Position" << setw(12) << "Velocity" << endl;
cout << "----" << setw(12) << "--------" << setw(12) << "--------" << endl;

// Display Output
cout << fixed << showpoint << setprecision(2);

while (pos > MIN_HEIGHT) {

vel = velocity(time, startVelocity);

cout << setw(4) << time;
if (vel > MAX_VELOCITY) {
pos = position(time, startVelocity, startHeight);
}
else {
pos = pos + MAX_VELOCITY;
vel = MAX_VELOCITY;
}

cout << setw(12) << pos;
cout << setw(12) << vel << endl;
timeFinal = time;
time++;
}

cout << endl;

cout << HEIGHT_OUT << setw(17) << startHeight << " ft" << endl;
cout << VELOCITY_OUT << setw(11) << startVelocity << " ft/sec" << endl;
cout << OBJECT_OUT << setw(16) << MIN_HEIGHT << " ft" << endl;
cout << AFTER_OUT << setw(28) << timeFinal << " seconds" << endl;
cout << POSITION_OUT << setw(11) << pos << " ft and" << endl;
cout << FINAL_VEL_OUT << setw(15) << vel << " ft/sec";

cout << endl << endl;
system("PAUSE");

return 0;
}

// Function to Calcultate Position at Given Time
double position(int time, double startVelocity, double startHeight)
{
double positionTime = -16 * (time * time) + startVelocity
* time + startHeight;
return positionTime;
}

// Function to Calculate Object's Velocity at the Current Time
double velocity(int time, double startVelocity)
{
double velocityTime = -32 * time + startVelocity;
return velocityTime;
}
//-------------------------------------------------------------------------

Any advice or direction on this would be greatly appreciated!

Thanks!
Have you tried debugging by running the program line by line? This would tell you which line is causing it to crash, which would help you zero in on where your problem is.
Hi joatmon,

I've tried doing that, but it confused me more than it helped me - using the debugger is even newer to me than doing any programming! I spent some time going through the debugging tutorial provided to us, and everything made sense, but when I try using it on my program my brain gets all scrambled...

I realize ignorance isn't a good excuse, but right now, it's all I've got. I'd love to be able to use the debugger to get to the bottom of this, but don't know where to start.

Thanks!
OK - got it figured out. Looks like the problem was due to the following variable declaraton:
double pos = startHeight;
Because I assigned a value to this variable at the beginning of my main function, it was messing things up. I changed it to
double pos;
then assigned a value to it further down in the program.

Thanks!
Topic archived. No new replies allowed.