在“[”标记 C PROGRAM (DEV C++) 之前出现错误预期主表达式
ERROR expected primary-expression before '[' token C PROGRAM (DEV C++)
我刚刚完成了这个简单的任务,如您所见,它还没有完成,但我遇到了这个问题
65 17 [Error] expected primary-expression before '[' token
69 23 [Error] expected primary-expression before '[' token
下面是我的代码;我看不出问题,你能帮帮我吗?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void viewAll();
void viewStd();
int student[4][5] = {{1232 , 10 , 23 , 45 , 56 }, //global
{2343 , 45 , 43 , 24 , 78 },
{2345 , 34 , 45 , 45 , 45 },
{3423 , 67 , 6 , 65 , 56 }};
int main()
{
int choice;
system("CLS");
printf("===========================");
printf("\n\t MENU\n");
printf("===========================");
printf("\n1. View all students records\n2. View a student records by ID\n3. Show the highest and the lowest final scores");
printf("\n\n Please enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: viewAll();break;
case 2: viewStd();break;
/*case 3: HLscore();break;*/
default: printf("Input not recognized");
}
getch();
}
void viewAll()
{
char choice;
system("CLS");
printf("=========================================================");
printf("\n|StudentID | Quiz1 | Quiz2 | Mid-Term | Final |\n");
printf("=========================================================\n");
printf("| 1232 | 10 | 23 | 45 | 56 |\n");
printf("| 2343 | 45 | 43 | 24 | 78 |\n");
printf("| 2345 | 34 | 45 | 45 | 45 |\n");
printf("| 3423 | 67 | 6 | 65 | 56 |\n");
printf("\n\nPress (R) to return, press (E) to exit: ");
scanf("%s", &choice);
switch(choice)
{
case 'R': main();break;
case 'E': exit(1);break;
default: printf("Input not recognized");
}
}
void viewStd()
{
int id;
printf("\n\nPlease enter ID: ");
scanf("%d", &id);
for (int i = 0; i < 4; ++i)
{
if (id == std[i][0])
{
for (int j = 0; j < 5; ++j)
{
printf("%d ", std[i][j]);
}
}
}
}
最终修改:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void viewAll();
void viewStd();
void HLscore();
int student[4][5]={{1232 , 10 , 23 , 45 , 56 }, //global
{2343 , 45 , 43 , 24 , 78 },
{2345 , 34 , 45 , 45 , 45 },
{3423 , 67 , 6 , 65 , 56 }};
int main()
{
int choice;
system("CLS");
printf("===========================");
printf("\n\t MENU\n");
printf("===========================");
printf("\n1. View all students records\n2. View a student records by ID\n3. Show the highest and the lowest final scores");
printf("\n\n Please enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: viewAll();break;
case 2: viewStd();break;
case 3: HLscore();break;
default: printf("Input not recognized");
}
getch();
}
void viewAll()
{
char choice;
system("CLS");
printf("=========================================================");
printf("\n|StudentID | Quiz1 | Quiz2 | Mid-Term | Final |\n");
printf("=========================================================\n");
printf("| 1232 | 10 | 23 | 45 | 56 |\n");
printf("| 2343 | 45 | 43 | 24 | 78 |\n");
printf("| 2345 | 34 | 45 | 45 | 45 |\n");
printf("| 3423 | 67 | 6 | 65 | 56 |\n");
printf("\n\nPress (R) to return, press (E) to exit: ");
scanf("%s",&choice);
switch(choice){
case 'R': main();break;
case 'E': exit(1);break;
default:printf("Input not recognized");
}
}
void viewStd()
{
int id;
char choice;
printf("\n\nPlease enter ID: ");
scanf("%d",&id);
printf("=========================================================");
printf("\n|StudentID | Quiz1 | Quiz2 | Mid-Term | Final |\n");
printf("=========================================================\n");
for (int i=0; i<4; ++i)
{
if (id == student[i][0])
{
for (int j=0; j<5; ++j)
{
printf(" %d ",student[i][j]);
}
printf("\n_________________________________________________________");
}
}
printf("\n\n To search for other students press (S), To return press (R), To exit press (E): ");
scanf("%s",&choice);
switch(choice){
case 'S': viewStd();break;
case 'R': main();break;
case 'E': exit(1);break;
default: printf("ERROR");}
}
void HLscore()
{
int max,min;
char choice;
max=student[0][1];
for(int i=0; i<4; ++i)
{
for(int j=1; j<5; ++j)
{
if(student[i][j]>max)
{
max=student[i][j];
}
}
}
min=student[0][1];
for(int i=0; i<4; ++i)
{
for(int j=1; j<5; ++j)
{
if(student[i][j]<min)
{
min=student[i][j];
}
}
}
printf("\nHighest mark is %d",max);
printf("\nLowest mark is %d",min);
printf("\n\nPress (R) to return, press (E) to exit: ");
scanf("%s",&choice);
switch(choice){
case 'R': main();break;
case 'E': exit(1);break;
default:printf("Input not recognized");
}
}
您尝试通过 id == std[i][0]
访问 std
,但我没有在任何地方看到 std 声明。 (你是说 student
吗?)
我刚刚完成了这个简单的任务,如您所见,它还没有完成,但我遇到了这个问题
65 17 [Error] expected primary-expression before '[' token
69 23 [Error] expected primary-expression before '[' token
下面是我的代码;我看不出问题,你能帮帮我吗?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void viewAll();
void viewStd();
int student[4][5] = {{1232 , 10 , 23 , 45 , 56 }, //global
{2343 , 45 , 43 , 24 , 78 },
{2345 , 34 , 45 , 45 , 45 },
{3423 , 67 , 6 , 65 , 56 }};
int main()
{
int choice;
system("CLS");
printf("===========================");
printf("\n\t MENU\n");
printf("===========================");
printf("\n1. View all students records\n2. View a student records by ID\n3. Show the highest and the lowest final scores");
printf("\n\n Please enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: viewAll();break;
case 2: viewStd();break;
/*case 3: HLscore();break;*/
default: printf("Input not recognized");
}
getch();
}
void viewAll()
{
char choice;
system("CLS");
printf("=========================================================");
printf("\n|StudentID | Quiz1 | Quiz2 | Mid-Term | Final |\n");
printf("=========================================================\n");
printf("| 1232 | 10 | 23 | 45 | 56 |\n");
printf("| 2343 | 45 | 43 | 24 | 78 |\n");
printf("| 2345 | 34 | 45 | 45 | 45 |\n");
printf("| 3423 | 67 | 6 | 65 | 56 |\n");
printf("\n\nPress (R) to return, press (E) to exit: ");
scanf("%s", &choice);
switch(choice)
{
case 'R': main();break;
case 'E': exit(1);break;
default: printf("Input not recognized");
}
}
void viewStd()
{
int id;
printf("\n\nPlease enter ID: ");
scanf("%d", &id);
for (int i = 0; i < 4; ++i)
{
if (id == std[i][0])
{
for (int j = 0; j < 5; ++j)
{
printf("%d ", std[i][j]);
}
}
}
}
最终修改:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void viewAll();
void viewStd();
void HLscore();
int student[4][5]={{1232 , 10 , 23 , 45 , 56 }, //global
{2343 , 45 , 43 , 24 , 78 },
{2345 , 34 , 45 , 45 , 45 },
{3423 , 67 , 6 , 65 , 56 }};
int main()
{
int choice;
system("CLS");
printf("===========================");
printf("\n\t MENU\n");
printf("===========================");
printf("\n1. View all students records\n2. View a student records by ID\n3. Show the highest and the lowest final scores");
printf("\n\n Please enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: viewAll();break;
case 2: viewStd();break;
case 3: HLscore();break;
default: printf("Input not recognized");
}
getch();
}
void viewAll()
{
char choice;
system("CLS");
printf("=========================================================");
printf("\n|StudentID | Quiz1 | Quiz2 | Mid-Term | Final |\n");
printf("=========================================================\n");
printf("| 1232 | 10 | 23 | 45 | 56 |\n");
printf("| 2343 | 45 | 43 | 24 | 78 |\n");
printf("| 2345 | 34 | 45 | 45 | 45 |\n");
printf("| 3423 | 67 | 6 | 65 | 56 |\n");
printf("\n\nPress (R) to return, press (E) to exit: ");
scanf("%s",&choice);
switch(choice){
case 'R': main();break;
case 'E': exit(1);break;
default:printf("Input not recognized");
}
}
void viewStd()
{
int id;
char choice;
printf("\n\nPlease enter ID: ");
scanf("%d",&id);
printf("=========================================================");
printf("\n|StudentID | Quiz1 | Quiz2 | Mid-Term | Final |\n");
printf("=========================================================\n");
for (int i=0; i<4; ++i)
{
if (id == student[i][0])
{
for (int j=0; j<5; ++j)
{
printf(" %d ",student[i][j]);
}
printf("\n_________________________________________________________");
}
}
printf("\n\n To search for other students press (S), To return press (R), To exit press (E): ");
scanf("%s",&choice);
switch(choice){
case 'S': viewStd();break;
case 'R': main();break;
case 'E': exit(1);break;
default: printf("ERROR");}
}
void HLscore()
{
int max,min;
char choice;
max=student[0][1];
for(int i=0; i<4; ++i)
{
for(int j=1; j<5; ++j)
{
if(student[i][j]>max)
{
max=student[i][j];
}
}
}
min=student[0][1];
for(int i=0; i<4; ++i)
{
for(int j=1; j<5; ++j)
{
if(student[i][j]<min)
{
min=student[i][j];
}
}
}
printf("\nHighest mark is %d",max);
printf("\nLowest mark is %d",min);
printf("\n\nPress (R) to return, press (E) to exit: ");
scanf("%s",&choice);
switch(choice){
case 'R': main();break;
case 'E': exit(1);break;
default:printf("Input not recognized");
}
}
您尝试通过 id == std[i][0]
访问 std
,但我没有在任何地方看到 std 声明。 (你是说 student
吗?)