如果陈述不起作用(时间和超时)

时间:2016-03-10 09:56:06

标签: c dev-c++

似乎if语句不起作用。

例如,如果我在" Time in"中输入2405或"超时" if语句不起作用或者它不执行if(s)中的给定语句。

我们知道2405大于2400,为什么它不起作用?我试过更改逻辑运算符但它不起作用。

我的编译器是DevC ++ 4.9.9.2

这是我的源代码:

#include <windows.h>
#include <stdio.h>
#include <conio.h>

struct record
{
       char computer_code[10];
       int time_in;
       int time_out;
       int time_consumed;
};

int main()
{
    FILE *fp;
    fp = fopen("computer.txt", "w");

    struct record comprec[50];

    int a=0;
    char ans;

    do
    {
        do
        {
              a++;
              printf("\n");
              printf("Computer code: ");
              scanf("%s", comprec[a].computer_code);

              printf("Time in: ");
              scanf("%d", &comprec[a].time_in);
              if(comprec[a].time_in<0000 || comprec[a].time_in>2400)
              {
                                   printf("Time in: ");
                                   scanf("%d", &comprec[a].time_in);
              }

              printf("Time out: ");
              scanf("%d", &comprec[a].time_out);
              if(comprec[a].time_out<0000 || comprec[a].time_out>2400)
              {
                                   printf("Time out: ");
                                   scanf("%d", &comprec[a].time_out);
              }

              if(comprec[a].time_in>comprec[a].time_out)
              {
                                   printf("Time in: ");
                                   scanf("%d", &comprec[a].time_in);
              }

              comprec[a].time_consumed = comprec[a].time_out-comprec[a].time_in;

              printf("\nTime consumed: %0.4d", comprec[a].time_consumed);
              fprintf(fp, "%s    %d  %d  %0.4d\n", comprec[a].computer_code, comprec[a].time_in, comprec[a].time_out, comprec[a].time_consumed);

        }while(a<1);

        printf("\nDo you want to input again?: ");
        scanf("%s", &ans);

    }while(ans == 'Y' || ans == 'y');

    getch();
    return 0;
}

我在该计划中的输入:

Computer code: COMP-001
Time in: 2405
Time in: 2406
Time out: 2407
Time out: 2408

Time consumed: 0002
Do you want to input again?: N

预期计划:

Computer code: COMP-001
Time in: 2405
Time in: 2406
Time in: 1200
Time out: 2407
Time out: 2408
Time out: 1247

Time consumed: 0047
Do you want to input again?: N

提前谢谢你们。

1 个答案:

答案 0 :(得分:0)

你应该循环询问Time-in并循环询问超时,直到它们被正确输入。您的代码只会再次询问,然后继续输入错误。

    do {
          printf("Time in: ");
    } while (scanf("%d", &comprec[a].time_in)!=1
      || (comprec[a].time_in<0000 || comprec[a].time_in>2400));