0

Can anyone help me with this and tell me why my program keeps telling me the values are incorrect? My code runs and there are no bugs. However if I enter a high temperature of 20 and a low temperature of 10, the printf statement keeps coming up saying the value is incorrect. But it shouldn’t be because I said that if high is greater than 40 or low is less than -40 or high is greater than low! Can anyone help me? Thank you.

#define NUMS3 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int high, low;

int main(void)
{ 
    printf("---=== IPC Temperature Analyzer ===--- \n");
    for (int i = 1; i < 4; i++) 
    {
        printf("Enter the high value for day %d:", i);
        scanf("%d", &high);

        printf("Enter the low value for day %d:", i);
        scanf("%d", &low);      
        while (high > 40 || low < -40 || high < low); 
        {
            printf("Incorrect values, temperatures must be in the range "
                   "-40 to 40, high must be greater than low.\n");
        }
    }
    return 0;
}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
Daniel
  • 11
  • 1
  • 3
    Take the semicolon off the end of your while loop. Also, you probabaly want to query the user again when the temperatures entered are invalid. And you should get in the habit of checking the return value of scanf for invalid input. – MFisherKDX May 30 '18 at 03:20
  • 2
    well first of all, you'll want to remove the `;` at the end of the `while` loop,, but more importantly, that shouldn't be a `while` loop to begin with. If you do enter bad values that will loop forever on the `printf`, you never give the user a chance to re-enter them. The semi-colon masks the body of the loop (so there is no body). if you enter the values you say, the loop is false, and it drops to the next statement, the `printf`. If you did enter bad values, you would infinitely loop and never reach the `printf` or anything else, making your program hang. – yano May 30 '18 at 03:20
  • @yano -- that looks like something that could be expanded upon slightly and turned into an answer. (and since he is looping, he will need to validate `EOF`, *matching or input* failures, and presumably `break` after the validation of the values.) – David C. Rankin May 30 '18 at 03:47
  • @DavidC.Rankin good looking out, however, I was just passing through. J...S has got it covered! – yano May 31 '18 at 05:07

1 Answers1

4

As pointed out in the comments, because of the immediate semicolon, the while loop has no body.

The conditions are checked and the printf() right after the while will always be executed. So the while loop here as of now is useless for those values of high and low. But had the condition for the loop been true, it would've been an infinite loop.

And while seems out of place there. You also need to prompt the user to re-enter correct values in case invalid values are entered.\

You could do something like

if (high > 40 || low < -40 || high < low) 
{
    printf("Incorrect values, temperatures must be in the range - 40 to 40, high must be greater than low.\n");
    i--;
}

in place of the loop and that printf().

i is decremented if invalid input is found so that at the end of the loop when i++ is done, the variable value remains the same.

Edit:

The user may enter invalid input like characters when you are expecting numbers. In such a case, scanf() will leave the input unconsumed on the input buffer and an infinite loop can arise if that invalid data is not consumed before the scanf() in the next iteration.

scanf() returns the number of successful assignments.

if( scanf("%d", &high)!=1 )
{
    printf("\nInvalid input.");
    i--;
    consumeBufferData(); //see the link
    continue;
}
if( scanf("%d", &low)!=1 )
{
    printf("\nInvalid input.");
    i--;
    consumeBufferData(); //see the link
    continue;
}

If invalid input is found you should consume the invalid data. See this post to see a discussion on how to do that.

Something like

void consumeBufferData()
{
    int c;
    while( (c=getchar())!='\n' && c!=EOF );
}

might suffice.

J...S
  • 5,079
  • 1
  • 20
  • 35
  • 1
    Add a solid scolding about always validating the return of `scanf` and removing extraneous characters on *matching* failure and you have something there. (currently a keyboard slip, e,g. `23a` will just loop until the loop is exhausted) – David C. Rankin May 30 '18 at 03:49
  • @DavidC.Rankin I added something about it.Thanks – J...S May 30 '18 at 03:58
  • 1
    Yep, much better. One of the most common problems is the incorrect use of `scanf` by new C programmers, it is always worth a couple of additional paragraphs to make sure they leave with at least a firm understanding of what they need to do. – David C. Rankin May 30 '18 at 04:03