Please help with do while loop

Hello, I am supposed to create a program that converts time in seconds into minutes, hours, days, weeks, months, years, decades, century, and millennia. I have already emailed my prof and don't expect to get a response (because he usually takes at least a week to respond, if he does at all) and I've already visited the tutor center and they could not help me either, they didn't know how to fix it :\ so I hope someone here could help...
Basically here is what is happening is that everything works okay until seconds need to be converted to centuries, then it either outputs nothing (if seconds ==> century is the first user input) or it loops infinitely (if seconds ==> century is input after an amount of seconds has already been converted).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
void testexercise2(){

	long millenia = 0;
	long centuries = 0;
	long decades = 0;
	long years = 0;
	double months = 0;
	double weeks = 0;
	long days = 0;
	long hours = 0;
	long minutes = 0;
	long seconds = 0;
	char answer;

	do{

        //reinitialize variables
        millenia = 0;
	centuries = 0;
	decades = 0;
	years = 0;
	months = 0;
	weeks = 0;
	days = 0;
	hours = 0;
	minutes = 0;
	seconds = 0;
	char answer;

		std::cout << "Enter an amount of seconds: ";
		std::cin >> seconds;

		if (seconds >= 60) { minutes = seconds  / 60; seconds %= 60; }
		if (minutes >= 60) { hours = minutes    / 60; minutes %= 60; }
		if (hours >= 24)   { days = hours       / 24; hours   %= 24; }
		if (days >= 7)	   { weeks = days       / 7;  days    %= 7;  }
		if (weeks >= 4.34812)      {
			months = weeks / 4.34812;
			weeks -= months * 4.34812;
		}
		months = floor(10.0 * months + 0.5) / 10.0; // round to nearest tenth
		if (months >= 12)    {   years = months          / 12;	months    -= (years * 12); }
		if (years >= 10)     {   decades = years         / 10;  years     %= 10;           }
		if (decades >= 10)   {   centuries = decades     / 10;  decades   %= 10;           }
		if (centuries >= 10) {   millenia = centuries    / 10;  centuries %= 10;           }

		if (millenia > 1){
			std::cout << millenia << " millenium " << centuries << " centuries " << decades << " decades " << years << " years " << months
			<< " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (millenia == 1 && millenia < 2 ){
			std::cout << millenia << "millenia" << centuries << " centuries " << decades << " decades " << years << " years " << months
			<< " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (centuries > 1 && millenia < 1){
			std::cout << centuries << " centuries " << decades << " decades " << years << " years " << months
			<< " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (centuries == 1 && millenia < 1){
			std::cout << centuries << " century " << decades << " decades " << years << " years " << months
			<< " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (decades > 1 && centuries < 1){
			std::cout << decades << " decades " << years << " years " << months
			<< " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (decades == 1 && centuries < 1 ){
			std::cout << decades << " decade " << years << " years " << months
			<< " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (years > 1 && decades < 1 && centuries < 1){
			std::cout << years << " years " << months << " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (years == 1 && decades < 1 && centuries < 1){
			std::cout << years << " year " << months << " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (months > 1 && years < 1 && decades < 1){
			std::cout << months << " months " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (months == 1 && years < 1 && decades < 1){
			std::cout << months << " month " << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (weeks > 1 && months < 1 && years < 1){
			std::cout << weeks << " weeks " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (weeks == 1 && months < 1 && years < 1){
			std::cout << weeks << " week " << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (days > 1 && weeks < 1 && months < 1 ){
			std::cout << days << " days " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (days == 1 && weeks != 0 && months < 1){
			std::cout << days << " day " << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (hours > 1 && days < 1 && weeks < 1){
			std::cout << hours << " hours " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (hours == 1 && days < 1 && weeks < 1){
			std::cout << hours << " hour " << minutes << " minutes " << seconds << " seconds\n";
		}
		if (minutes > 1 && hours < 1 && days < 1){
			std::cout << minutes << " minutes " << seconds << " seconds\n";
		}
		if (minutes == 1 && hours < 1 && days < 1){
			std::cout << minutes << " minute " << seconds << " seconds\n";
		}
		if (seconds > 1 && minutes < 1 && hours < 1){
			std::cout << seconds << " seconds\n";
		}
		if (seconds == 1 && minutes < 1 && hours < 1){
			std::cout << seconds << " second\n";
		}
		
		std::cout << std::endl;
		std::cout << "Enter another? (y/n)";
		std::cin >> answer;
		
	} while (answer == 'y' || answer == 'Y');
	
	
}
I think your code may be unnecessarily complicated. Why not try something like this, instead?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const int seconds_per_minute = 60;
const int minutes_per_hour = 60;
const int hours_per_day = 24;

int seconds = 0;
int minutes = 0;
int hours = 0;
int days = 0;

cout << "Enter number of seconds to be converted: ";
cin >> seconds;

while (seconds >= 60) //once you are under 60 seconds, no longer need to convert
   {
      seconds = seconds - seconds_per_minute; //subtracts number of seconds in a minute off 
      minutes = (minutes + 1)%minutes_per_hour; //adds another minute until it reaches the number of minutes in an hour, then resets to zero
      if( minutes == 0 ) //minutes re-rolled, add an hour
         {
            hours = (hours + 1)%hours_per_day;
            if( hours == 0 ) //hours re-rolled, add a day
               {
                  //and so on and so forth...
               }
         }
   }


EDIT: Actually I was thinking about it, and while something like this would work it's way slower than needs to be for this program. I would just do something with modular arithmetic, starting by converting seconds into millennia, subtracting off the number of millenia, move onto years, and do the same thing.
Last edited on
Is a long 32 bits or 64 on your system? To find out add
cout << "long is " << sizeof(long) << " bytes\n";
to your output. If it's 32 bits then you are simply overflowing the size of a long. a signed 32 bit number can only hold about 72 years worth of seconds.

Incidentally, this is why I plan to come out of retirement and make gobs of money in the 2030's leading up to the 2034(?) bug when 32-bit C and C++ programs will overflow the time_t value. If you thought Y2K was bad, just wait :).
Thank you!!! I changed it to long long now it seems to be working :D
Topic archived. No new replies allowed.