473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
6 2080
Banfa
9,065 Expert Mod 8TB
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
Candy6
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
9,208 Expert Mod 8TB
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
Candy6
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
whodgson
542 512MB
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
9,208 Expert Mod 8TB
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

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

Similar topics

4
by: Nomen Nescio | last post by:
can anyone be so kind as to look at http://www.mysolution.ws/HYPOCRITE.php and let me know why it isn't passing the form data to http://www.mysolution.ws/insertHYPOCRITES.php for the most...
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
2
by: Deepa | last post by:
I need a routine to convert milliseconds to a traditional dd:hh:mm:ss or four separate variables that I can display. I am not wanting to display a date but rather the interval between two...
2
by: Rudi Groenewald | last post by:
Hi all. If I've got a query which has a field with seconds in it... how will I use the Convert function to get my field converted to the format: HH:MM:SS ? The field with the seconds in is...
16
by: graham.reeds | last post by:
I am updating a website that uses a countdown script embedded on the page. When the page is served the var's are set to how long the countdown has left in minutes and seconds, but the rest of the...
7
by: Stu | last post by:
Is there a simple function call within "C" that I can use to convert number of seconds (keep in mind this may be a type longlong and has to work on UNIX and NT) into Days, months, Hours, Minutes...
5
by: kpp9c | last post by:
Hi, I was looking at python & datetime and hoping that it would already have a method/func to translate time formats. I need to translate seconds to hh:mm:ss.ms and vice versa and would like...
6
by: alexs | last post by:
hi, I'm moving a mysql database over to using db2 V9.5 The database is used by our radius server to store network accounting information from our switches. One parameter is the length of a...
3
by: =?Utf-8?B?U2hlbGRvbg==?= | last post by:
Hello - I have various times formatted like 00:00:00, so for example, 01:32:05 would be one thirty-two with five milliseconds. I need to convert this into seconds (an integer). First of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.