429,326 Members | 2,726 Online
+ 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 #include  #include    using namespace std;   int main() {     int seconds;      int minutes;      int hours;     int secs;     seconds = 0;     secs = 0;     minutes = 0;     hours = 0;    cout <<"Please input your total time in seconds " ;  cin >> seconds;    hours = seconds % 3600;  minutes = seconds % 60;  seconds = secs - minutes * 60;  secs = hours * 3600;  cout << "Your time is:   ";  cout << hours +  minutes + secs; Sep 8 '10 #1
6 Replies

 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

 Expert Mod 5K+ P: 9,197 Expand|Select|Wrap|Line Numbers 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 #include  #include  using namespace std;     int main() { int seconds;  int minutes;  int hours;   seconds = 0;   minutes = 0; hours = 0; cout <<"Please input your total time in seconds " ; cin >> seconds; cout<<"\n"; hours = seconds / 3600; minutes = seconds / 60; seconds = hours / 3600;     cout <<"(Hours:Minutes:Seconds)\n"; cout <

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

 Expert Mod 5K+ P: 9,197 Expand|Select|Wrap|Line Numbers seconds = hours / 3600;  ? Don't you mean: Expand|Select|Wrap|Line Numbers seconds = hours * 3600;  ? Sep 9 '10 #7