C++ hours.cpp program help

Sharky Forums


Results 1 to 4 of 4

Thread: C++ hours.cpp program help

  1. #1
    Sushi
    Join Date
    Apr 2003
    Posts
    4

    C++ hours.cpp program help

    I am supposed to write a program that is interactive and converts seconds to hours, minutes, and seconds. For an input of 7903 seconds, the output should be:

    7903 sec. = 2 hr. 11 min. 43 sec.

    This is what I have and it doesnt seem to be working very well.. if I type in any amount of seconds, it doesnt give me the leftover seconds it gives me a certain amount of hours that is wrong, then a godly isane amount of minutes, and then it gives me the same amount of seconds that the input is.

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
      int s, hours, minutes;
      while (s >= 3600)
        { s = s - 3600;
        hours++; }
      while (s >= 60)
        { s = s - 60;
        minutes++; }
    
      cin >> s;
      cout << hours << "hr." << minutes
           << "min." << s << "sec." << endl;
    
      return 0;
    }
    Please help me out.............

  2. #2
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Erie, PA, USA
    Posts
    693
    First of all, you need to have your while loops AFTER you get the input. The way you have it now, your program does nothing, then accepts input, then outputs the seconds (which were just input), the minutes and hours (which haven't been initialized and could be anything.
    AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
    Shuttle AN35N nForce2 Ultra 400
    2x512MB Kingston PC3200 (3-3-3)
    ATI Radeon 9600 Pro
    40GB WD ATA-100 8MB cache
    Creative 12X DVD Drive
    Memorex 52X CD-RW
    Running Windows XP Pro

  3. #3
    Sushi
    Join Date
    Apr 2003
    Posts
    4
    I know your not going to do my code for me, but is there any pointers or particular parts in my code you can point me towards and tell me how to go about changing it. thank you

    p.s.- I hate school

  4. #4
    Texan Dragon Moderator Galen of Edgewood's Avatar
    Join Date
    Sep 2000
    Location
    Ft Irwin, CA
    Posts
    5,602
    DJ, what Malone has told you is that you goofed. You need to get your cin statement (where you get your input) placed before your while loops. How are you going to run the thing that calculates how many hours, minutes, and seconds BEFORE you know what it's going to convert?
    Dragon of the OC Crusaders

    Break the rules and you're snack food for this dragon...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •