Click to See Complete Forum and Search --> : URGENT Help Needed with Endless Loop Problem


Jody
April 27th, 1999, 04:41 AM
I need help with my code. It seems to be caught in an endless loop and I can't find the problem
Please Help!!



// program to calculate number of seconds from user entered date until Y2K

#include <stdio.h>

#include <string.h>



void Date();

void ComputeTime();



int day, month, year;

int hour, minute, second;

long hoursLeft, minutesLeft, secondsLeft;

long hoursPast, minutesPast, secondsPast;



int main()

{

while(1)

{

Date();

ComputeTime();

}

return(0);

}



void Date()

{

while (1)

{

printf ("Please enter a date using the following format: \n(hh:mm:ss:mm/dd/yyyy) \n\n");

scanf("%d/%d/%d/%d/%d/%d" ,&hour, &minute, &second, &month, &day, &year);

/* error trapping for month, day and year */

if(hour < 24 && hour >= 0 && minute < 60 && minute >= 0 && second < 60 && second >= 0 &&

(month < 13) && (month > 0) && day < 32 && day > 0 && year < 2100 && year > 1899)

break;

else

{

printf ("Data and/or format is incorrect!\n");

printf ("Please reenter the date using the following format: \n(hh:mm:ss:mm/dd/yyyy) \n\n");

printf ("Date entered = %d/%d/%d/%d/%d/%d\n", hour, minute, second, month, day, year);

}

}

}



void ComputeTime()

{

long yearDiff, days, totalSeconds;

yearDiff = 1999 - year;



if(year < 2000)

{

switch(month)

{

case 12:

days = 31 - day;

break;

case 11:

days = 31 + 30 - day;

break;

case 10:

days = 31 + 30 + 31 - day;

break;

case 9:

days = 31 + 30 + 31 + 30 - day;

break;

case 8:

days = 31 + 30 + 31 + 30 + 31 - day;

break;

case 7:

days = 31 + 30 + 31 + 30 + 31 + 31 - day;

break;

case 6:

days = 31 + 30 + 31 + 30 + 31 + 30 + 30 - day;

break;

case 5:

days = 31 + 30 + 31 + 30 + 31 + 30 + 30 + 31 - day;

break;

case 4:

days = 31 + 30 + 31 + 30 + 31 + 30 + 30 + 31 + 30 - day;

break;

case 3:

days = 31 + 30 + 31 + 30 + 31 + 30 + 30 + 31 + 30 + 31 - day;

break;

case 2:

days = 31 + 30 + 31 + 30 + 31 + 30 + 30 + 31 + 30 + 31 + 28 - day;

break;

case 1:

days = 31 + 30 + 31 + 30 + 31 + 30 + 30 + 31 + 30 + 31 + 28 + 31 - day;

break;

}

hoursLeft = (yearDiff * 8760) + (days * 24) + (23 - hour);

minutesLeft = (60 - minute) - 1;

secondsLeft = (60 - second) - 1;

totalSeconds = (hoursLeft * 3600) + (minutesLeft * 60) + secondsLeft;

printf("Hours,minutes,seconds remaining to New Years Day 2000 are: %ld,%ld,%ld\n", hoursLeft, minutesLeft, secondsLeft);

printf("The total # of seconds remaining to New Years Day 2000 are: %ld\n", totalSeconds);

}

else

{

yearDiff = year - 2000;



switch(month)

{

case 1:

days = day;

break;

case 2:

days = 31 + day;

break;

case 3:

days = 31 + 28 + day;

break;

case 4:

days = 31 + 28 + 31 + day;

break;

case 5:

days = 31 + 28 + 31 + 30 + day;

break;

case 6:

days = 31 + 28 + 31 + 30 + 31 + day;

break;

case 7:

days = 31 + 28 + 31 + 30 + 31 + 30 + day;

break;

case 8:

days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;

break;

case 9:

days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;

break;

case 10:

days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;

break;

case 11:

days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;

break;

case 12:

days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;

break;

}

hoursLeft = (yearDiff * 8760) + ((days-1) * 24) + (hour);

minutesLeft = (minute);

secondsLeft = (second);

totalSeconds = (hoursLeft * 3600) + (minutesLeft * 60) + secondsLeft;

printf("hours,minutes,seconds past New Years Day 2000 is: %ld,%ld,%ld\n", hoursLeft, minutesLeft, secondsLeft);

printf("The total # of seconds past New Years Day 2000 is: %ld\n", totalSeconds);

}

}

Lothar Haensler
April 27th, 1999, 04:49 AM
???
while(1) {...}
is the best way to write an endless loop.
it never ends, because 1 is always equal to 1 and the condition is always true.

Jody
April 27th, 1999, 04:55 AM
So, what can I use intead of while (1) ??

Franky Braem
April 27th, 1999, 05:18 AM
As long as the condition of your while is true (= 1) the loop will be executed.

solution :

1) Make a condition

eg.:

BOOL bLoop = TRUE;
while ( bLoop )
{
// Do your processing here
...
// Ask the user if he wants to end the program
// if he answers yes then set bLoop to FALSE
}



2) use break

Instead of using a BOOL variable use the break command when the user decides to end the program.

G Kirkham
April 27th, 1999, 09:42 AM
You can also use

for( ; ; )
{
//stuff
}

Andy St.Clair
April 27th, 1999, 12:49 PM
Hello Jodie,
I noticed a couple of things:
1. The printf message says enter a date in the format hh:mm:ss:mm/dd/yyyy BUT the scanf function
is looking for the format hh/mm/ss/mm/dd/yyyy.

Note the use of the colons and slashes!!!!!



2. I suggest that in main the loop could be written as: (But this is only one of many possibilities)
int main()

{
char ch;
do //at least once

{
Date();
ComputeTime();

fflush(stdin);//clear input buffer of any extraneous characters
printf ("\n Press Ctrl+Z to exit program or any other key to continue\n");

}

while( (ch=getchar())!=EOF);

return(0);
}