CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Posts
    13

    URGENT Help Needed with Endless Loop Problem

    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);

    }

    }








  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: URGENT Help Needed with Endless Loop Problem

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


  3. #3
    Join Date
    Apr 1999
    Posts
    13

    Re: URGENT Help Needed with Endless Loop Problem

    So, what can I use intead of while (1) ??


  4. #4
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: URGENT Help Needed with Endless Loop Problem

    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.



  5. #5
    Join Date
    Apr 1999
    Location
    Huntsville, Al
    Posts
    27

    Re: URGENT Help Needed with Endless Loop Problem

    You can also use

    for( ; ; )
    {
    //stuff
    }


  6. #6
    Join Date
    May 1999
    Posts
    25

    Re: URGENT Help Needed with Endless Loop Problem

    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);
    }


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured