I'm working on a C++ program to convert MPH to minutes and seconds.

I'm stuck.

This is my code so far.

It runs, but when I key a value (i.e. 6.5), the program crashes and stalls.

What I am overlooking?

Recommended Answers

All 12 Replies

Here is the code.



#include <iostream>
using namespace std;

int main()
{
    int miles_per_hour, mins, secs, M, Y;
    {
        cout << "What is the miles per hour? ";
        cin >> miles_per_hour;
    }
        M = miles_per_hour;
        Y = (1 / 60) * M;
        mins = (int)(1 / Y);
        secs = (int)((1 / Y) - M * 60);
    {
        cout << "The pace is " << mins << "minutes" << "and" << secs << "seconds";
    }
    return 0;
}

Welp. An int can only hold integers. So, can you guess what happens when Y = (1 / 60) * M runs? What about lines 17 and 18.

Use doubles instead, and see what happens.

I apologize. I'm new to coding.

I changed it to double mins, sec, M, Y

When I debug, it appears to run asks me to input (I key 6.5), but after that it ends with no output.

Asides from using double, how does the rest of the code look?

Am I missing some variable regarding the double & divison?

Don't apologize. Making mistakes is not against any rule, and we've all been there before.

It would be nice to see your new code. Here's a few things to remember though. Remover all of your (int) casts. Also, remember that 1 / 60 = 0 in c++. You might want to try out (1.0 / 60), and see what happens.

Take a look at your math too. It is impossible to convert MPH to minutes and seconds. MPH is distance/time, where as minutes and seconds are just time.

Given a speed and a distance you can calculate time though.

I tried the following.

int main()
Inline Code Example Here

{


double miles_per_hour, mins, secs, M, Y;
{
    cout << "What is the miles per hour? ";
    cin >> miles_per_hour;
}   
    M = miles_per_hour;
    Y = (1.0 / 60) * M;
    mins = (double)(1 / Y);
    secs = (double)((1 / Y) - M * 60);
{
    cout << "The pace is " << mins << "minutes" << "and" << secs << "seconds";
}
return 0;

}

The output looks weird. I'm still missing/not thinking on the formula?

I'm new to C++ and taking a course online which I'm regretting now. I should be taking it in-class.

I'm working on the following.

Many treadmills output the speed of the treadmill in miles per hour (mph) on the console, but most runners think of speed in terms of a pace. A common pace is the number of minutes and seconds per mile instead of mph. Write a program that starts with a quantity in mph and converts the quantity into minutes and seconds per mile. As an example. The proper output for an input of 6.5 mph should be 9 minutes and 13.8 seconds per mile.

I hope this makes sense.

I'm using the math based on what I know and verified on a calculator, but what I am interpreting incorrectly in code?

Oh yeah. Another thing is that you have extra { } at lines 11, 14, 19 nd 21. It's not causing your crash, but it's not needed.

The output looks weird possibly because your cout is formatted weirdly. Try:

 cout << "The pace is " << mins << " minutes and " << secs << " seconds." << endl;

instead. Yes, your physics is flawed, as I have mentioned above.

If the MPH = 6.5, then I divide 60/6.5 that gives me 9.230769230769231.

I need to drop the decimals so I have 9 minutes.

Then for the secodns I need to subtract the minutes value (9.0 from 9.230769230769231) that gives me .230769230769231 that I multiply by 60 which gives me 13.84615384615385 that I round up to 13.8 seconds.

What can I do in the formula to achieve that output?

Ok, so the program determines how long it would take to travel 1 mile given MPH.

You have already figured out double minutes = 60 / mph; in your last post, assuming mph is a double. Good start.

One way to drop the decimal is to convert it back to an integer! For example, try cout << (int)(6.5) << endl; and see what comes out.

Your also right for calculating the seconds. I think you can work out the formula for yourself though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.