【发布时间】:2020-01-19 13:52:51
【问题描述】:
您好,很抱歉再次提出,我可以看到这里有很多类似的问题,但我仍然无法理解我做错了什么。 我的任务是为学生信息编写一个简单的数据库,具有动态内存分配和结构。 我的问题是我不明白如何创建动态分配的数组,在其上写入信息以及如何进一步传递它以读取它并在另一个函数的屏幕上显示。 我尝试编写一些代码,它甚至可以工作,但是一天过去了,我尝试运行它并出现了问题,编译器没有显示任何重大问题,但它只是在输入第一个值(学生 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;
}
【问题讨论】:
-
if(k>0) {realloc(st,k*sizeof(student));这期望st指向一个使用malloc或类似分配的有效内存块。实际上,st是一个包含随机垃圾的未初始化变量。因此您的程序表现出未定义的行为。 -
如果您的代码是 C++ 而不是 C,请使用
std::vector -
您可以使用
std::string代替char[] -
您的 C 风格字符串有问题。当您说
char ID[7];时,该数组有 7 个元素,唯一有效的索引是 0-6。由于 C 字符串以 NULL 结尾,因此只有 6 个字符和 NULL 的空间。您不能将 7 位 id 放入该数组,也不能编写像if (st->ID[7]!='\0')这样的代码,因为 7 不是有效索引。您的所有其他字符串也有同样的问题。停止这样做,只使用std::string(和std::vector)。 -
选择一种语言,C 或 C++,并删除另一种的标记。