| | |
MPH to Minutes and Seconds to run a mile
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 7
Reputation:
Solved Threads: 0
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:
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!
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:
C++ Syntax (Toggle Plain Text)
#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; }
Thanks in advance!
Last edited by Ancient Dragon; Oct 12th, 2008 at 7:57 am. Reason: add code tags
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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
Chris
Knowledge is power -- But experience is everything
![]() |
Other Threads in the C++ Forum
- Previous Thread: Bubble sorting and finding a location of a number in array
- Next Thread: C++ Powerball program?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






