c - 为什么编译器显示错误 "expected expression before stud"?

标签 c

这个程序预计会做一些事情: 1.使用ID添加新学生 2.通过ID查找学生 3. 按姓名搜索学生 4.列出所有学生 5.显示80分及以上学生名单

这就是我到目前为止所做的

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 200
int i;
int id;

/*Declaration of structures*/
typedef struct student{
    char studID[20];
    char name[50];
    char address[80];
    char tel_no[15];
    float marks; 
} stud;

int add_Name ();
int menu_Display ();
int get_Menu ();
int get_Option ();
int stud_id();
int stud_Name();

int main() {
    menu_Display ();
    get_Menu ();
    get_Option ();
    getchar();

    return 0;
}

int menu_Display () {
    int choice=0;
    while (choice!=6) {
        get_Menu ();
        choice = get_Option();
    }
}

int get_Menu (){
    int i;
    for (i=0; i<1; i++) {
        printf ("\t1. Insert new student\n\n");
        printf ("\t2. Search for a particular student by ID\n\n");
        printf ("\t3. Search for a particular student by name\n\n");
        printf ("\t4. Display a list of all students \n\n");
        printf ("\t5. Display a list of students achieving 80.0 marks and above\n\n");
        printf ("\t6. Exit\n\n");
    }
}

int get_Option () {
    int option;
    printf ("Please enter your option...");
    scanf ("%d", &option);
}

int menu_Option (int option) {
    int i;
    switch (option) {
        case 1 : add_ID();
        break;
        case 2 : stud_id();
        break;
        case 3 : stud_Name();
        break;
        case 4 : list_students();
        break;
        case 5 : students_marks();
        break;
        case 6 : printf("Exit\n");
        break;
        default : printf ("Wrong input");
        i=getchar();
        break;
    }
}

int add_ID () {
    int i;
    struct student stud[200];
    for (i=0; i<1; i++){
        strcpy(stud[i].studID,"ID00");

        printf("Enter Name:\n");
        scanf("%s", &(stud[i].name)) ;

        printf("Enter address:\n");
        scanf("%s", &(stud[i].address));

        printf("Enter telephone number:\n");
        scanf("%s", &(stud[i].tel_no));

        printf("Enter marks:\n"); 
        scanf("%f", &(stud[i].marks));
    }      
}

int stud_Name() {
    int i;
    int result;
    char name[20];

    printf("Please enter the name of the student\n");
    gets(name);

    for (i = 0; i < MAX; i++) {
        if ((result = strcmp(stud[i].name, name)) == 0){
            printf("%s \t %s \t %s \t %s \t %.2f \n", stud[i].studID, stud[i].name, stud[i].address, stud[i].tel_no, stud[i].marks);
        }

    if (i == MAX) {
        printf ("Match not found\n");
    }

    i=getchar();
    }
}

int stud_id() {
    int i;
    int result;
    char ID[20];

    printf("Please enter the Student ID which you are searching for\n");
    gets(ID);

    for (i = 0; i < MAX; i++) {
        if ((result = strcmp(stud[i].studID, ID)) == 0){
            printf("%s \t %s \t %s \t %s \t %.2f \n", stud[i].studID, stud[i].name, stud[i].address, stud[i].tel_no, stud[i].marks);
        }

    if (i == MAX) {
        printf ("Match not found\n");
    }

    i=getchar();
    }
}

int list_Students(){
    printf("Student ID \t Name \t Address \t Telephone number \t Marks \n");

    for (i = 0; i < MAX; i++) {
        printf("%s \t %s \t %s \t %s \t %.2f \n", stud[i].studID, stud[i].name, stud[i].address, stud[i].tel_no, stud[i].marks);

    }   
}

我在尝试编译程序时遇到了一些错误。 显示的错误是函数 stud_Namestud_idlist_Students 中的“‘stud’之前的预期表达式”。我尝试修改代码但没有任何效果。我使用 strcmp 的方式正确吗?谢谢

最佳答案

您已使用 typedef 将结构命名为 studtypedef 不会创建变量,它允许您创建不带单词 struct 的变量。

例如:

typedef struct NameOfStruct { } ShortName;
struct NameOfStruct var1; // Creates variable named var1 
ShortName var2; // Creates variable named var2

您似乎想使用 stud 作为全局变量。在这种情况下,您应该从 add_ID() 中删除行 struct Student Stud[200];

您可以像这样创建全局变量stud(请注意,这不使用单词typedef):

struct student{
    char studID[20];
    char name[50];
    char address[80];
    char tel_no[15];
    float marks; 
} stud[200];

关于c - 为什么编译器显示错误 "expected expression before stud"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971089/

相关文章:

c - varargs 是否提供了一种穷人的多态性?

c# - 像在 C 中一样在 C# 中使用 struct - 可以吗?

C编程指针和char数组问题

c - 使用 goto 时程序运行不一样

c - 解释程序的输出

c - TCP Linux 服务器在第一次接受后锁定

c - AVX - 将 __256 vector 存储回 C 中的内存 (void**),

c - Makefile 对多个文件的依赖

c++ - malloc 和 New 中堆初始化的混淆

c - 构建驱动程序产生 `error: void value not ignored as it ought to be`