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.

3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Ancient Dragon

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.