Home > Software engineering >  for loop repeats until value is less than three
for loop repeats until value is less than three

Time:02-14

I dont know why my for loop is not working. I have been trying for so long. It just runs for the do / while and gives return. Everything in the do / while loop. I have declared a macro outside the main function and I am using it as loop condition. I can't add more details, all I have is code.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#define MIN_YEAR 2012
#define MAX_YEAR 2022

#define LOG_DAYS 3

int main(void)
{
    const int JAN = 1;
    const int DEC = 12;
    int year = 0;
    int month = 0;
    char day1 = 01;
    char day2 = 02;
    char day3 = 03;
    float ratingMorning = 0;
    float ratingEvening = 0;

    printf("General Well-being Log\n"
        "======================\n");

    do
    {
        printf("Set the year and month for the well-being log (YYYY MM): ");
        scanf(" %d %d", &year, &month);

        if ((MIN_YEAR > year || year > MAX_YEAR))
        {
            printf("   ERROR: The year must be between 2012 and 2022 inclusive\n");
        }

        if ((JAN > month || month > DEC))
        {
            printf("   ERROR: Jan.(1) - Dec.(12)\n");
        }

        if ((JAN <= month && month <= DEC) && (MIN_YEAR <= year && year <= MAX_YEAR))
        {
            printf("\n*** Log date set! ***\n\n");
            break;
        }

    } while ((MIN_YEAR <= year || year <= MAX_YEAR) && (JAN <= month || month <= DEC));

    for (int i = 0; i < LOG_DAYS; i  )
    {
        printf(" %d", year);
        printf("-");
        printf(" %d", month);
        printf("-");
        printf(" %d",day1);
        printf("   Morning rating (0.0-5.0): ");
        scanf(" %d", &ratingMorning);

        if (ratingMorning < 0 || ratingMorning > 5)
        {
            printf("      ERROR: Rating must be between 0.0 and 5.0 inclusive!");
        }
        
        printf("   Evening rating (0.0-5.0): ");
        scanf(" %d", &ratingEvening);

        if (ratingEvening < 0 || ratingEvening > 5)
        {
            printf("      ERROR: Rating must be between 0.0 and 5.0 inclusive!");
        }
    }
    return 0;
}

CodePudding user response:

If the problem is that:

if (ratingMorning < 0 || ratingMorning > 5) is not working

i.e. the error line ERROR: Rating must be between 0.0 and 5.0 inclusive!" is never printed, then when you change the %d to %f in the scanf(" %d", &ratingMorning); lines, the errors are printed because now ratingMorning and ratingEvening contain valid values, whereas when they were input as integers, the values were invalid.

CodePudding user response:

The test in the while part of the do / while loop is incorrect. It is much simpler to use a for(;;) loop and break when the input is correct, avoiding redundant and sometime inconsistent tests.

The scanf() conversion specifier for float is %f, not %d.

Here is a modified version:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#define MIN_YEAR 2012
#define MAX_YEAR 2022

#define LOG_DAYS 3

int main(void) {
    const int JAN = 1;
    const int DEC = 12;
    int year = 0;
    int month = 0;
    char day1 = 1;
    char day2 = 2;
    char day3 = 3;
    float ratingMorning = 0;
    float ratingEvening = 0;

    printf("General Well-being Log\n"
        "======================\n");

    for (;;) {
        printf("Set the year and month for the well-being log (YYYY MM): ");
        if (scanf(" %d %d", &year, &month) != 2) {
            printf("invalid input\n");
            return 1;
        }
        if (year < MIN_YEAR || year > MAX_YEAR) {
            printf("   ERROR: The year must be between 2012 and 2022 inclusive\n");
        } else
        if (month < JAN || month > DEC)) {
            printf("   ERROR: Jan.(1) - Dec.(12)\n");
        } else {
            printf("\n*** Log date set! ***\n\n");
            break;
        }
    }

    for (int i = 0; i < LOG_DAYS; i  ) {
        printf(" %d- %d- %d", year, month, day1);
        printf("   Morning rating (0.0-5.0): ");
        if (scanf("%f", &ratingMorning) != 1) {
            printf("invalid input\n");
            return 1;
        }
        if (ratingMorning < 0 || ratingMorning > 5) {
            printf("      ERROR: Rating must be between 0.0 and 5.0 inclusive!");
            continue;
        }
        
        printf("   Evening rating (0.0-5.0): ");
        if (scanf("%f", &ratingEvening) != 1) {
            printf("invalid input\n");
            return 1;
        }
        if (ratingEvening < 0 || ratingEvening > 5) {
            printf("      ERROR: Rating must be between 0.0 and 5.0 inclusive!");
            continue;
        }
        // ... handle the ratings and report success
    }
    return 0;
}
  • Related