我想获得一些帮助,我是编程新手
我无法弄清楚错误的位置以及造成错误的原因。请帮助我是编程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;
}
这是我的结构。
struct checks
{
int cnum;
char date[20];
char nameto[50];
double amount;
char description[100];
struct checks* next;
};
答案 0 :(得分:1)
我没有看到分段错误,但以下更正的代码有效。 关键是检查游标是否为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;
}