Seraj Sersawi Seraj Sersawi - 7 days ago 4
C Question

ERROR expected primary-expression before '[' token C PROGRAM (DEV C++)

I've just had this simple assignment and it's not complete yet as you see, but I have faced this problem


65 17 [Error] expected primary-expression before '[' token

69 23 [Error] expected primary-expression before '[' token


This is my code below; I can't see the problem could you please help me?

#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]);
}
}
}
}

Answer

You try to access std via id == std[i][0], But I don't see std declared anywhere. (Did you mean student?)