C:';其他';当满足if条件时执行-结构

C:';其他';当满足if条件时执行-结构,c,if-statement,while-loop,structure,C,If Statement,While Loop,Structure,我目前正在从一本书中学习C语言,一个问题是编写一个程序来查找用户输入两次之间经过的时间,我使用了24小时格式 问题是主函数中的while循环没有在应该执行的时候执行。当用户输入的时间超出边界时(0以下、24小时以上等) while循环应该一直执行,直到用户输入一个有效的时间,但是它只是被跳过 我认为原因可能是因为检查时间并将全局变量'error'赋值为等于1或0的函数始终在执行else语句(设置error=0),即使if语句为true也是如此 void checkTime (struct tim

我目前正在从一本书中学习C语言,一个问题是编写一个程序来查找用户输入两次之间经过的时间,我使用了24小时格式

问题是主函数中的while循环没有在应该执行的时候执行。当用户输入的时间超出边界时(0以下、24小时以上等)

while循环应该一直执行,直到用户输入一个有效的时间,但是它只是被跳过

我认为原因可能是因为检查时间并将全局变量'error'赋值为等于1或0的函数始终在执行else语句(设置error=0),即使if语句为true也是如此

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;
}
}
void checkTime(结构时间a)
{   
如果(a.hours<0 | | a.hours>23){
printf(“请输入1到23范围内的有效小时数\n”);
误差=1;
}
如果(a.minutes<0 | | a.minutes>59){
printf(“请输入有效的分钟数,范围为1到59\n”);
误差=1;
}
如果(a.seconds<0 | | a.seconds>59){
printf(“请在1到59之间输入有效的秒数\n”);
误差=1;
}
否则{
误差=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;
结构时间
{
整小时;
整数分钟;
整数秒;
};
//函数以查找两个参数之间经过的时间
结构时间经过时间(结构时间时间1,结构时间时间2)
{
结构时间;
如果(time2.hours>time1.hours)
eTime.hours=time2.hours-time1.hours;
其他的
eTime.hours=(24-time1.hours)+time2.hours;
如果(time2.minutes>time1.minutes)
eTime.minutes=time2.minutes-time1.minutes;
其他的
eTime.minutes=(60-time1.minutes)+time2.minutes;
如果(time2.seconds>time1.seconds)
eTime.seconds=time2.seconds-time1.seconds;
其他的
eTime.seconds=(60-time1.seconds)+time2.seconds;
返回时间;
}
//函数检查输入的时间是否在有效参数内
无效检查时间(结构时间a)
{   
如果(a.hours<0 | | a.hours>23){
printf(“请输入1到23范围内的有效小时数\n”);
误差=1;
}
如果(a.minutes<0 | | a.minutes>59){
printf(“请输入有效的分钟数,范围为1到59\n”);
误差=1;
}
如果(a.seconds<0 | | a.seconds>59){
printf(“请在1到59之间输入有效的秒数\n”);
误差=1;
}
否则{
误差=0;
}
}
内部主(空)
{   
结构时间1;
结构时间2;
结构时间主时间;
printf(“以小时:分钟:秒的格式输入时间1”);
scanf(“%i:%i:%i”、&time1.hours、&time1.minutes、&time1.seconds);
检查时间(时间1);
while(错误==1){
printf(“以小时:分钟:秒的格式输入时间1”);
scanf(“%i:%i:%i”、&time1.hours、&time1.minutes、&time1.seconds);
}
printf(“以小时:分钟:秒的格式输入时间2\n”);
scanf(“%i:%i:%i”、&time2.hours、&time2.minutes、&time2.seconds);
检查时间(时间2);
while(错误==1){
printf(“以小时:分钟:秒的格式输入时间2\n”);
scanf(“%i:%i:%i”、&time2.hours、&time2.minutes、&time2.seconds);
}
主时间=经过的时间(时间1,时间2);
printf(“时间1和时间2之间经过的时间是%i:%i:%i\n”,main_eTime.hours,
main_eTime.minutes,main_eTime.seconds);
返回0;
}

我认为您的分支条件有问题,特别是在
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;
    }
}
void checkTime(结构时间a)
{   
如果(a.hours<0 | | a.hours>23){
printf(“请输入1到23范围内的有效小时数\n”);
误差=1;
}
否则如果(a.minutes<0 | | a.minutes>59){
printf(“请输入有效的分钟数,范围为1到59\n”);
误差=1;
}
否则如果(a.seconds<0 | | a.seconds>59){
printf(“请在1到59之间输入有效的秒数\n”);
误差=1;
}
否则{
误差=0;
}
}

仅当三个变量(小时、分钟和秒)均有效时,上述操作才会运行
else
块。

您的代码结构为:

if (COND_1) {
    ...
}

if (COND_2) {
    ...
}

if (COND_3) {
    ...
}
else {
    ...
}
如果
COND_1
COND_2
COND_3
中的任何一个都不正确,您似乎认为应该运行
else
。但它不是这样工作的:
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;
}
错误=0;
如果(a.hours<0 | | a.hours>23){
printf(“请输入1到23范围内的有效小时数\n”);
误差=1;
}
如果(a.minutes<0 | | a.minutes>59){
printf(“请输入有效的分钟数,范围为1到59\n”);
误差=1;
}
如果(a.seconds<0 | | a.seconds>59){
printf(“请在1到59之间输入有效的秒数\n”);
误差=1;
}

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

我认为后一种解决方案更好,因为它会报告输入中所有可能的问题。不要使用全局设置将是一个很好的建议另一个没有提到的大问题是,如果
error
实际上设置为1,你进入一个无限循环。
checkTime
应该在循环内。Aldo您可能想在scanf中使用
%d
而不是
%i
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;
}