By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,326 Members | 2,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,326 IT Pros & Developers. It's quick & easy.

I'm trying to write a code that will ask the user to input seconds hours:minutes:sec

P: 3
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int seconds; 
  9.     int minutes; 
  10.     int hours;
  11.     int secs;
  12.     seconds = 0;
  13.     secs = 0;
  14.     minutes = 0;
  15.     hours = 0;
  16.  
  17.  cout <<"Please input your total time in seconds " ;
  18.  cin >> seconds;
  19.  
  20.  hours = seconds % 3600;
  21.  minutes = seconds % 60;
  22.  seconds = secs - minutes * 60;
  23.  secs = hours * 3600;
  24.  cout << "Your time is:   ";
  25.  cout << hours +  minutes + secs;
Sep 8 '10 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
Don't just post code explain your problem too.

Line 21 uses secs which is not initialised until line 22.

The whole thing can be done simply without the secs variable using the / and % operators. See if you can work out how.

Start by concentrating on getting the hours out correctly, when you have done that move onto the minutes and then the seconds.
Sep 8 '10 #2

P: 3
My problem is getting it to output hours minutes and seconds with a : in between and also, the output isn't correct. Thanks
Sep 8 '10 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Expand|Select|Wrap|Line Numbers
  1. cout << hours +  minutes + secs; 
This makes no sense. Adding hours, minutes and seconds has no meaning.

Did you mean to display hours : minutes :: seconds?

If so, then just do so.
Sep 8 '10 #4

P: 3
It seems to be working correctly...Thank you for the help




Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. int seconds; 
  9. int minutes; 
  10. int hours;
  11.  
  12. seconds = 0;
  13.  
  14. minutes = 0;
  15. hours = 0;
  16. cout <<"Please input your total time in seconds " ;
  17. cin >> seconds;
  18. cout<<"\n";
  19. hours = seconds / 3600;
  20. minutes = seconds / 60;
  21. seconds = hours / 3600;
  22.  
  23.  
  24. cout <<"(Hours:Minutes:Seconds)\n";
  25. cout <<hours<<":";
  26. cout << minutes<<":";
  27. cout <<seconds<< "\n";
  28.  
  29.     cout << endl;
  30.  
  31. }
Sep 9 '10 #5

100+
P: 542
it seems to be working correctly...
Thats surprising because you have an expression
Expand|Select|Wrap|Line Numbers
  1. seconds = hours / 3600; 
Also it would be helpful if you used code tags as above.
Sep 9 '10 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
Expand|Select|Wrap|Line Numbers
  1. seconds = hours / 3600; 
?

Don't you mean:

Expand|Select|Wrap|Line Numbers
  1. seconds = hours * 3600; 
?
Sep 9 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.