C中未定义的符号st(学生记录系统)

问题描述 投票:-4回答:4

当我尝试运行以下代码时,它会生成错误:

未定义的符号

当我尝试显示完整的学生记录时,它向我显示选项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();
}
c
4个回答
3
投票

问题是你的声明是在错误的地方。

if(option==1)
{
    struct student st[9];
    ...
}

此声明仅在if(option==1)子句中可见,但您尝试在else if(option == 2)中使用它

我猜你应该把声明移到程序的开头

void main()
{
    int option, i;
    struct student st[9];

您应该阅读有关使用变量时重要的几个概念,范围是程序中可见变量的区域,以及变量存在的时间范围。你写的代码都错了。

您的代码中还有很多其他错误,但我想您会在很长一段时间内发现这些错误。


1
投票

struct student st[9];被限制在option等于1的范围内,因此st超出了if块的其他部分的范围,因此编译器诊断。

main的开头声明它,就像你为option做的那样。

最后,考虑从Turbo编译器迁移:从那时起,标准已经发生了很大的变化,而你只是养成了坏习惯。


1
投票

struct student st[9];if区块中的局部变量,这在else区块中不可用,并且您尝试使用它。将声明移到if上方,使两个块中的st数组可用。


1
投票

这是因为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
© www.soinside.com 2019 - 2024. All rights reserved.