MPH to Minutes and Seconds to run a mile

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 7
Reputation: nuubee is an unknown quantity at this point 
Solved Threads: 0
nuubee nuubee is offline Offline
Newbie Poster

MPH to Minutes and Seconds to run a mile

 
0
  #1
Oct 12th, 2008
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:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. double mph, count = 1;
  8. int mpm, seconds;
  9.  
  10. cout << "\n";
  11. cout << "Program that converts MPH into minutes and seconds per mile." << endl;
  12.  
  13. cout << "Please enter the MPH at which you were running: ";
  14. cin >> mph;
  15. cout << endl;
  16.  
  17. mpm = mph / 60;
  18.  
  19. do{
  20. mpm = mpm * 2;
  21. count = count + 1;
  22. }
  23. while (mpm < 1);
  24.  
  25. seconds = mpm % 60;
  26.  
  27. cout << "You are running 1 mile in " << count << " minutes and " << seconds << " seconds.";
  28. cout << endl;
  29.  
  30. return 0;
  31.  
  32. }
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!
Last edited by Ancient Dragon; Oct 12th, 2008 at 7:57 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,396
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: MPH to Minutes and Seconds to run a mile

 
0
  #2
Oct 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: nuubee is an unknown quantity at this point 
Solved Threads: 0
nuubee nuubee is offline Offline
Newbie Poster

Re: MPH to Minutes and Seconds to run a mile

 
0
  #3
Oct 12th, 2008
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...?!
Last edited by nuubee; Oct 12th, 2008 at 8:13 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: MPH to Minutes and Seconds to run a mile

 
0
  #4
Oct 12th, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: nuubee is an unknown quantity at this point 
Solved Threads: 0
nuubee nuubee is offline Offline
Newbie Poster

Re: MPH to Minutes and Seconds to run a mile

 
0
  #5
Oct 12th, 2008
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?
Last edited by nuubee; Oct 12th, 2008 at 8:31 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,396
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: MPH to Minutes and Seconds to run a mile

 
0
  #6
Oct 12th, 2008
use fmod() function for doubles. Also do division with doubles, not integers or you will have problems with that too.
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC