Need More Help with Code Problem

Hi, I tried working on the code, but I don't know what to do next even after reading the chapter from the textbook. Could someone please help me out? I don't no what to put for the brackets of the return functions where I put a ? mark. I don't no if I'm doing it right. Thanks.

The code problem is the following which I also posted in a earlier post to get help and to get started:

You would like to know how fast you can run in miles per hour, however your treadmill tells you your speed in terms of pace (minutes and seconds per mile, such as “5:30 mile”) or in terms of kilometers per hour (kph). Write an overloaded function called convertToMPH. The first definition should take as input two integers that represent the pace in minutes and seconds per mile and return the speed in mph as a double. The second definition should take as input one double that represents the speed in kph and return the speed in mph as a double. One mile is approx. 1.61 kilometers. [Your submitted code for this question should include only the two definitions of the overloaded function, nothing else.]

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
34
35
36
37
38
39
40
#include <iostream>
using namespace std;
//#include <cstdlib>

double convertToMPH(int paceInMin, int paceInSec);
//represents pace in minutes and seconds per mile and returns the speed in mph 
double convertToMPH(double kph);
//represents in kph and returns the speed in mph

//1 mile = 1.61 km
//1 km = 1000m
//d=vt, v=d/v

int main()
{
	int paceInMin;
	int paceInSec;
	double kph;
	
	cout << "Please enter your pace in minutes: ";
	cin >> paceInMin;
	cout << "Please enter your pace in seconds: ";
	cin >> paceInSec;
	cout << "Your speed in mph is " << convertToMPH(paceInMin, paceInSec) << endl;

	cout << "Please enter your speed in kph: ";
	cin >> kph;
	cout << "Your speed in mph is " << convertToMPH(kph);

	return 0;
}

double convertToMPH(int paceInMin, int paceInSec)
{
	return(60 / (paceInMin + paceInSec / 60.));
}
double convertToMPH(double kph)
{
	return (kph / 1.61);
}
Last edited on
Your code seems to be fine. I see no errors. It compiles, it runs.
I prefer to pass a variable to the function instead of streamlining the calls. It makes it easier to play with once the value has been returned. Like so...

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;

double convertToMPH(int paceInMin, int paceInSec);
//represents pace in minutes and seconds per mile and returns the speed in mph
double convertToMPH(double kph);
//represents in kph and returns the speed in mph

//1 mile = 1.61 km
//1 km = 1000m
//d=vt, v=d/v

int main()
{
	int paceInMin;
	int paceInSec;
	double kph;
	double total;            //  *************   Variable added   *************

	cout << "Please enter an integer to represent your pace in minutes: ";
	cin >> paceInMin;
	cout << "Please enter an integer to represent your pace in seconds: ";
	cin >> paceInSec;

       //  ************  Pass total to the function   ************

	total = convertToMPH(paceInMin, paceInSec);

        // *********  Now you can manipulate the variable any way you'd like...   ************

	cout << "Your speed is " << total << " miles per hour." << endl;
	if (total < 4)
    {
        cout << "Really? "<< total << " miles per hour? \nYou must be the six million dollar man!"<< endl;
    }
    else
    {
        if (total >10) {
            cout << "Come on! My grand mother can run faster than " << total << " miles in an hour." << endl;
        }
        else
        {
            cout << "Nice pace!!" << endl;
        }

    }

        //  *************  continue with program here  ***************

	cout << "\nPlease enter your speed in kph: ";
	cin >> kph;
	total = convertToMPH(kph);
	cout << "Your speed in mph is " << total << endl;
	return 0;
}

double convertToMPH(int paceInMin, int paceInSec)
{
    return(60 / (paceInMin + paceInSec / 60.));
};
double convertToMPH(double kph)
{
	return (kph / 1.61);
};
Last edited on
Topic archived. No new replies allowed.