警告:while循环

时间:2017-02-08 02:53:36

标签: c arrays while-loop int

我正在编写一个程序,要求用户在三天内输入高温和低温。每天的高温必须大于低,高必须大于41,低必须小于负-41。

我在第一天的输入后写了一个while语句但是,我得到了指针和整数之间的错误比较。

我认为它与我使用集合整数有关,所以我试着制作一个while语句,其中涉及高大于低,这导致程序工作,但我发现while循环是完全跳过。到目前为止,这是我的代码:

编辑:我开始明白我的while循环出错了。我相信这是因为我忽略了将数组中的值分配给高和低,并且如果用户满足while循环的条件,我也忽略了重新运行代码。最初,我写了它,所以高和低没有值,while条件被困在一个无限循环中,因为在满足条件后我没有给它执行某些东西。

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

}

1 个答案:

答案 0 :(得分:1)

  

每天的高温必须大于低温   高不得大于41且低值不得小于   否定-41。

我修改了你的代码和书面评论也理解:

#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部分。