• 0

Who can find out the bug?


Question

My codes can compile correctly in VC6.0, but they always have something wrong in TC2.0. I know TC2.0 is C compiler, but I really did't use any C++ codes in my program. Unfortunately,my homework asked us to do it use TC2.0. My god,who can find out the bugs??????

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct info
{
	char num[20];
	char name[10];
	char adress[30];
	char sex[5];
	struct info *next;
	struct info *last;
};

void author();
struct info* input(struct info *);
struct info* search(char *,struct info *);
struct info* del(char *,struct info *);
struct info* deleteMenu(struct info *);
void searchMenu(struct info *fPtr);
void saveData(struct info *);
struct info* loadData(struct info *);
void showAll(struct info *);

void main()
{
	author();
	struct info *first=0;
	int choose;
	while(1){
 ?printf("1:Input\n2:Delete\n3:Search\n4:Quit\n5:Save Data\n6:Load Data\n7:Show All\n\n\n");
 ?printf("input your choose:"); ?
 ?scanf("%d",&choose); ? ? 
 ?
 ?switch(choose){
 ?case 1:
 ?	first=input(first);
 ?	break;
 ?case 2:
 ?	first=deleteMenu(first);
 ?	break;
 ?case 3:
 ?	searchMenu(first);
 ?	break;
 ?case 4:
 ?	return;
 ?case 5:
 ?	saveData(first);
 ?	break;
 ?case 6:
 ?	first=loadData(first);
 ? ? ?break;
 ?case 7:
 ?	showAll(first);
 ?	break;
 ?default:
 ?	printf("Wrong\n\n\n");
 ?	break;
 ?}
	printf("\n\n\n");
	}
}


struct info *input(struct info *fPtr)
{
	struct info *i;
	i=(struct info*)malloc(sizeof(struct info));

	printf("input student number:");
	scanf("%s",i->num);
	printf("input student name:");
	scanf("%s",i->name);
	printf("input student adress:");
	scanf("%s",i->adress);
 ? ?printf("input sex:");
	scanf("%s",i->sex);

	(i->next)=fPtr;
	if(fPtr!=0){
 ?(fPtr->last)=i;
	}
	fPtr=i;
	return fPtr;
}


struct info* del(char *s,struct info *fPtr)
{
	struct info *r;
	r=search(s, fPtr);
	if(r==0){
 ?printf("Wrong\n\n\n");
 ?return fPtr;
	}
	else{
 ?if(r==fPtr){
 ?	fPtr=fPtr->next;
 ? ? ?free(r);
 ?	printf("OK\n\n\n");
 ?	return fPtr;
 ?}
 ?((r->last)->next)=(r->next);
 ?free(r);
 ?printf("OK\n\n\n");
 ?return fPtr;
	}
}


struct info *search(char *s,struct info *fPtr)
{
	int r;
	if(fPtr==0){
 ?return 0;
	}
	while(1){
 ?r=strcmp(fPtr->num, s);
 ?if(r==0){
 ?	return fPtr;
 ? ? ? ?}
 ?r=strcmp(fPtr->name,s);
 ? ? ? ?if(r==0){
 ?	return fPtr;
 ? ? ? ?}
 ?if((fPtr->next)!=0){
 ?	fPtr=(fPtr->next);
 ?}
 ?else{
 ?	return 0;
 ?}
	}
}


struct info* deleteMenu(struct info *fPtr)
{
	char nn[20];
	printf("input the student's Name or Number:");
	scanf("%s",nn);
	return del(nn,fPtr);
}


void searchMenu(struct info *fPtr)
{
	char nn[2];
	struct info *r;
	printf("input the student's Name or Number:");
	scanf("%s",nn);
	r=search(nn,fPtr);
	if(r==0){
 ?printf("Wrong\n\n\n");
 ?return;
	}
	else{
 ?printf("Number:%s Name:%s Adress:%s sex:%s\n\n\n",r->num,r->name,r->adress,r->sex);
	}
}


void saveData(struct info *fPtr)
{
	if(fPtr==0){
 ?printf("There are nothing to save!\n\n\n");
 ?return;
	}
	FILE *fp;
	fp=fopen("info.stu","wb+");
 ? ?while(1){
 ?fwrite(fPtr,sizeof(struct info),1,fp);
 ?fPtr=(fPtr->next);
 ?if(fPtr==0){
 ?	break;
 ?}
	}
	printf("Save is OK\n");
	fclose(fp);
}


struct info* loadData(struct info *fPtr)
{
	struct info *now=0;
	struct info *up=0;
	FILE *fp;
	if((fp=fopen("info.stu","rb"))==NULL){
 ?printf("can't open file");
 ?return fPtr;
	}
 ? ?fPtr=(struct info*)malloc(sizeof(struct info));
	up=fPtr;
	fread(fPtr,sizeof(struct info),1,fp);
	if((fPtr->next)==0){
 ?printf("Load data is OK!\n\n\n");
 ?return fPtr;
	}
	while(1){
 ? ? now=(struct info*)malloc(sizeof(struct info));
 ? ? ? ?fread(now,sizeof(struct info),1,fp);
 ?(now->last)=up;
 ?(up->next)=now;
 ?up=now;
 ?if((now->next)==0){
 ?	fclose(fp);
 ?	printf("Load data is OK!\n\n\n");
 ?	return fPtr;
 ?}
	}
}


void showAll(struct info *fPtr)
{
	if(fPtr==0){
 ?printf("There are noting to show!\n\n\n");
 ?return;
	}
	while(1){
 ?printf("Number:%s Name:%s Adress:%s sex:%s\n-----------------------------\n",fPtr->num,fPtr->name,fPtr->adress,fPtr->sex);
 ?fPtr=fPtr->next;
 ?if(fPtr==0){
 ?	printf("Show all is OK!\n\n\n");
 ?	return;
 ?}
	}
}

void author()
{
	printf("***********************************************\n");
	printf("* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *\n");
	printf("* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1.0 ?*\n");
	printf("***********************************************\n\n\n");
}

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Some corrections in your program's english (i post this because of your sig ;))

There are noting to show -> should be -> There *is* *nothing* to show

There are nothing to save -> should be -> There *is* nothing to save

input your choose -> should be -> input your choice

and instead of "Data Load is Ok!" you should consider writing something like just "Data loaded" or something like that. Same for the save. just "Data saved".

I can't really explain why it is "There is" instead of "There are" as I am not british or american or from any country where the first language is english. Maybe someone else can provide that explanation...

As for the bug in your program, sorry but I can't really help. I know a bit of C, but not that much...

Link to comment
Share on other sites

  • 0
Some corrections in your program's english (i post this because of your sig ;))

There are noting to show -> should be -> There *is* *nothing* to show

There are nothing to save -> should be -> There *is* nothing to save

input your choose -> should be -> input your choice

and instead of "Data Load is Ok!" you should consider writing something like just "Data loaded" or something like that. Same for the save. just "Data saved".

I can't really explain why it is "There is" instead of "There are" as I am not british or american or from any country where the first language is english. Maybe someone else can provide that explanation...

As for the bug in your program, sorry but I can't really help. I know a bit of C, but not that much...

Thank you all the same

Link to comment
Share on other sites

  • 0

I'm guessing that won't compile at all, correct? The problem is that you are using C++ (and C99, but TC2.0 will only support C89).

All variables have to be declared before any functions are called in C89. You also need to declare your main() as int, and return 0 at the end of it.

Example:

void main()

{

author();

struct info *first=0;

int choose;

while(1){

is not valid C. The proper syntax would be:
int main(void)

{

    struct info *first=0;

    int choose;

    author();

    while(1) {

The same goes for

void saveData(struct info *fPtr)

{

if(fPtr==0){

printf("There are nothing to save!\n\n\n");

return;

}

FILE *fp;

which should be:
void saveData(struct info *fPtr)

{

    FILE *fp;

    if(fPtr==0) {

        printf("There are nothing to save!\n\n\n");

        return;

    }

Declare your variables before calling any functions, and it should work fine. You should try setting Visual C++ to "Compile as C code", as it defaults to C++. In Visual Studio .NET this is under "C/C++, Advanced"

Link to comment
Share on other sites

  • 0
I'm guessing that won't compile at all, correct? The problem is that you are using C++ (and C99, but TC2.0 will only support C89).

All variables have to be declared before any functions are called in C89. You also need to declare your main() as int, and return 0 at the end of it.

Example:

is not valid C. The proper syntax would be:

The same goes for

which should be:

Declare your variables before calling any functions, and it should work fine. You should try setting Visual C++ to "Compile as C code", as it defaults to C++. In Visual Studio .NET this is under "C/C++, Advanced"

Oh, you are right . Thank you.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.