简体   繁体   中英

Need some help for this C code

I have even entered value even between 3 to 10 but same error. Need some help!

int main() {
    printf("\nPlease enter the number of days between 3 and 10, inclusive:\n");

    int high[10] = { 0 };
    int low[3] = { 0 };

    int i = 0;
    int days = 4;

    for (i = 0; i < days; i++) {

        do
        {
            if (i < 3 || i > 10) {
              printf("Invalid entry, please enter a number between 3 and 10, inclusive:");
            }
            printf(" \nDay %d - High: ", i + 1);
            scanf("%d", &high[i]);
            printf("Day %d - Low: ", i + 1);
            scanf("%d", &low[i]);
       } while (i < 3 || i > 10);
    }

Output Should be like this:

(Part A)

  • Day 1 - High: 6
  • Day 1 - Low: -2
  • Day 2 - High: 8
  • Day 2 - Low: -1
  • Day 3 - High: 7
  • Day 3 - Low: -3
  • Day 4 - High: 9
  • Day 4 - Low: -4

Other part of output: (Part B)

Enter a number between 1 and 4 to see the average temperature for the entered number of days, enter a negative number to exit: 5

Invalid entry, please enter a number between 1 and 4, inclusive: 3

The average temperature up to day 3 is: 2.50

Enter a number between 1 and 4 to see the average temperature for the entered number of days, enter a negative number to exit: 2

The average temperature up to day 2 is: 2.75

Enter a number between 1 and 4 to see the average temperature for the entered number of days, enter a negative number to exit: -1

This is what happens (read in the comments):

// THE CODE STARTS SHOWING THIS:
printf("\nPlease enter the number of days between 3 and 10, inclusive:\n");

// ...declarations...

// THEN THIS IS A DO ... WHILE, IT WILL RUN THE FIRST TIME WITHOUT CHEKING
// YOU HAVEN'T TAKEN USER INPUT YET
for (i = 0; i < days; i++) {
    // ON THE FIRST ITERATION i = 0
    do
    {
        // YOU HAVEN'T TAKEN USER INPUT YET
        // REMBEMBER THAT i = 0, SO i < 3 IS true
        if (i < 3 || i > 10) {
          // IT WILL SHOW THIS, BUT YOU HAVEN'T TAKEN USER INPUT YET
          printf("Invalid entry, please enter a number between 3 and 10, inclusive:");
        }
        // ... NOW YOU TAKE USER INPUT ...
    } while (i < 3 || i > 10);
}

I want to point out that the for loop you have will go from 0 to 3, but your low array only has 3 items since you declare it int low[3] , these items are low[0] , low[1] and low[2] . That means that the line scanf("%d", &low[i]); will point beyond the low array when i is greater than 2.


I'm unsure what you want, yet, here is some code to control the range of a variable:

int var;

do
{
    printf("\nEnter a value between 5 and 12:");
    scanf("%d", &var);
} while (var < 5 || var > 12);

Run Online

Here is a variant to show a message only when the input is incorrect:

int var;

for(;;)
{
    printf("\nEnter a value between 5 and 12:");
    scanf("%d", &var);
    if (var < 5 || var > 12)
    {
        printf("\nInvalid Input\n");
    }
    else
    {
        break;
    }
}

Run Online

I'd suggest to go line by line to understand how the code above works. In particular for(;;) will breate an infinite loop, the code will only leave it when it reaches break; . Note: you may also use continue; to skip to the next iteration of the loop, this is not used in the code above, but may come handy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM