gpt4 book ai didi

c - 动态分配数组结构的数组

转载 作者:行者123 更新时间:2023-11-30 21:04:46 27 4
gpt4 key购买 nike

嗨,很抱歉再次提出这个问题,我可以看到这里有很多类似的问题,但我仍然不明白我做错了什么。我的任务是为学生信息编写一个简单的数据库,具有动态内存分配和结构。我的问题是我不明白如何创建动态分配的数组、在其上写入信息以及如何进一步传递它以读取它并在另一个函数中显示在屏幕上。我尝试编写一些代码,它甚至起作用了,但是有一天,我尝试运行它,问题出现了,编译器没有显示任何重大问题,但它在输入第一个值(学生 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;
}

关于c - 动态分配数组结构的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59810670/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com