Need help with function.

Sep 26, 2019 at 5:28am
I need help creating with the conversions. Here’s the question?
A function called eta( ) which takes a distance in miles and a speed in MPH and returns a string that displays the hours and minutes until arrival. So a function call to eta(80,55) should return the string "1 Hour 27 Minutes".
Sep 26, 2019 at 8:20am
Do you know how to calculate the amount of time it takes to go 80 miles if you're travelling at 55 MPH?
Sep 26, 2019 at 8:28am
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>
std::string eta(const int distance, const int speed){
      int hour{}, minutes{};
    // get hour and minutes from distance and speed
    return std::to_string(hour) + " Hour " + std::to_string(minutes) +" minutes.";
}

int main(){
    std::cout << eta(80, 55) << std::endl;
    
    return 0;
}


Hope that helps.
Last edited on Sep 26, 2019 at 8:36am
Sep 26, 2019 at 5:37pm
here is what i currently have. minutes are not working though.


string mph(int distance, int speed)
{
int milesPerhours = (distance/ speed);
int temp = ((distance/ speed ) * 60);
int minutes = (temp - (milesPerhours / 60));
string eta = to_string(milesPerhours) + " hours " + to_string(minutes) + " minutes ";
return eta;
}

Sep 26, 2019 at 5:50pm
What does "not working" mean? Give us a clear and precise description of what your problem is, to make it easier for us to help you.

You're doing lots of integer division there, which means you're likely to get some unexpected results. I'd recomment using floats rather than ints for your numerical values.
Sep 26, 2019 at 6:12pm
here is the full thing. also i mean that my minutes are not correct. i am getting the wrong time. any help would be appreciated.

http://cpp.sh/7d4lm

Sep 26, 2019 at 6:19pm
If you'd read all the way to the end of my previous post, you'd already have some of the help you're looking for.
Sep 27, 2019 at 2:44am
haha. thanks Mikeyboy your right. had to change the parameters to floats.
Sep 27, 2019 at 11:05am
You're welcome.
Topic archived. No new replies allowed.