C:'其他'如果条件满足则执行 - 结构

时间:2017-11-10 01:11:18

标签: c if-statement while-loop structure

我目前正在从一本书中学习C,而一个问题是编写一个程序来查找用户输入2次之间的时间,我已经使用24小时格式这样做了。

问题是main函数中的while循环在应该执行时不执行。当用户输入的时间超出界限时(0岁以下,超过24小时等)

while(error == 1) {
    printf("Enter time 1 in the format hours:mins:sec\n");
    scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);

然后while循环应该继续执行,直到用户输入有效时间,但它只是被跳过。

我认为原因可能是因为检查时间并分配全局变量'错误'等于1或0总是执行else语句(设置error = 0),即使if语句为真。

void checkTime (struct time a)
{   

if(a.hours < 0 || a.hours > 23) {
    printf("Please enter a valid figure for hours in the range 1 to 23\n");
    error = 1;
}

if(a.minutes < 0 || a.minutes > 59) {
    printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
    error = 1;
}

if(a.seconds < 0 || a.seconds > 59) {
    printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
    error = 1;

}

else {
    error = 0;
}
}

这是一个与结构工作方式有关的逻辑问题吗?

这是完整的代码:

#include <stdio.h>

//Program to find the time elapsed between 2 times entered by user

int error = 0;

struct time 
{
int hours;
int minutes;
int seconds;
};

//Function to find elapsed time between the 2 arguments
struct time elapsed_time (struct time time1, struct time time2)
{

struct time eTime;


if (time2.hours > time1.hours)
    eTime.hours = time2.hours - time1.hours;
else
    eTime.hours = (24 - time1.hours) + time2.hours;


if (time2.minutes > time1.minutes)
    eTime.minutes = time2.minutes - time1.minutes;
else
    eTime.minutes = (60 - time1.minutes) + time2.minutes;


if (time2.seconds > time1.seconds)
    eTime.seconds = time2.seconds - time1.seconds;
else
    eTime.seconds = (60 - time1.seconds) + time2.seconds;


return eTime;
}

//Function to check if inputed times are within valid parameters
void checkTime (struct time a)
{   

if(a.hours < 0 || a.hours > 23) {
    printf("Please enter a valid figure for hours in the range 1 to 23\n");
    error = 1;
}

if(a.minutes < 0 || a.minutes > 59) {
    printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
    error = 1;
}

if(a.seconds < 0 || a.seconds > 59) {
    printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
    error = 1;

}

else {
    error = 0;
}

}


int main (void)
{   
struct time time1;
struct time time2;
struct time main_eTime;

printf("Enter time 1 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);

checkTime(time1);

while(error == 1) {
    printf("Enter time 1 in the format hours:mins:sec\n");
    scanf("%i:%i:%i", &time1.hours, &time1.minutes, &time1.seconds);
}

printf("Enter time 2 in the format hours:mins:sec\n");
scanf("%i:%i:%i", &time2.hours, &time2.minutes, &time2.seconds);

checkTime(time2);

while(error == 1) {
    printf("Enter time 2 in the format hours:mins:sec\n");
    scanf("%i:%i:%i", &time2.hours, &time2.minutes, &time2.seconds);
}

main_eTime = elapsed_time(time1, time2);

printf("Elapsed time between time 1 and time 2 is %i:%i:%i\n", main_eTime.hours, 
        main_eTime.minutes, main_eTime.seconds);

return 0;

}

2 个答案:

答案 0 :(得分:4)

代码的结构是:

if (COND_1) {
    ...
}

if (COND_2) {
    ...
}

if (COND_3) {
    ...
}
else {
    ...
}

如果elseCOND_1COND_2都不属实,您似乎认为COND_3应该会运行。但这不是它的工作原理:else附加在前面的if语句中,仅此而已。关于else块是否运行的唯一部分是COND_3条件。

你应该做的是:

if (COND_1) {
    ...
}
else if (COND_2) {
    ...
}
else if (COND_3) {
    ...
}
else {
    ...
}

通过这种方式构造代码,我们确保只执行一个块。

......或:

error = 0;

if(a.hours < 0 || a.hours > 23) {
    printf("Please enter a valid figure for hours in the range 1 to 23\n");
    error = 1;
}

if(a.minutes < 0 || a.minutes > 59) {
    printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
    error = 1;
}

if(a.seconds < 0 || a.seconds > 59) {
    printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
    error = 1;
}

通过在开头设置error = 0,您根本不需要else块。

答案 1 :(得分:2)

我认为您的分支条件有问题,尤其是checkTime函数。如果秒有效,则最后的else条件将执行,并且它不关心您是否正确输入小时和分钟。

将其更改为:

void checkTime (struct time a)
{   
    if(a.hours < 0 || a.hours > 23) {
        printf("Please enter a valid figure for hours in the range 1 to 23\n");
        error = 1;
    }

    else if(a.minutes < 0 || a.minutes > 59) {
        printf("Please enter a valid figure for minutes in the range of 1 to 59\n");
        error = 1;
    }

    else if(a.seconds < 0 || a.seconds > 59) {
        printf("Please enter a valid figure for seconds in the range of 1 to 59\n");
        error = 1;
    }

    else {
        error = 0;
    }
}

只有当所有三个变量(小时,分钟和秒都有效)时,上述内容才会运行else块。