Why does my program keep freezing when I want to print the scanned person?
(Programmed in Visual Basic C, Windows 7 64-bit)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct {
int pernum;
char name[20];
}TPerson;
TPerson* addPerson();
void scanP(TPerson *person);
void printP(TPerson *personlist[], int x);
void deleteP();
void modifyP();
void main()
{
int exit, x = 0;
char ans;
do {
TPerson *personlist[100] = {0};
system("cls");
printf("1 scan \n");
printf("2 print \n");
printf("3 delete \n");
printf("4 modify \n");
printf("5 exit \n");
ans = getche();
switch(ans) {
case '1': personlist[x] = addPerson(); x++;
break;
case '2': printP(personlist, x);
getch();
break;
case '3': deleteP();
break;
case '4': modifyP();
break;
case '5': exit = ans;
}
}while(exit != ans);
}
TPerson* addPerson() {
TPerson *person;
person = (TPerson *)malloc(sizeof(TPerson));
scanP(person);
return person;
}
void scanP(TPerson *person) {
printf("\nWrite a number:");
scanf("%d", &person->pernum);
fflush(stdin);
printf("\nWrite a name: ");
gets(person->name);
}
void printP(TPerson *personlist[], int x) {
int c;
printf("%d\n",x);
for(c = 0; c < x; c++) {
printf("\n%d %s", personlist[c]->pernum, personlist[c]->name);
}
system("pause");
}
void deleteP() {
}
void modifyP() {
}
|
Last edited on
Yes, I meant Visual C. I tried to run my program without system("pause"); and it still freezes.
You are declaring
TPerson *personlist[100] = {0};
each time through your loop. . .
Oh my gosh, thanks PCrumley48! I didn't notice it:) Problem is solved, thanks for the answers.