当我尝试运行以下代码时,它会生成错误:
未定义的符号
当我尝试显示完整的学生记录时,它向我显示选项2的错误。
我在turbo C ++编译器上运行它。
void main()
{
int option, i;
while (5)
{
printf("========== Student Database ==========\n");
printf("--------------------------------------\n");
printf("1. Insert Record\n");
printf("2. Display Record\n");
printf("3. Edit/Update Record\n");
printf("4. Delete a Record\n");
printf("5. Exit\n");
printf("--------------------------------------\n");
printf("Enter Your Choice: ");
scanf("%d",&option);
if(option==1)
{
struct student st[9];
{
printf("\student data");
}
clrscr();
break;
}
else if(option==2)
{
printf("\n===== Displaying Student Information =====\n");
printf("\n Roll No: \t Name \t \t \t Marks \t Mobile Number\n");
for (i = 0; i < 9; ++i)
{
printf("\n %d \t %st \t \t \t %d \t %d\n", st[i].roll, st[i].name, st[i].marks, st[i].number);
}
clrscr();
break;
}
getch();
}
问题是你的声明是在错误的地方。
if(option==1)
{
struct student st[9];
...
}
此声明仅在if(option==1)
子句中可见,但您尝试在else if(option == 2)
中使用它
我猜你应该把声明移到程序的开头
void main()
{
int option, i;
struct student st[9];
您应该阅读有关使用变量时重要的几个概念,范围是程序中可见变量的区域,以及变量存在的时间范围。你写的代码都错了。
您的代码中还有很多其他错误,但我想您会在很长一段时间内发现这些错误。
struct student st[9];
被限制在option
等于1的范围内,因此st
超出了if
块的其他部分的范围,因此编译器诊断。
在main
的开头声明它,就像你为option
做的那样。
最后,考虑从Turbo编译器迁移:从那时起,标准已经发生了很大的变化,而你只是养成了坏习惯。
struct student st[9];
是if
区块中的局部变量,这在else
区块中不可用,并且您尝试使用它。将声明移到if
上方,使两个块中的st
数组可用。
这是因为st
的范围。在您的代码中,变量仅在if
块内有效,即它在else
块中不可用。因此,您会收到编译错误。
试试这个:
struct student st[9]; // Declare outside the if
if(option==1)
{
// struct student st[9]; Don't do it inside the if