[抱歉,再次提出该问题,我可以看到这里有很多类似的问题,但是我仍然不明白我在做什么错。我的任务是为学生信息编写一个简单的数据库,其中包含动态内存分配和结构。我的问题是我不了解如何创建动态分配的数组,在其上写信息以及如何进一步传递它以读取它以及在另一个函数中在屏幕上显示。我已经尝试编写一些代码,甚至可以工作,但是有一天,我尝试运行它,但问题出现了,编译器没有显示任何重大问题,但是在输入第一个值(学生ID)后才停止。请帮助我解决我做错了的事,我不知道该怎么办。
这是我的代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
struct student{
char ID[7];
char name[20];
char surename[20];
char addres[20];
char phone_num[9];
};
//inputs with arrays overflow protection
student new_student(int k)
{
student *st,*stt;
if(k>0)
{
realloc(st,k*sizeof(student));
}
else
{
st=(student*)malloc(sizeof(student));
if (st==NULL)
{
printf("\nERROR\nnot enough memory");
exit(1);
}
}
system("cls");
//ID
while (1)
{
fflush(stdin);
printf("\nenter 7 digit student ID: ");
gets(st->ID);
//check the entered ID
if (st->ID[7]!='\0')
{
printf("\nentered ID is to long\n");
getch();
system("cls");
st->ID[7]='\0';
}
else if(st->ID[6]=='\0')
{
printf("\nentered ID is to short\n");
getch();
system("cls");
st->ID[6]='\0';
}
else
{
break;
}
}
//NAME
while (1)
{
fflush(stdin);
printf("\nenter student's name: ");
gets(st->name);
if (st->name[20]!='\0')
{
printf("\nentered name is to long\n");
getch();
system("cls");
st->name[20]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st->ID[i]);
printf("\n");
}
else
{
break;
}
}
//SURENAME
while (1)
{
fflush(stdin);
printf("\nenter student's surename: ");
gets(st->surename);
if (st->surename[20]!='\0')
{
printf("\nentered surename is to long\n");
getch();
system("cls");
st->surename[20]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st->ID[i]);
printf("\n");
printf("\nenter student's name: ");
for (int i=0;i<20;i++)
printf("%c",st->name[i]);
printf("\n");
}
else
{
break;
}
}
//ADDRES
while (1)
{
fflush(stdin);
printf("\nenter student's addres: ");
gets(st->addres);
if (st->addres[20]!='\0')
{
printf("\nentered addres is to long\n");
getch();
system("cls");
st->addres[20]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st->ID[i]);
printf("\n");
printf("\nenter student's name: ");
for (int i=0;i<20;i++)
printf("%c",st->name[i]);
printf("\n");
printf("\nenter student's surename: ");
for (int i=0;i<20;i++)
printf("%c",st->surename[i]);
printf("\n");
}
else
{
break;
}
}
//PHONE
while (1)
{
fflush(stdin);
printf("\nenter student's phone number: ");
gets(st->phone_num);
if (st->phone_num[9]!='\0')
{
printf("\nentered phone number is to long\n");
getch();
system("cls");
st->phone_num[9]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st->ID[i]);
printf("\n");
printf("\nenter student's name: ");
for (int i=0;i<20;i++)
printf("%c",st->name[i]);
printf("\n");
printf("\nenter student's surename: ");
for (int i=0;i<20;i++)
printf("%c",st->surename[i]);
printf("\n");
printf("\nenter student's addres: ");
for (int i=0;i<20;i++)
printf("%c",st->addres[i]);
printf("\n");
}
else if(st->phone_num[8]=='\0')
{
printf("\nentered phone number is to short\n");
getch();
system("cls");
st->phone_num[8]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st->ID[i]);
printf("\n");
printf("\nenter student's name: ");
for (int i=0;i<20;i++)
printf("%c",st->name[i]);
printf("\n");
printf("\nenter student's surename: ");
for (int i=0;i<20;i++)
printf("%c",st->surename[i]);
printf("\n");
printf("\nenter student's addres: ");
for (int i=0;i<20;i++)
printf("%c",st->addres[i]);
printf("\n");
}
else
{
break;
}
}
return *st;
}
void search_student ()
{
;
}
int main(int argc, char** argv)
{
while (1)
{
system("cls");
int choice,st_counter=0;
printf("===MENU===\n");
printf("1. Add Student\n");
printf("2. Search Student\n");
printf("3. Modify Student Record\n");
printf("4. Generate Marksheet\n");
printf("5. Delete Student Record\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
st_counter++;//increase array size every time entering new student's data
new_student(st_counter);
break;
case 2:
void search_student ();
break;
case 3:
break;
case 4:
//this will print to file later
break;
case 5:
break;
case 6:
return 0;
break;
default:
break;
}
}
return 1;
}
好吧,考虑到您编写的内容,我对代码做了一些更改。我希望它更接近正确。
student new_student(int k)
{
student *st;
if(k>1)
{
realloc(st,k*sizeof(student));
}
else
{
st=(student*)malloc(sizeof(student));
if (st==NULL)
{
printf("\nERROR\nnot enough memory");
exit(1);
}
}
system("cls");
k-=1;
//ID
while (1)
{
fflush(stdin);
st[k].ID[6]='\0';
st[k].ID[7]='\0';
printf("\nenter 7 digit student ID: ");
gets(st[k].ID);
//check the entered ID
if (st[k].ID[7]!='\0')
{
printf("\nentered ID is to long\n");
getch();
system("cls");
st[k].ID[7]='\0';
}
else if(st[k].ID[6]=='\0')
{
printf("\nentered ID is to short\n");
getch();
system("cls");
st[k].ID[6]='\0';
}
else
{
break;
}
}
//NAME
while (1)
{
fflush(stdin);
printf("\nenter student's name: ");
gets(st[k].name);
if (st[k].name[20]!='\0')
{
printf("\nentered name is to long\n");
getch();
system("cls");
st[k].name[20]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st[k].ID[i]);
printf("\n");
}
else
{
break;
}
}
//SURENAME
while (1)
{
fflush(stdin);
printf("\nenter student's surename: ");
gets(st[k].surename);
if (st[k].surename[20]!='\0')
{
printf("\nentered surename is to long\n");
getch();
system("cls");
st[k].surename[20]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st[k].ID[i]);
printf("\n");
printf("\nenter student's name: ");
for (int i=0;i<20;i++)
printf("%c",st[k].name[i]);
printf("\n");
}
else
{
break;
}
}
//ADDRES
while (1)
{
fflush(stdin);
printf("\nenter student's addres: ");
gets(st[k].addres);
if (st[k].addres[20]!='\0')
{
printf("\nentered addres is to long\n");
getch();
system("cls");
st[k].addres[20]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st[k].ID[i]);
printf("\n");
printf("\nenter student's name: ");
for (int i=0;i<20;i++)
printf("%c",st[k].name[i]);
printf("\n");
printf("\nenter student's surename: ");
for (int i=0;i<20;i++)
printf("%c",st[k].surename[i]);
printf("\n");
}
else
{
break;
}
}
//PHONE
while (1)
{
fflush(stdin);
printf("\nenter student's phone number: ");
gets(st[k].phone_num);
if (st[k].phone_num[9]!='\0')
{
printf("\nentered phone number is to long\n");
getch();
system("cls");
st[k].phone_num[9]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st[k].ID[i]);
printf("\n");
printf("\nenter student's name: ");
for (int i=0;i<20;i++)
printf("%c",st[k].name[i]);
printf("\n");
printf("\nenter student's surename: ");
for (int i=0;i<20;i++)
printf("%c",st[k].surename[i]);
printf("\n");
printf("\nenter student's addres: ");
for (int i=0;i<20;i++)
printf("%c",st[k].addres[i]);
printf("\n");
}
else if(st[k].phone_num[8]=='\0')
{
printf("\nentered phone number is to short\n");
getch();
system("cls");
st[k].phone_num[8]='\0';
printf("\nenter 7 digit student ID: ");
for (int i=0;i<7;i++)
printf("%c",st[k].ID[i]);
printf("\n");
printf("\nenter student's name: ");
for (int i=0;i<20;i++)
printf("%c",st[k].name[i]);
printf("\n");
printf("\nenter student's surename: ");
for (int i=0;i<20;i++)
printf("%c",st[k].surename[i]);
printf("\n");
printf("\nenter student's addres: ");
for (int i=0;i<20;i++)
printf("%c",st[k].addres[i]);
printf("\n");
}
else
{
break;
}
}
return *st;
}