c - 段错误 - 不知道错误在哪里

标签 c segmentation-fault

只是想获得一些帮助,我是编程新手

我无法弄清楚错误在哪里以及导致错误的原因。请帮助我是 C 编程新手,这是我的代码。存在段错误,我无法找出原因。

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include "7struct.h"

struct checks checkbook;
struct checks *start;

void addcheck();
void displayall();
int deletecheck(int);

int main(int argc, char **argv)
{

start = NULL;

char input[3];
int choice = 0;

printf("\n\nThis program will gather information from the user, the");
printf("\ninformation that will be taken in from the user will be");
printf("\ncheck number, date, check written to, amount, and");
printf("\ndescription of the transaction.  The user can store up to");
printf("\n10 checks maximum. Each time a check is taken in, it will");
printf("\nbe displayed back to the user then stored.");

while(choice != 4){

    printf("\n\nPlease pick from the following options: ");
    printf("\n\n1. add a check\n2. Display checkbook\n3. Delete check\n4. Exit program\n");
    fgets(input,3,stdin);

    /*this is a check to ensure the default is called in the switch in user inputs wrong input*/
    if(atoi(input) == 0)
    {
        choice = 0;
    }
    else
    {
        choice = atoi(input);
    }

    /*switch statement for the user input, used to call the appropriate function*/
    switch(choice){

        case 1: 

            printf("\n\nYou have choosen to add a check to your checkbook.");
            addcheck( );
            break;

        case 2:

            printf("\n\nYou have choosen to display all checks in your checkbook.");
            displayall( );
            break;

        case 3:

            printf("\n\nYou have choosen to delete a check");
            deletecheck(choice);
            break;

        case 4:

            printf("\n\nYou have choosen to exit the program, Good Bye!");
            return 0;
            break;

        default:

            printf("\n\nYou have entered an invalid entry, please try again.");
            break;

    }

}

printf("\n\nGood Bye!\n");

return 0;   

}

void addcheck( ){

struct checks *cursor = NULL;
struct checks *temp = NULL;
char num[8];
char camount[10];

if(start == NULL){

    while(getchar() != '\n');
    printf("\n\nYour checkbook is empty, we will add your first check!");

    start = (struct checks*) malloc(sizeof(struct checks));
    start->next = NULL;

    printf("\nPlease enter the check number: ");
    fgets(num, 8, stdin);
    start->cnum = atoi(num);

    printf("\nPlease enter the date the check was entered: ");
    fgets(start->date, 20, stdin);

    printf("\nPlease enter the name of the person or company the check");
    printf("\nwas written to: ");
    fgets((start)->nameto, 50, stdin);

    /*extra check to make sure the user inputs a valid amount*/
    printf("\nPlease enter the amount of the check: ");
    fgets(camount, 10, stdin);
    if(atoi(camount) == 1)
    {
        (start)->amount = atoi(camount);
    }
    else
    {
        while(atoi(camount) == 0)
        {
            printf("\nYou have enter an invalid amount, please try again: ");
            fgets(camount, 10, stdin);
        }
        (start)->amount = atoi(camount);
    }

    printf("\nPlease enter a description of the check: ");
    fgets((start)->description, 100, stdin);

    printf("\n\nHere is the information about the check you have entered: ");
    printf("\n\nCheck number: %i", (start)->cnum);
    printf("\nDate check was written: %s", (start)->date);
    printf("Written to: %s", (start)->nameto);
    printf("Amount of check: %.2f", (start)->amount);
    printf("\nDescription: %s", (start)->description);

}

else
{

    cursor = start;

    while(cursor->next != NULL)
    {

        cursor = cursor->next;

    }

    cursor->next = malloc(sizeof(struct checks));
    temp = cursor->next;

    while(getchar() != '\n');
    printf("\nPlease enter the check number: ");
    fgets(num, 8, stdin);
    temp->cnum = atoi(num);

    printf("\nPlease enter the date the check was entered: ");
    fgets(temp->date, 20, stdin);

    printf("\nPlease enter the name of the person or company the check");
    printf("\nwas written to: ");
    fgets(temp->nameto, 100, stdin);

    printf("\nPlease enter the amount of the check: ");
    fgets(camount, 10, stdin);

    if(atoi(camount) == 1)
    {
        temp->amount = atoi(camount);
    }
    else
    {
        while(atoi(camount) == 0)
        {
            printf("\n\nYou have enter an invalid amount, please try again: ");
            fgets(camount, 10, stdin);

        }
        temp->amount = atoi(camount);
    }

    printf("\nPlease enter a description of the check: ");
    fgets(temp->description, 100, stdin);        

    printf("\n\nHere is the information about the check you have entered: ");
    printf("\n\nCheck number: %i", temp->cnum);
    printf("\nDate check was written: %s", temp->date);
    printf("Written to: %s", temp->nameto);
    printf("Amount of check: $%.2f", temp->amount);
    printf("\nDescription: %s", temp->description);

    return;


}

}

void displayall( ){

struct checks *cursor = NULL;
cursor = start;

if(start == NULL)
{

    printf("\n\nThere are no checks to be displayed.");

}

else
{

    printf("\n\n------------------------------------------------------");

    if(cursor->next == NULL)
    {

        printf("\n\nCheck number: %i", cursor->cnum);
        printf("\nDate check was written: %s", cursor->date);
        printf("Written to: %s", cursor->nameto);
        printf("Amount of check: $%.2f", cursor->amount);
        printf("\nDescription: %s", cursor->description);
        printf("\n-------------------------------------------------------");

    }

    else
    {

        while(cursor->next != NULL)
        {

            printf("\n\nCheck number: %i", cursor->cnum);
            printf("\nDate check was written: %s", cursor->date);
            printf("Written to: %s", cursor->nameto);
            printf("Amount of check: $%.2f", cursor->amount);
            printf("\nDescription: %s", cursor->description);
            printf("\n-------------------------------------------------------");
            cursor = cursor->next;

        }

        printf("\n\nCheck number: %i", cursor->cnum);
        printf("\nDate check was written: %s", cursor->date);
        printf("Written to: %s", cursor->nameto);
        printf("Amount of check: $%.2f", cursor->amount);
        printf("\nDescription: %s", cursor->description);
        printf("\n-------------------------------------------------------");
    }

}

}

int deletecheck(int choice){

struct checks *cursor;
struct checks *temp;
int checknum;

cursor = start;

printf("\n\ntest %d", start->cnum);

printf("\n\nPlease enter the check number you would like to delete: ");
scanf("%d", &checknum);

if(start == NULL)
{

    printf("\n\nThere are no records in the checkbook to be deleted");

}

else
{

    while(cursor->next != NULL)
    {
        if(cursor->cnum == checknum)
        {   

            printf("\n\ntest\n\n");
            if(start == cursor)
            {

                start = cursor->next;
                free(cursor);
                printf("\n%d", cursor->cnum);
                return 1;

            }

            else
            {
                printf("\n\ntest\n\n");
                temp->next = cursor->next;
                free(cursor);

                return 1;
            }

        }

        else
        {

            temp = cursor;
            cursor = cursor->next;

        }
    }
}

printf("\n\nCheck number: %d, has been deleted", checknum);
return 0;
}

here is my struct below.

struct checks
{

int cnum;
char date[20];
char nameto[50];
double amount;
char description[100];
struct checks* next;

};

最佳答案

我没有看到段错误,但以下更正后的代码可以工作。 关键点是检查cursor是否为NULL。如果您检查下一个成员,则您没有检查列表的最后一个位置。

int deletecheck(int choice)
{
    struct checks *cursor;
    struct checks *temp;
    int checknum;

    cursor = start;
    temp = cursor;

    printf("\n\ntest %d", start->cnum);

    printf("\n\nPlease enter the check number you would like to delete: ");
    scanf("%d", &checknum);

    if(start == NULL)
    {

        printf("\n\nThere are no records in the checkbook to be deleted");
    }
    else
    {

        do
        {
            if(cursor->cnum == checknum)
            {
                printf("\n\ntest\n\n");
                if(start == cursor)
                {

                    if (cursor->next != NULL)
                    {
                        start = cursor->next;
                    }
                    else
                    {
                        start = NULL;
                    }
                    free(cursor);
                    printf("\n%d", cursor->cnum);
                    return 1;

                }
                else
                {
                    printf("\n\ntest\n\n");
                    if (cursor->next != NULL)
                    {
                        temp->next = cursor->next;
                    }
                    else
                    {
                        temp->next = NULL;
                    }
                    free(cursor);

                    return 1;
                }
            }
            else
            {

                temp = cursor;
                cursor = cursor->next;

            }
        }
        while(cursor != NULL);
    }

    printf("\n\nCheck number: %d, has been deleted", checknum);
    return 0;
}

关于c - 段错误 - 不知道错误在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34216780/

相关文章:

c - 这个条件测试什么?

c++ - OpenSSL 解密 - EVP_DecryptFinal_ex 失败

linux - 确定在 GDB 下导致 SIGSEGV 的 CPU 陷阱?

c - 获取段错误

c++ - 继续出现段错误?

c - 如何用C在Amazon S3中进行签名

c - 将字符数组打字为 const char *

c - 无法为 Hello World 返回 'make',无法获取内核 header

objective-c - 调试 SIGSEGV 崩溃

c - 从 C 中的链接列表中删除时出现重复段错误