Why Is My C++ Program Not Running as Expected?

In summary: I'm assuming that means that my program crashed.I'm pretty sure I messed something up with my calculations, but I can't seem to figure out what it is. Can you help?
  • #1
asz304
108
0
Hi. I need help with in running my program, when I tried it in the computer lab, my program didn't run..And I'm sure it couldn't run because of my workings..which I assume are incorrect. I appreciate it if you could tell me where did I go wrong with my calculations and if it matched the "Input/Output" example in the Assignment Problem Source.

Homework Statement


Assignment Problem Source: http://www.engr.mun.ca/~markevans/engr1020/Eng1020Labs/Assignments/Entries/2010/10/7_Assignment_3__Mark_the_lazy_cyclist.html"



The Attempt at a Solution




My Workings:
#include <iostream>
#include <cmath>
using namespace std;

void outResults1(float deceleration,float v_final, float v_initial, int time);
int time ( float deceleration, float v_average, float v_inital, float v_final);
void outResults2(int time, int seconds);
float calculatedistance ( int time, float distance, float v_average);
void outResults3(float distance, float v_average, float time );

/********************************************************************
* main --
*
* @params: argc -
* argv - the value of the command line arguments as array of character pointers
* @descript: main entry point for program, handle inputs and outputs.
*
* @returns: 0 for execution success.
********************************************************************/


int main( int argc, char *argv[] )
{
float deceleration; //Deceleration Of Mark
int time; //Time spent for coasting
float distance; //Distance coasted
float v_final;
float v_initial;
float v_average;

outResults1(deceleration, v_final, v_initial,time);
deceleration = (v_final - v_initial)/time;
outResults2(int time, int seconds);
time = distance/v_average;
outResults3(distance,v_average, time );
distance = v_average*time;
}

/********************************************************************

deceleration - the acceleration rate of Mark

@params: none
@modifies: cin
cout
-Prompts the user and inputs the following:
Initial speed of in kph
Speed after 1 minute in kph
@pre: none

@returns: acceleration rate of Mark
******************************************************************/

float deceleration()
{
float deceleration = 0.f;
float v_initial = 34.7;
float v_final = 24.3;
float v_average = 0.f;
float distance = 0.f;
int time;

cout << "Please enter the initial speed in kph with decimal:" << endl;
cin >> v_initial;
cout << "Please enter the speed after 1 minute in kph with a decimal" << endl;
cin >> v_final;

time = distance/v_average;
deceleration = ( v_final - v_initial )/ time;


return deceleration;
}

/********************************************************************

* outResults1 - outputs the computed deceleration
*
* @params: deceleration--deceleration of Mark.
* @modifies: cout -- Outputs the results.
*
* @pre: none
*
* @returns: nothing
* ********************************************************************/

void outResults1(float deceleration, float v_final, float v_initial, int time )
{
cout << "\n Mark's deceleration rate is: " << ( v_final - v_initial ) / time;
}
/********************************************************************

@params: time - time spent coasting
v_average - average speed of Mark
deceleration - acceleration rate of Mark
@modifies: nothing
@pre: none

@returns: time
********************************************************************/

int time ( float deceleration, float v_average, float v_inital, float v_final)
{
int time = 0.f;
v_average = (v_inital + v_final)/ 2;
time = (v_final - v_inital)/ deceleration;

return time;
}

/********************************************************************

* outResults2 - outputs the time spent for coasting
*
* @params: time- time spent for coasting
* @modifies: cout -- Outputs the results
*
* @pre: none
* @returns: nothing
********************************************************************/

void outResults2 ( int time, int seconds )
{
cout << time / 60 << "minutes and " << time % 60 << seconds;
}
/******************************************************************** distance - distance reached after coasting
v_average - average speed
time - time spent coasting
@modifies: nothing
@returns: distance
********************************************************************/

float calculatedistance ( int time, float v_average)
{
float distance = 0.f;
distance = v_average*time;

return distance;
}
/******************************************************************** *
outResults3 - outputs the distance reached after coasting
*
* @params: distance- distance reached after coasting
* @modifies: cout - Outputs the results
*
* @returns: nothing
* ********************************************************************/

void outResults3 ( float distance, float v_average, float time )
{
cout << "\nThe distance that Mark reached after coasting is : " << v_average*time;
}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
What happens when you run it? It's a lot easier to debug if you see what's behaving wrongly.

That said, there are three "obvious" problems:

You never call the functions deceleration, time, or calculatedistance. They look important, so this is surely a problem.


You have variables in main with the same name as those functions. Not a problem per se, but it does tend to be confusing (a confusion I suspect is directly related to your problem with the assignment). Also, time is generally a bad choice for a variable name, since there is a standard library function that has that name.


The intent of several functions seems to be to modify the arguments passed into them. However, this is useless since the functions are declared to take copies of arguments, rather than references to the originals.
 
  • #3
Another big problem is that the first function called in main, outResults1, has arguments that are uninitialized.
 
  • #4
When I ran it on the computer lab. It says something about "terminated" and when I built my project I fixed all the errors, but the project can't run. And I don't have eclipse on my laptop, so I can't check out your suggestion.

Mark44 said:
Another big problem is that the first function called in main, outResults1, has arguments that are uninitialized.

How should I fix it?Why is it uninitialized?

@Hurky

How do I call them? But.. I thought I called them.
 
Last edited:
  • #5
The arguments to outResults1 (and possibly other functions) are uninitialized because you didn't initialize them. Your outResults1 function expects to be given (i.e., passed as parameters) four values when it is called: deceleration, v_final, v_initial, and time.

These variables are declared before outResults1 is called, but they are uninitialized, which means that they will have whatever values are lying around in the memory that is allocated for them. IOW, they will have "garbage" values.

As Hurkyl already pointed out, your deceleration function is not called, and it looks to me like it gets the initial speed and final speed from user input and calculates deceleration and time. In any case, this function should be called before outResults1 is called. As Hurkyl also pointed out, you have a variable named deceleration and a function named decleration, and these two names have very likely confused you.

One other point. You are using comments to describe the various functions, and this is a good thing. However, if the comments don't match what the functions are doing, then they are arguably worse than no comments. For example, you have this comment block above outResults1.
Code:
/************************************************** ******************

* outResults1 - outputs the computed deceleration
*
* @params: deceleration--deceleration of Mark.
* @modifies: cout -- Outputs the results.
*
* @pre: none
*
* @returns: nothing
* ************************************************** ******************/
void outResults1(float deceleration, float v_final, float v_initial, int time )
For params, there are four parameters, not one. Each of them should be described.
For pre (preconditions), you have None, but the precondition for this function is that each of the four parameters should have been initialized with values, either from user input or calculated.
 
  • #6
asz304 said:
When I ran it on the computer lab. It says something about "terminated" and when I built my project I fixed all the errors, but the project can't run.
Are you sure it didn't run and finish immediately?

How do I call them? But.. I thought I called them.
Where do you think you called them? (I bet the line you will point to will be not a function call, but the declaration of a variable)
 
  • #7
The reason it crashes is likely that time = distance/v_average;
gets executed in main() while v_average is unitialized and probably 0.

you modify the paramater v_average in the function time but:
- modifying a "by value" parameter (the default in c++) won't affect anything outside of th e function. you would need a reference parameter: float &v_average
- time() never gets called anyway
 
  • #8
Thanks guys. I followed your tips and here is my current program, the only problem is the calculations...

#include <iostream>
#include <cmath>
using namespace std;

void outDescription ();
float deceleration (float acceleration, int time);
int time ( float deceleration, float v_average, float v_initial, float v_final );
void outResults2(int time);
float calculatedDistance ( int time, float v_average );

/***************************************************************************************************************************************************
* main --
*
* @params: argc - the variable that contains the number of parameters (argument count) added to the command line when the program was run.
* argv - the value of the command line arguments as array of character pointers
* @descript: main entry point for program, handle inputs and outputs.
*
* @returns: 0 for execution success.
***************************************************************************************************************************************************/

int main( int argc, char *argv[] )
{
float acceleration = 0.f;
int coastTime = 0.f;
float distance = 0.f;
float v_final = 0.f;
float v_initial = 0.f;
float v_average = 0.f;



outDescription ();

cout << "Please enter the initial speed in kph with decimal:" << endl;
cin >> v_initial;
cout << "Please enter the speed after 1 minute in kph with a decimal" << endl;
cin >> v_final;

acceleration = deceleration(float deceleration, int time);

cout << "Mark's acceleration rate is" << acceleration ;

coastTime = time( acceleration, v_average, v_initial, v_final);

cout << "\nMark's total coast time is:" << coastTime;

outResults2( coastTime );

distance = calculatedDistance ( coastTime, v_average);

cout << "\nThe distance that Mark reached after coasting is : " << distance;

return 0;
}

/****************************************************************************************************************************************************
* outDescription - outputs the description of the program
*
* @params:
* @modifies: cout - outputs the description
*
* @return: none
*****************************************************************************************************************************************************/

void outDescription()
{
cout << "This program plays Marks crazy prairie cycling game\n";
cout << "and extrapolates deceleration, time and distance given 2 speeds 1 minute apart." << endl;
}

/****************************************************************************************************************************************************
deceleration - the acceleration rate of Mark

@params: none
@modifies: cin
cout
-Prompts the user and inputs the following:
Initial speed of in kph
Speed after 1 minute in kph
@pre: none

@returns: acceleration rate of Mark
*****************************************************************************************************************************************************/

float deceleration()
{
float acceleration = 0.f;
float v_initial = 0.f;
float v_final = 0.f;
int time = 1;

acceleration = ( v_final - v_initial ) / time;

return acceleration;
}

/*****************************************************************************************************************************************************
@params: time - time spent coasting
v_average - average speed of Mark
deceleration - acceleration rate of Mark
@modifies: nothing
@pre: none

@returns: time
******************************************************************************************************************************************************/

int time( float deceleration, float v_average, float v_initial, float v_final)
{
int coastTime = 0.f;

v_average = ( v_initial + v_final ) / 2;
coastTime = ( v_final - v_initial ) / deceleration;

return coastTime ;
}

/******************************************************************************************************************************************************
* outResults2 - outputs the time spent for coasting
*
* @params: time--time spent for coasting (minutes)
* seconds--time spent for coasting ( and seconds )
* @modifies: cout -- Outputs the results
*
* @pre: none
* @returns: nothing
*****************************************************************************************************************************************************/

void outResults2 ( int time )
{
cout << time%60 << "minutes and " << (time - (time%60)*60 )<< "seconds";
}

/*****************************************************************************************************************************************************
@params: v_average - average speed
time - time spent coasting
@modifies: nothing
@returns: distance
*****************************************************************************************************************************************************/

float calculatedDistance ( int time, float v_average)
{
float distance = 0.f;
distance = v_average*time;

return distance;
}
 
  • #9
Here's an excerpt of your code that shows a big problem. In main, you set v_initial and v_final. These variables are local variables in main.

When you call the deceleration function in main, it always returns 0.0 for the acceleration. The reason for this is that v_initial and v_final are also declared in the deceleration function, and are local to that function. Although they have the same names as the variables in main, they are different variables.

In the deceleration function, v_initial and v_final are initialized to 0.0, and your calculation for acceleration is carried out as (0.0 - 0.0)/1, or 0.0, which is the value returned by this function.

One way around this is to use global variables (variables defined outside of main and outside all the other functions). I would not advise doing this. The preferred alternative is to use function parameters to pass the information to each function that it needs to produce its output.
Code:
int main( int argc, char *argv[] )
{
float acceleration = 0.f;
int coastTime = 0.f;
float distance = 0.f;
float v_final = 0.f;
float v_initial = 0.f;
float v_average = 0.f;
.
.
.
cout << "Please enter the initial speed in kph with decimal:" << endl;
cin >> v_initial;
cout << "Please enter the speed after 1 minute in kph with a decimal" << endl;
cin >> v_final;
.
.
.
}

float deceleration()
{
float acceleration = 0.f;
float v_initial = 0.f;
float v_final = 0.f;
int time = 1;

acceleration = ( v_final - v_initial ) / time;

return acceleration;
}
 
  • #10
Thanks.

Here's my current code:

* main --
*
* @params: argc - the variable that contains the number of parameters (argument count) added to the command line when the program was run.
* argv - the value of the command line arguments as array of character pointers
* @descript: main entry point for program, handle inputs and outputs.
*
* @returns: 0 for execution success.
***************************************************************************************************************************************************/

int main( int argc, char *argv[] )
{
float acceleration = 0.f;
int coastTime = 0.f;
float distance = 0.f;
float v_final = 0.f;
float v_initial = 0.f;
float v_average = 0.f;

outDescription ();

cout << "Please enter the initial speed in kph with decimal:" << endl;
cin >> v_initial;
cout << "Please enter the speed after 1 minute in kph with a decimal" << endl;
cin >> v_final;

acceleration = deceleration(v_initial, v_final);

cout << "Mark's acceleration rate is" << acceleration ;

coastTime = time( acceleration, v_average, v_initial, v_final);

cout << "\nMark's total coast time is:" << coastTime;

outResults2( coastTime );

distance = calculatedDistance ( coastTime, v_average);

cout << "\nThe distance that Mark reached after coasting is : " << distance;

return 0;
}

/****************************************************************************************************************************************************
* outDescription - outputs the description of the program
*
* @params:
* @modifies: cout - outputs the description
*
* @return: none
*****************************************************************************************************************************************************/

void outDescription()
{
cout << "This program plays Marks crazy prairie cycling game\n";
cout << "and extrapolates deceleration, time and distance given 2 speeds 1 minute apart." << endl;
}

/****************************************************************************************************************************************************
deceleration - the acceleration rate of Mark

@params: none
@modifies: cin
cout
-Prompts the user and inputs the following:
Initial speed of in kph
Speed after 1 minute in kph
@pre: none

@returns: acceleration rate of Mark
*****************************************************************************************************************************************************/

float deceleration(float v_initial, float v_final)
{
float acceleration = 0.f;
int time = 1;

acceleration = ( v_final - v_initial ) / time;

return acceleration;
}

/*****************************************************************************************************************************************************
@params: time - time spent coasting
v_average - average speed of Mark
deceleration - acceleration rate of Mark
@modifies: nothing
@pre: none

@returns: time
******************************************************************************************************************************************************/

int time( float deceleration, float v_average, float v_initial, float v_final)
{
int coastTime = 0.f;

coastTime = ( v_final - v_initial ) / deceleration;

return coastTime ;
}

/******************************************************************************************************************************************************
* outResults2 - outputs the time spent for coasting
*
* @params: time--time spent for coasting (minutes)
* seconds--time spent for coasting ( and seconds )
* @modifies: cout -- Outputs the results
*
* @pre: none
* @returns: nothing
*****************************************************************************************************************************************************/

void outResults2 ( int time )
{
cout << time%60 << "minutes and " << (time - (time%60)*60 )<< "seconds";
}

/*****************************************************************************************************************************************************
@params: v_average - average speed
time - time spent coasting
@modifies: nothing
@returns: distance
*****************************************************************************************************************************************************/

float calculatedDistance ( int time, float v_average)
{
float distance = 0.f;
distance = v_average*time;

return distance;
}








For some reason...my acceleration and distance is 0...and my code doesn't calculate the time coasted...So I was wondering if void outResults2 ( int time ) should be void outResults2 ( int coastTime) and IF IT SHOULD be changed, float calculatedDistance ( int time ...) should be float calculatedDistance ( int coastTime ...) ?. And if my declerations of v_initial and v_final should be with without "0.f"?


Thanks again
 
  • #11
Can you capture a screen shot or text file that shows your input and the program's output? Your deceleration function looks fine, but since I don't know what you're entering I can't predict what the output should be.
 
  • #12
Sorry I don't have a screenshot because I only use Eclipse in the Computer Lab.

The outputs are supposed to be:
This program plays Marks crazy prairie cycling game and extrapolates deceleration, time and distance given 2 speeds 1 minute apart.


Initial speed in kph: 34.7

Speed after 1 minute in kph: 24.3


The deceleration rate is 0.0481482 m/s^2

The coasting time is 3 minutes and 20 seconds

The distance coasted is 964 meters



But mine says something like:
This program plays Marks crazy prairie cycling game and extrapolates deceleration, time and distance given 2 speeds 1 minute apart.


Initial speed in kph: 34.7

Speed after 1 minute in kph: 24.3


The deceleration rate is 0

The coasting time is -0.023264346466

The distance coasted is 0
 
  • #13
Your latest version doesn't show the function prototypes that you had in your first version. If you are calling the functions before (above) the function definitions and you don't have function prototypes, that's a problem.
 
  • #14
Sorry, I didn't copy paste my latest version completely in my post.
 
  • #15
Does your code have the function prototypes above main? I'm trying to figure out why your deceleration function is returning a bad value, and this is the only thing I can think of.
 
  • #16
Yeap, it does have the function prototypes.

This should be the missing prototypes that I didn't include in my post:
#include <iostream>
#include <cmath>
using namespace std;

void outDescription ();
float deceleration (float acceleration, int time);
int time ( float deceleration, float v_average, float v_initial, float v_final );
void outResults2(int time);
float calculatedDistance ( int time, float v_average );

/************************************************** ************************************************** ***********************************************
* main --
*
* @params: argc - the variable that contains the number of parameters (argument count) added to the command line when the program was run.
* argv - the value of the command line arguments as array of character pointers
* @descript: main entry point for program, handle inputs and outputs.
*
* @returns: 0 for execution success.
************************************************** ************************************************** ***********************************************/

int main( int argc, char *argv[] )
{
float acceleration = 0.f;
int coastTime = 0.f;
float distance = 0.f;
float v_final = 0.f;
float v_initial = 0.f;
float v_average = 0.f;
 
  • #17
Well, they don't agree, so that's a definite problem.

From your last post, the prototype of the deceleration function.
Code:
float deceleration (float acceleration, int time);
From post 10, the definition of this function.
Code:
float deceleration(float v_initial, float v_final)
{
float acceleration = 0.f;
int time = 1;

acceleration = ( v_final - v_initial ) / time;

return acceleration;
}

The signatures have to be identical, and they aren't. In the prototype, the function takes two parameters, a float and an int. In the function definition header, the parameters are both float. I would make both parameters float in the prototype.

Make sure that each of your function prototypes agrees with its corresponding function definition - same return type, same number of arguments, and each argument of one is the same as the corresponding argument in the other.

The parameter names in the prototype don't have to agree with the parameter names in the function definition. In fact, the parameter names in the prototype are optional, so you can leave them out.
 
  • #18
Thank you very much Mark44. Hopefully, I should have the correct outputs when I work it out in the computer lab tomorrow. Thanks again
 
  • #20
Mark44 said:
Did you ever get this to run correctly?

Yes it did. Thanks
 

Related to Why Is My C++ Program Not Running as Expected?

What is C++ input/output assignment?

C++ input/output assignment refers to the process of receiving data (input) and displaying data (output) in a C++ program. This allows the user to interact with the program and provide input, as well as view the results of the program's execution.

How do I receive input in a C++ program?

In C++, input can be received using the cin statement. This statement prompts the user to enter data, which is then stored in a variable. For example, cin >> x; will prompt the user to enter a value, which will be stored in the variable x.

How do I display output in a C++ program?

In C++, output can be displayed using the cout statement. This statement allows you to print values or text to the screen. For example, cout << "Hello World!"; will display the text "Hello World!" on the screen.

What is the difference between input and output in a C++ program?

Input refers to the data that is provided by the user, while output refers to the data that is displayed by the program. In C++, input is received using the cin statement and output is displayed using the cout statement.

How can I handle errors in input for my C++ program?

To handle errors in input, you can use the fail() function. This function checks if the input was successful and if not, allows you to handle the error accordingly. You can also use getline() to read a line of input and istringstream to convert the input into the desired data type.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
34
Views
10K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
27K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top