简体   繁体   中英

Warning: Comparison between pointer and integer in while loop

I'm writing a program that asks the user to input the high and low temperatures over the course of three days. The high temperature for each day has to be greater than the low, the high must not be greater than 41 and the low must not be less than negative -41.

I wrote a while statement following the inputs for the first day however, I get the error comparison between pointer and integer.

I figured it had something to do with me using a set integer so I tried just making a while statement that involved high being greater than low, which resulted in the program working, but I found the while loop was skipped entirely. Here's my code so far:

Edit: I'm beginning to understand where my while loop went wrong. I believe it was because I neglected to assign a value from the array to the high and low and I also neglected to have the code rerun if the user met the conditions for the while loop. Initially, I had wrote it so the high and low held no value and the while condition was trapped in an infinite loop because I did not give it something to execute following the conditions being met.

#include <stdio.h>
#define NUMS 3

int main (void)

{
int high[NUMS];
int low[NUMS];
int max = 40;
int min = -40;

    printf ("---===IPC Temperatur Analyzer ===---\n");

    printf ("Enter the high value for day 1: ");
    scanf ("%d", &high);

    printf ("Enter the low value for day 1: ");
    scanf ("%d", &low);

     while (high[0] > max || low[0] > min || high[0] < low[0]) {
           printf("Try again\n");

           printf ("Enter the high value for day 1: ");
           scanf ("%d", &high[0]);

           printf ("Enter the low value for day 1: ");
           scanf ("%d", &low[0]);

    }

    printf ("Enter the high value for day 2: ");
    scanf ("%d", &high[1]);

    printf ("Enter the low value for day 2: ");
    scanf ("%d", &low[1]);

    printf ("Enter the high value for day 3: ");
    scanf ("%d", &high[2]);

    printf ("Enter the low value for day 3: ");
    scanf ("%d", &low[2]);

return 0;

}

The high temperature for each day has to be greater than the low, the high must not be greater than 41 and the low must not be less than negative -41.

i have modified your code and written comment also to understand :

#include <stdio.h>
#define NUMS 3

int main (void)

{
int high[NUMS];
int low[NUMS];
const int MAX = 41;
const int MIN = -41;

    printf ("---===IPC Temperatur Analyzer ===---\n");

    printf ("Enter the high value for day 1: ");
    scanf ("%d", &high[0]);       //address of first element

    printf ("Enter the low value for day 1: ");
    scanf ("%d", &low[0]);        //address of first element

    /*Check for User Input Value*/
     while (high[0] > MAX || low[0] < MIN || high[0] < low[0]) {

           printf("Try again\n"); 

           printf ("Enter the high value for day 1: ");
           scanf ("%d", &high[0]);

           printf ("Enter the low value for day 1: ");
           scanf ("%d", &low[0]);


    }

    printf ("Enter the high value for day 2: ");
    scanf ("%d", &high[1]);

    printf ("Enter the low value for day 2: ");
    scanf ("%d", &low[1]);

    //TODO-:/*Check for User Input Value*/

    printf ("Enter the high value for day 3: ");
    scanf ("%d", &high[2]);

    printf ("Enter the low value for day 3: ");
    scanf ("%d", &low[2]);
    //TODO-:/*Check for User Input Value*/

    //TODO-:/*Print the all value*/

return 0;

}

Todo part you can complete by taking reference from other part of code.

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