Click to See Complete Forum and Search --> : C++ hours.cpp program help


DJKlacka
04-13-2003, 06:45 PM
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.


#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.............

Malone
04-13-2003, 09:48 PM
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.

DJKlacka
04-13-2003, 10:11 PM
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

Galen of Edgewood
04-13-2003, 10:56 PM
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?