c - 程序没有按预期退出

标签 c

我是 C 的新手,我正在尝试编写一个程序,让您输入最多 100 个人的年龄和薪水。程序首先会打印一些句子来介绍(显示功能),然后问你是否要继续(yes_no 功能)。输入一个人的信息后,程序还会询问是否继续输入下一个人的信息。如果您想继续,您需要输入 1 和 0 表示不/退出。当我编译并运行代码时,我发现了一个问题,我想不通为什么!

The problem is that if you choose 0 (which means no/exit) after entering one's information, the program does not exit. It instead just asks the next person's information. But when I choose 0 right from the beginning, it just exits like usual. Why?

#include<stdio.h>

#define max 100
#define yes 1
#define no 0

int display(void);
int yes_no(void);
void get_data(void);

int date[max],month[max],year[max]; 
int cont;
int salary;

int main(){
    cont=display();
  if (cont==yes){
        get_data();
    }
return 0;
}

int display(void){
    printf("This program will let you enter ");
    printf("the age and salary of up to 100 people ");

    cont=yes_no();
    return cont;
}

int yes_no(void){
    int i=0;
    printf("\nDo you want to continue? Enter 1 for Yes and 0 for No\n");
    scanf("%d", &i);
    while(i<0 || i>1){
        printf("Invalid value.Please enter again\n");
        scanf("%d", &i);
    }
    if(i==1){
        return (yes);
    }else return (no);
}

void get_data(void){
    int i=0;
    for(i=0;i<max;i++){
        printf("Enter information for people %d\n", i+1);
        printf("Enter birthday\n");
        do{
            printf("Enter date\n");
            scanf("%d", &date[i]);
        }while( 0>date[i] || 31<date[i] );
        do{
             printf("Enter month\n");
             scanf("%d", &month[i]);
        }while( 0>month[i] || 12<month[i]);
        do{
             printf("Enter year\n");
             scanf("%d", &year[i]);
        }while( 1900>year[i] || 2016<year[i]);

             printf("Enter salary\n");
             scanf("%d", &salary);

    cont=yes_no();
    }
}

最佳答案

void get_data(void){
    int i=0;
    for(i=0;i<max;i++){
        printf("Enter information for people %d\n", i+1);
        printf("Enter birthday\n");
        do{
            printf("Enter date\n");
            scanf("%d", &date[i]);
        }while( 0>date[i] || 31<date[i] );
        do{
             printf("Enter month\n");
             scanf("%d", &month[i]);
        }while( 0>month[i] || 12<month[i]);
        do{
             printf("Enter year\n");
             scanf("%d", &year[i]);
        }while( 1900>year[i] || 2016<year[i]);

             printf("Enter salary\n");
             scanf("%d", &salary);

    cont=yes_no(); // <== The Problem lies here
    }
}

您询问用户是否要继续,但您从不检查 yes_no() 的返回值 只需在这一行之后添加它,它就会像一个魅力一样工作:

if (cont == no)
    return;

正如其他人所提到的,您仍然可以做一些事情来“改进”您的代码。

defines 应该大写,这样 #define YES 1 就会遵守这个约定。

而且你不应该使用全局变量。这些都是糟糕的编程风格。只需将您在其他函数中需要的东西作为参数传递,如果您稍后需要操纵值,则将它们作为指针传递。

格式也可以改进(但这是一个主要基于意见的主题;)) 在 C 语言中,通常每个大括号都有一行。

void get_data(void)
{
...
}

//instead of
void get_data(void){
...
}

do-while 循环之后的空白应该更像这样:

do
{
...
} while(1900 > year[i]); //here the curly bracket is ok that way 

并且运算符两边应该有一个空白:

printf("Enter information for people %d\n", i + 1);
// instead of this
printf("Enter information for people %d\n", i+1);

这就是我到目前为止所看到的。

关于c - 程序没有按预期退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35580901/

相关文章:

c - 将malloc的变量值分配给一个新变量,新变量是否也在堆内存上?

c - 在 c 中确定运行时的精度

c - 在 C 中编辑文件中的一行

c - 为什么 100 * 4.2 结果是 419 而不是 420?

c - main 函数和之后的变量

c - 在 C 中检查给定字符串是否为回文的有效方法

c - gtk_menu_item_new_with_labelex 与 gtk_menu_item_new_with_label

c - 汇编 intel x86 结构,并接收结构作为参数

c - FD_SETSIZE 与计算值

c - 从文件中读取矩阵到 **a 给我段错误