954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MPH to Minutes and Seconds to run a mile

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!

nuubee
Newbie Poster
7 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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...?!

nuubee
Newbie Poster
7 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

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?

nuubee
Newbie Poster
7 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You