Hi all,

Ok, I've just been trying to write a program that will convert MPH (miles per hour) into the number of minutes and seconds it takes that person to run one mile. This is what I have:

#include <iostream>
using namespace std;

int main()
{

  double  mph, count = 1;
  int mpm, seconds;

  cout << "\n";
  cout << "Program that converts MPH into minutes and seconds per mile." << endl;

  cout << "Please enter the MPH at which you were running: ";
  cin >> mph;
  cout << endl;

  mpm = mph / 60;

  do{
    mpm = mpm * 2;
    count = count + 1;
  }
  while (mpm < 1);

  seconds = mpm % 60;

  cout << "You are running 1 mile in " << count << " minutes and " << seconds << " seconds.";
  cout << endl;

  return 0;

}

The compiler doesnt like the '%' in my code. Is that because it has to be an integer value to follow it? Also not too sure if i've opted for the simplest method to solve the problem.

Thanks in advance!

Dani AI

Generated

You're almost there, . Two separate issues caused the behavior you saw: integer truncation and use of the modulus operator on a floating value. correctly pointed out that % only works for integer types, and correctly explained why your loop can hang when integer division makes a value zero and it never changes.

A much simpler, robust approach is to compute time-per-mile directly. One hour = 3600 seconds, so time for one mile (in seconds) = 3600.0 / mph. Do that with double arithmetic, round to an integer number of seconds, then split into minutes and remaining seconds.

#include <iostream>
#include <cmath>

int main() {
    double mph;
    std::cout << "Enter speed (mph): ";
    if (!(std::cin >> mph) || mph <= 0.0) {
        std::cerr << "Invalid input (mph must be > 0)\n";
        return 1;
    }
    double total_seconds = 3600.0 / mph;
    long secs = std::lround(total_seconds); // round to nearest second
    long minutes = secs / 60;
    secs = secs % 60;
    std::cout << "1 mile in " << minutes << " minutes and " << secs << " seconds\n";
    return 0;
}

Notes and troubleshooting: use a floating literal (3600.0) or cast to double so you don't trigger integer division; check for mph <= 0 to avoid divide-by-zero; use std::fmod only if you really need a floating remainder — here converting the rounded total seconds to integers then using % is simpler and avoids precision surprises. If you prefer truncation instead of rounding, cast with static_cast<long>(total_seconds) instead of std::lround.

Recommended Answers

All 5 Replies

The mod operator doesn't work on doubles -- only integers. And why is mph a double? Your program isn't even using the fractional parts so you might as well make it an integer and that will solve the problem for you.

Hi, thanks for the quick response. I have changed MPH to an int, only now, when I run the program, it doesn't run any further than prompting the user for MPH at the beginning...?!

Wrong it does, pick a number >= 60 it will work fine. mpm is an integer, integer's ignore and decimal places, so and number small that 60 when divided by 60 would make mpm equal to 0, thus 0*2 = 0, so the program continues to loop multiplying 0 by 2 until it reaches a number greater than one, which it won't.

Chris

Ah, that would explain it! If I were to make MPM a double though, then the '%' part would not work later in the program..?

So I would have to type cast it in some way?

use fmod() function for doubles. Also do division with doubles, not integers or you will have problems with that too.

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.