Glitch in Total Distance Ran Program

i am a beginner to C++ , jus started taking a course on it. I was trying to create a program where you can enter strides in first minute of running, strides in last minute of running, and time ran ( hours, minutes) and using an average number of strides per minute the program gives you your total distance run. Using 50 stides in first minute, 70 stides in last minute , and 1hour 30 minutes for time, the program outputs 60 minutes for my average stride rate but 3.1145e-007 for total distance ran. I cant find the problem, any help would be greatly appreciated. here is the code i am using

# include <iostream>
using namespace std;

double average ( double s1, double s2);
double totalMinutes ( double h, double m);

int main()
{
double s1, s2;
double totalStrides, totalMinutes, totalDistance, avgStrides;
double const strideLength = 0.00065;
avgStrides= average( s1, s2);
totalStrides= avgStrides * totalMinutes;
totalDistance= totalStrides* strideLength;
cout<< " # of Strides in first minute?";
cin>>s1;
cout<< " # of strides in last minute?";
cin>> s2;
double h;
double m;
cout<< " Enter time as hours and minutes seperated by a space.";
cin>>h>>m ;
cout<< " Estimated average stride rate (strides per minute)"
<< average(s1,s2)<< endl;
cout<< " Total distance jogged(km):"
<< totalDistance << endl;
}

double average(double s1, double s2)
{
return (s1 + s2)/2.0;

}

double totalMinutes ( double h, double m)
{
return (h*60.0)+m;
}
Last edited on
you've given no values to s1, s2 totalStrides etc before using them in functions and calculations. 3.1145e-007 means 3.1145 to the power of -7, and evaluates to roughly zero
thanks for the advice buthow do i go about fixing this? if i have to rearrange everything what order should it go in?
i tried this rearrangement but did not work either so im not sure if that is what you mean.

# include <iostream>
using namespace std;

double average ( double s1, double s2);
double totalMinutes ( double h, double m);

int main()
{
double s1, s2;
double totalStrides, totalMinutes, totalDistance, avgStrides;
double const strideLength = 0.00065;
cout<< " # of Strides in first minute?";
cin>>s1;
cout<< " # of strides in last minute?";
cin>> s2;
double h;
double m;
cout<< " Enter time as hours and minutes seperated by a space.";
cin>>h>>m ;
avgStrides= average( s1, s2);
totalStrides= avgStrides * totalMinutes;
totalDistance= totalStrides* strideLength;
cout<< " Estimated average stride rate (strides per minute)"
<< average(s1,s2)<< endl;
cout<< " Total distance jogged(km):"
<< totalDistance << endl;
}

double average(double s1, double s2)
{
return (s1 + s2)/2.0;

}

double totalMinutes ( double h, double m)
{
return (h*60.0)+m;
}

closed account (D80DSL3A)
I think you almost got it there! One variable (totalMinutes) is still uninitialized though. You are forgetting to call your function totalMinutes() to find totalMinutes before calculating totalStrides.

It may be legal to use the same name for the variable totalMinutes and the function totalMinutes(), but I think it's asking for trouble (or at least confusion). I'd recommend renaming one of them.
thanks for the help, it got it working. i changed the name of the function as you suggested but i think the problem was as you said, i was forgetting to call the function .
Topic archived. No new replies allowed.