-
Calculating pace.
I'm trying to write a program to convert your pace in minutes and seconds to MPH. All the program is doing is taking in the input of minutes and seconds. What am I doing wrong?
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
double ConvertToMPH (int paceInMin, int paceInSec);
double ConvertToMPH (double kph);
int main()
{
double paceInMin;
double paceInSec;
cout << "Please enter your pace in minutes: ";
cin >> paceInMin;
cout << "Please enter your pace in seconds: ";
cin >> paceInSec;
return 0;
}
double ConvertToMPH(int paceInMin, int paceInSec)
{
return 60/(paceInMin + paceInSec/60.);
}
double ConvertToMPH(double kph)
{
return kph/1.61;
}
-
If you want either of those ConvertToMPH() functions to be called, then you need to write code that calls them. You have not done so. The compiler does not just magically call a function because you defined it.
-
Ah, right. I have never been good with functions. That's why I'm playing around with this for practice.
I tried this, it runs, but I get automatic garbage data :/
Code:
cout << "Please enter your pace in minutes: " << ConvertToMPH(paceInMin) << endl;
cin >> paceInMin;
cout << "Please enter your pace in seconds: " << ConvertToMPH(paceInSec) << endl;
cin >> paceInSec;
-
You have to read the input, then call your function.
Your instructions are executed by the computer sequentially.
-
I tried calling ConvertToMPH after each cin, and I got a lot of errors.
I also tried to call it after the cout, cin block. Ran, but it only took the input and exited out of teh program.
-
We can't guess what you've tried. Post some code.
-
Sorry man! Here is some code of things I have tried! :cool:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
double ConvertToMPH (int paceInMin, int paceInSec);
double ConvertToMPH (double kph);
int main()
{
double paceInMin;
double paceInSec;
ConvertToMPH (paceInMin, paceInSec);
cout << "Please enter your pace in minutes: ";
cin >> paceInMin;
cout << "Please enter your pace in seconds: ";
cin >> paceInSec;
return 0;
}
double ConvertToMPH(int paceInMin, int paceInSec)
{
return 60/(paceInMin + paceInSec/60.);
}
double ConvertToMPH(double kph)
{
return kph/1.61;
}
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
double ConvertToMPH (int paceInMin, int paceInSec);
double ConvertToMPH (double kph);
int main()
{
double paceInMin;
double paceInSec;
cout << "Please enter your pace in minutes: ";
cin >> paceInMin;
cout << "Please enter your pace in seconds: ";
cin >> paceInSec;
ConvertToMPH (paceInMin, paceInSec);
return 0;
}
double ConvertToMPH(int paceInMin, int paceInSec)
{
return 60/(paceInMin + paceInSec/60.);
}
double ConvertToMPH(double kph)
{
return kph/1.61;
}
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
double ConvertToMPH (int paceInMin, int paceInSec);
double ConvertToMPH (double kph);
int main()
{
double paceInMin;
double paceInSec;
cout << "Please enter your pace in minutes: ";
cin >> paceInMin >> ConvertToMPH (paceInMin);
cout << "Please enter your pace in seconds: ";
cin >> paceInSec >> ConvertToMPH (paceInSec);
return 0;
}
double ConvertToMPH(int paceInMin, int paceInSec)
{
return 60/(paceInMin + paceInSec/60.);
}
double ConvertToMPH(double kph)
{
return kph/1.61;
}
-
Stop trying random things...
What is this? is it an assignment? are you trying to learn C++ by yourself? what is the context here?
ATTEMPT#1
Same remark than earlier, you need to get the input first, then do something with it.
ATTEMPT#2
That one is better, you have your input, your calculate the result but... you do nothing with it. Use cout to print it (for instance).
ATTEMPT#3
Is this even compiling?
-
I am taking a C++ class, but I pulled this out of the book to try for some practice.
Here is the actual question:
You would like to know how fast you can run in Miles Per Hour. Your treadmill will output your speed in terms of a pace (Minutes and Seconds per mile, e.g. a "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 approximately 1.61 kilometers. Write a test/driver program to test your function.
-
Ok. I've edited my previous post to add remarks.
Remember a program is like a recipe, you prepare the ingredients first then you mix them, not the opposite.
Try to review I/O to understand why your 3rd attempt is wrong.
-
I figured it out! It works correctly now! Thanks for the nudges, fellas! Much appreciated!!
:D
-
root4, attempt 3 did compile but I got garbage data.
-
Here is my working code:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
double ConvertToMPH (int paceInMin, int paceInSec);
double ConvertToMPH (double kph);
int main()
{
double paceInMin;
double paceInSec;
double kph;
cout << "Please enter your pace in minutes: ";
cin >> paceInMin;
cout << "Please enter your pace in seconds: ";
cin >> paceInSec;
cout << "Your pace in MPH is: " << ConvertToMPH (paceInMin, paceInSec);
cout << "Please enter your speed in KPH: ";
cin >> kph;
cout << "Your KPH pace 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;
}
Thanks again! :)
-