简体   繁体   English

程序(C 语言)不启动 function,它只是简单地结束

[英]Program (in C) doesn't start function, and it just simply ends

After running the next code, in the function get_data(), everything runs normally until I get to the end of the For, when program should call the function continue_function() but in somehow, ignores it, and the program just ends.运行下一个代码后,在 function get_data() 中,一切正常运行,直到我到达 For 的末尾,此时程序应该调用 function continue_function() 但不知何故,忽略它,程序就结束了。

Am I calling it in a wrong way?我是否以错误的方式称呼它?

I'm just start learning programming in C and this is one of the last exam's exercises.我刚刚开始在 C 中学习编程,这是最后一次考试的练习之一。

#include <stdio.h>


#define MAX 100
#define YES 1
#define NO 0


struct record
{
    char fname[15+1];
    char lname[20+1];
    char phone[9+1];
    long income;
    int month;
    int day;
    int year;
};

struct record list[MAX];
int last_entry = 0; 


void get_data(void);
void display_report(void);
int continue_function(void);
void clear_kb(void);


main()
{
    int cont = YES;
    int ch;

    while(cont == YES)
    {
        printf("\n");
        printf("\n      MENU");
        printf("\n    =========\n");
        printf("\n1. Enter Names");
        printf("\n2. Print Report");
        printf("\n3. Quit");
        printf("\n\nEnter Selection ==> ");

        scanf("%d", &ch);
        clear_kb();

        system("cls");

        switch(ch)
        {
            case 1: get_data();
                    break;
            case 2: display_report();
                    break;
            case 3: printf("\n\nThank You for using this Program!");
                    cont = NO;
                    break;
            default: printf("\n\nInvalid Choice, Please Select 1 to 3!");
                    break;
        }
    }
}


void get_data(void)
{
    int cont;
    int ctr;

    for(cont = YES; last_entry < MAX && cont == YES; last_entry++)
    {
        printf("\n\nEnter information for Person %d.", last_entry+1);

        printf("\n\nEnter first name: ");
        gets(list[last_entry].fname);

        printf("Enter last name: ");
        gets(list[last_entry].lname);

        printf("Enter phone in 123-4567 format: ");
        gets(list[last_entry].phone);

        printf("Enter Yearly Income: ");
        scanf("%ld", &list[last_entry].income);

        printf("Enter Birthday: ");

        do
        {
            printf("\n\tMonth (0 - 12): ");
            scanf("%d", &list[last_entry].month);
        } 
        while (list[last_entry].month < 0 || list[last_entry].month > 12);

        do
        {
            printf("\tDay (0 - 31): ");
            scanf("%d", &list[last_entry].day);
        } 
        while (list[last_entry].day < 0 || list[last_entry].day > 31);

        do
        {
            printf("\tYear (1800 - 2025): ");
            scanf("%d", list[last_entry].year);
        } 
        while (list[last_entry].year < 1800 || list[last_entry].year > 2025);
        

        cont = continue_function();

    }

    if(last_entry == MAX)
    {
        printf("\n\nMaximum Number of Names has been entered!\n");
    }

}


void display_report(void)
{
    long month_total = 0, grand_total = 0;
    int x, y;

    fprintf(stdout, "\n\n");
    fprintf(stdout, "\n         REPORT");
    fprintf(stdout, "\n        ========");


    for(x = 0; x <= 12; x++)
    {
        month_total = 0;
        for(y = 0; y < last_entry; y++)
        {
            if(list[y].month == x)
            {
                fprintf(stdout, "\n\t%s %s %s %ld", list[y].fname, list[y].lname, list[y].phone, list[y].income);
                month_total += list[y].income;
            }

        }

        fprintf(stdout, "\nTotal for month %d is %ld:", x, month_total);
        grand_total += month_total;
    }

    fprintf(stdout, "\n\nReport Totals:");
    fprintf(stdout, "\nTotal Income is %ld", grand_total);
    fprintf(stdout, "\nAverage Income is %ld", grand_total/last_entry);


    fprintf(stdout, "\n\n* * * End of Report * * *");
}


int continue_function(void)
{
    char ch;

    printf("\n\nDo you wish to continue? (Y)es/(N)o: ");
    getc(ch);

    while(ch != 'n' && ch != 'N' && ch != 'y' && ch != 'Y')
    {
        printf("\n%c is invalid!", ch);
        printf("\n\nPlease enter \'N\' to Quit or \'Y\' to Continue: ");
        getc(ch);
    }

    clear_kb();

    if(ch == 'n' || ch == 'N')
    {
        system("cls");
        return(NO);
    }
    else
    {
        system("cls");
        return(YES);
    }
}


void clear_kb(void)
{
    char junk[80];
    gets(junk);
}

You're not using your get functions properly: https://www.tutorialspoint.com/c_standard_library/c_function_getc.htm您没有正确使用get函数: https://www.tutorialspoint.com/c_standard_library/c_function_getc.htm

int getc(FILE *); for instance requires a FILE* as argument and returns the character, but you are passing the character as argument, it should look like ch = getc(stdin);例如需要一个FILE*作为参数并返回字符,但是您将字符作为参数传递,它应该看起来像ch = getc(stdin); . .

On another note, notice that when you use scanf you pass &c ?另一方面,请注意,当您使用scanf时,您会通过&c This is because for a value to be modified, you need to pass the address where it's stored (&) and not the value itself.这是因为要修改值,您需要传递存储它的地址 (&),而不是值本身。 So even if getc took a char, it would have to be getc(&ch) .因此,即使getc使用了一个字符,它也必须是getc(&ch)

You should follow a quick tutorial on pointers and types, if you come from a higher level language or if you're new that could be a good idea.如果您来自更高级别的语言或者如果您是新手,那么您应该遵循关于指针和类型的快速教程,这可能是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM