Hi. Im new to programming and to this website and i need help with one of my assignments. The instructions for the assignment are to "write a program that uses an array of student structures to answer inquiries. Using a menu-driven user interface, provide inquiries that report a student's score, average, or grade. A fourth menu option provides all data for a requested student, and a fifth prints a list of student IDs and names". My code runs but when it does, i keep getting an error and i definitely dont know what's wrong with it. Please any help will be appreciated. oh and sorry that my code is so long...that's the only way that i know how to do it. Thanks.

Here's my code:

/*	Janice Flores
    COP 2220 - Online
    Summer 2008
    Prof. Enger
    Ch 12 - Project 40
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//	Global Declarations
	typedef struct
	   {
	    int  id[5];
	    char name[5];
	    int quiz1[5];
	    int quiz2[5];
	    int quiz3[5];
	    int quiz4[5];
	    int exam[5];
	   } STUDENT;

void menu(int option);
void studentAverage(double average);
char scoreToGrade (int score);
int studentid;

int main (void)
{
//	Local Declarations
	STUDENT student;
	int option, average, score;
   menu(option);
	
//	Statements
	student.id[0] = 1234;
	student.id[1] = 2134;
	student.id[2] = 3124;
	student.id[3] = 4532;
	student.id[4] = 4678;
	
	student.name[0] = "Julie Adams";
	student.name[1] = "Harry Smith";
	student.name[2] = "Tuan Nguyen";
	student.name[3] = "Jorge Gonzalez";
	student.name[4] = "Amanda Trapp";
	
	student.quiz1[0] = 52;
	student.quiz1[1] = 90;
	student.quiz1[2] = 100;
	student.quiz1[3] = 11;
	student.quiz1[4] = 20;
	
	student.quiz2[0] = 7;
	student.quiz2[1] = 36;
	student.quiz2[2] = 45;
	student.quiz2[3] = 17;
	student.quiz2[4] = 12;
	
	student.quiz3[0] =100;
	student.quiz3[1] = 90;
	student.quiz3[2] = 20;
	student.quiz3[3] = 81;
	student.quiz3[4] = 45;
	
	student.quiz4[0] = 78;
	student.quiz4[1] = 77;
	student.quiz4[2] = 90;
	student.quiz4[3] = 32;
	student.quiz4[4] = 78;
	
	student.exam[0] = 34;
	student.exam[1] = 30;
	student.exam[2] = 70;
	student.exam[3] = 77;
	student.exam[4] = 34;
	
	
	
	if (option = 1)
  {
	printf("\n\nEnter Student ID: ");
        scanf("%d", studentid);
        
      printf("Student's Name  Quiz 1  Quiz 2  Quiz 3  Quiz 4  Exam\n");
      printf("==============  ======  ======  ======  ======  ====\n\n");
      
      if (studentid = student.id[0])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[0], 
	           student.quiz1[0],
	           student.quiz2[0],
	           student.quiz3[0],
	           student.quiz4[0],
	           student.exam[0]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[1])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[1], 
	           student.quiz1[1],
	           student.quiz2[1],
	           student.quiz3[1],
	           student.quiz4[1],
	           student.exam[1]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[2])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[2], 
	           student.quiz1[2],
	           student.quiz2[2],
	           student.quiz3[2],
	           student.quiz4[2],
	           student.exam[2]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[3])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[3], 
	           student.quiz1[3],
	           student.quiz2[3],
	           student.quiz3[3],
	           student.quiz4[3],
	           student.exam[3]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[4])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[4], 
	           student.quiz1[4],
	           student.quiz2[4],
	           student.quiz3[4],
	           student.quiz4[4],
	           student.exam[4]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[5])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[5], 
	           student.quiz1[5],
	           student.quiz2[5],
	           student.quiz3[5],
	           student.quiz4[5],
	           student.exam[5]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      }
	
	else if (option = 2)
	{
	printf("\n\nEnter Student ID: ");
    scanf("%d", studentid);
	studentAverage(average);
    }
	
	else if (option = 3)
	{
	printf("\n\nEnter Student ID: ");
    scanf("%d", studentid);
	scoreToGrade(score);
    }
	
	else if (option = 4)
	{
        printf("\n\nEnter Student ID: ");
        scanf("%d", studentid);
        
      printf("Student's Name  Quiz 1  Quiz 2  Quiz 3  Quiz 4  Exam\n");
      printf("==============  ======  ======  ======  ======  ====\n\n");
      
      if (studentid = student.id[0])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[0], 
	           student.quiz1[0],
	           student.quiz2[0],
	           student.quiz3[0],
	           student.quiz4[0],
	           student.exam[0]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[1])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[1], 
	           student.quiz1[1],
	           student.quiz2[1],
	           student.quiz3[1],
	           student.quiz4[1],
	           student.exam[1]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[2])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[2], 
	           student.quiz1[2],
	           student.quiz2[2],
	           student.quiz3[2],
	           student.quiz4[2],
	           student.exam[2]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[3])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[3], 
	           student.quiz1[3],
	           student.quiz2[3],
	           student.quiz3[3],
	           student.quiz4[3],
	           student.exam[3]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[4])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[4], 
	           student.quiz1[4],
	           student.quiz2[4],
	           student.quiz3[4],
	           student.quiz4[4],
	           student.exam[4]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      else if (studentid = student.id[5])
      {
      printf("%-26c %4d %4d %4d %4d %4d\n",
	           student.name[5], 
	           student.quiz1[5],
	           student.quiz2[5],
	           student.quiz3[5],
	           student.quiz4[5],
	           student.exam[5]);
	           studentAverage(average);
	           scoreToGrade(score);
            }
      }
	
	else 
	
	printf("%d\n", student.id[5]);
	printf("%c\n",  student.name[5]);

	system("pause");
	return 0;
}	// main

void menu(int option)

{
   printf("\t*MENU*\t\n\n");
   printf("1. View Student's Score\n");
   printf("2. View Student's Average\n");
   printf("3. View Student's Grade\n");
   printf("4. View Complete Student's Data\n");
   printf("5. View All Student IDs and Names\n\n");
   printf("Choose Option: ");
   scanf("%d", option);
}


void studentAverage(double average)
{   
        if (studentid = 1234)
        average = 54;
        else if (studentid = 2134)
        average = 65;
        else if (studentid = 3124)
        average = 51;
        else if (studentid = 4532)
        average = 44;
        else if (studentid = 5678)
        average = 38;
     
     printf("The student's average is: %.2f", average);
     }

char scoreToGrade (int score)
{
        
     	char grade;
     	double average;

     	average = score;
     	
        if (studentid = 1234)
        average = 54;
        else if (studentid = 2134)
        average = 65;
        else if (studentid = 3124)
        average = 51;
        else if (studentid = 4532)
        average = 44;
        else if (studentid = 5678)
        average = 38;	
     
        if (score >= 90)
	    grade = 'A';
	    else if (score >= 80)
	    grade = 'B';
	    else if (score >= 70)
	    grade = 'C';
	    else if (score >= 60)
	    grade = 'D';
	    else
	    grade = 'F';
    
    printf("The grade is: %c\n", grade);
}	// scoreToGrade
Ancient Dragon commented: Thanks for taking the time to use code tags :) +33

Recommended Answers

All 12 Replies

What you should have coded is an array of 5 STUDENT structures. Each structure holds the information for just one student

typedef struct
	   {
	    int  id[5];
	    char* name;
	    int quiz1;
	    int quiz2;
	    int quiz3;
	    int quiz4;
	    int exam;
	   } STUDENT;
int main()
{
    STUDENT students[5];

//	Statements
	student[0].id = 1234;
	student[1].id = 2134;
	student[2].id = 3124;
	student[3].id = 4532;
	student[4].id = 4678;


}

1. You want an array of structures, not a structure with arrays. Declare:

typedef struct /* One student <--> one struct object */
{
    int  id;
    char name[50]; /* char[5] for name length <= 4? too short */
    int quiz1;
    int quiz2;
    int quiz3;
    int quiz4;
    int exam;
} STUDENT;
/* Now for N students declare array of structures */
#define N 5
STUDENT student[N] = /* Initialize */
{
    { 1234, "Julie Adams", 52, 7, 100, 78, 34 },
    { ... next student data ... },
    ...
    { ... last student data... }, /* keep comma for future use */
};
...
/* Now for i-th student use index */
... student[i].name ... - pointer to name string of i-th student

Now write a function which returns index in student array by id, for example:

#define NOTFOUND (-1)
int findIndex(STUDENT* student, int maxN, int id)
{
    int i;
    for (i = 0; i < maxN && student[i].id != id, ++i)
         ;
    return i < maxN? i: NOTFOUND; /* return -1 if not found */
}
/* Use it: */
i = findIndex(student,N,studentid);
if (i == NOTFOUND)
{
    /* No such student, print message and continue */
}
else
{
    /* Now pointer to i-th student is student+i */
}

Rewrite your code totally with this constructs. You get 1/5 of existing...

can you give me an example of how i would do one student? sorry but i'm a little lost :(

ok i'm a little bit lost in this part:

.../* Now for i-th student use index */
... student[i].name ... - pointer to name string of i-th student

and what goes here:

/* Use it: */
i = findIndex(student,N,studentid);
if (i == NOTFOUND)

{   
 /* No such student, print message and continue */
}

else

{    
/* Now pointer to i-th student is student+i */
}

can you give an example of what goes there? thanks :)

It all boils down to understanding how arrays are stored in memory and how we access each element of an array.

arrays are stored sequentially in memory. So lets say you declare and array of 5 integers
int arr[5];
in memory you can imagine it to be 5 cells allocated one after another, each cell big enough to hold an integer. Each of these cells is given a number starting from '0'. Hence to get the value of first element i can say arr[0], for 2nd element arr[1] n so on.

In your code you declare an array of type 'STUDENT'. hence student gives you the value stored in 'i'th location which is of type 'STUDENT'. hence you get the structure, now you access the 'name' attribute of that structure student.name.

Internally accessing an array is done using pointers. Once you understand this you should learn that too.

Thank you, Agni.
Dear perfectlover09 (what's a nick;), I can't write the program in place of you.
See, for example, how to print student info when you get studentid from the console:

int studentidx; /* it's better than simple i */
...
scanf("%d",&studentid);
studentidx = findIndex(studentid);
if (studentidx == NOTFOUND)
{
   printf("Sorry, student with id %d not found\n",studentid);
   return 1; /* or try to continue */
}
     printf("Student's Name  Quiz 1  Quiz 2  Quiz 3  Quiz 4  Exam\n");
     printf("==============  ======  ======  ======  ======  ====\n\n");
      
      if (studentid = student.id[0])
      {
      printf("%-26s %4d %4d %4d %4d %4d\n", /* not %-26c !!! */
	           student[studentidx].name, 
	           student[studentidx].quiz1,
	           student[studentidx].quiz2,
	           student[studentidx].quiz3,
	           student[studentidx].quiz4,
	           student.exam[0]);
/*** Rewrite these functions: pass pointer to STUDENT structure for studentidx-th element
	           studentAverage(average);
	           scoreToGrade(score);
***/

See the format string in your printf call: no format descriptors for last two arguments.

Apropos: suppose, you ask me to enter studentid but I type "ha-ha, catch you!" (well, it was misprinting, not a number). The scanf call was failed, no new value in studentid. In the best case you repeate calculations for old value. Moral: always add input check up code, for example (in that case):

if (scanf("%d",studentid) != 1)
{
    printf("Alas, you type not a number\n");
    return 2; /* Of course, invent better code to repeate studentid request here */
}

Regrettably, original code for array of structures habdling was absolutely inadequate.
Please, make every effort to rewrite it.

Good luck!

You see: it was misprinting, the last code snippet above must be

if (scanf("%d",&studentid) != 1) /* One ampersand was missed == total run-time crash */
{    
   printf("Alas, you type not a number\n");    
   return 2; /* Of course, invent better code to repeate studentid request here */
}
...

oooooh i see...Thanks!! i'll try that tomorrow, but now i'm going to sleep hahaha but thanks for ur help :P and yes my nick is unique...for a girl lol ;)

i tried what you told me but now im getting a different error.
this is what i have so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N 5
#define NOTFOUND (-1)

//	Global Declarations
typedef struct /* One student <--> one struct object */
{
        int  id;
	    char name[50]; 
	    int quiz1;
	    int quiz2;
        int quiz3;
        int quiz4;
	    int exam;
} STUDENT;


void menu(int option);
int findIndex(STUDENT* student, int id);
void studentAverage(double average);
char scoreToGrade (int score);
int studentid;

int main (void)
{
//	Local Declarations
	int option, average, score, id;
	int studentidx;
   
	
//	Statements
	STUDENT student[N] = 
	{
	    { 1234, "Julie Adams", 52, 7, 100, 78, 34 },
	    { 2134, "Harry Smith", 90, 36, 90, 77, 30},
	    { 3124, "Tuan Nguyen", 100, 45, 20, 90, 70},
	    { 4532, "Jorge Gonzales", 11, 17, 81, 32, 77},
	    { 5678, "Amanda Trapp", 20, 12, 45, 78, 34}, 
	};
	menu(option);
	while (option == 1 || option == 2 || option == 3 || option == 4)
	{
       printf("Enter student id: ");
       scanf("%d",&studentid);
       }
       
	studentidx = findIndex(student, id);
	
	if (studentidx == NOTFOUND)
	{
	   printf("Sorry, student with id %d not found\n",studentid);
	   return 1; 
	}
	
	while (option == 1)
	{
	     printf("Student's Name  Quiz 1  Quiz 2  Quiz 3  Quiz 4  Exam\n");
	     printf("==============  ======  ======  ======  ======  ====\n\n");
 
      if (studentid = student.id[0])
	      {
      printf("%-26s %4d %4d %4d %4d %4d\n", 
               student[studentidx].name, 
	           student[studentidx].quiz1,
	           student[studentidx].quiz2,
	           student[studentidx].quiz3,
	           student[studentidx].quiz4,
	           student[studentidx].exam);
            }
      
      else if (studentid = student.id[1])
	      {
      printf("%-26s %4d %4d %4d %4d %4d\n", 
               student[studentidx].name, 
	           student[studentidx].quiz1,
	           student[studentidx].quiz2,
	           student[studentidx].quiz3,
	           student[studentidx].quiz4,
	           student[studentidx].exam);
            }
      
      else if (studentid = student.id[2])
	      {
      printf("%-26s %4d %4d %4d %4d %4d\n", 
               student[studentidx].name, 
	           student[studentidx].quiz1,
	           student[studentidx].quiz2,
	           student[studentidx].quiz3,
	           student[studentidx].quiz4,
	           student[studentidx].exam);
            }
      
      else if (studentid = student.id[3])
	      {
      printf("%-26s %4d %4d %4d %4d %4d\n", 
               student[studentidx].name, 
	           student[studentidx].quiz1,
	           student[studentidx].quiz2,
	           student[studentidx].quiz3,
	           student[studentidx].quiz4,
	           student[studentidx].exam);
            }
      
      else if (studentid = student.id[4])
	      {
      printf("%-26s %4d %4d %4d %4d %4d\n", 
               student[studentidx].name, 
	           student[studentidx].quiz1,
	           student[studentidx].quiz2,
	           student[studentidx].quiz3,
	           student[studentidx].quiz4,
	           student[studentidx].exam);
            }
      }

and these are the errors that im getting:


In function `main':
69 request for member `id' in something not a structure or union
80 request for member `id' in something not a structure or union
91 request for member `id' in something not a structure or union
102 request for member `id' in something not a structure or union
113 request for member `id' in something not a structure or union
In function `findIndex':
166 syntax error before ')' token


any suggestions?

if ( studentid = student.id[0] )

should be

if ( studentid = student[0].id )

etc.

No, Dave, it should be this:

if ( studentid == student[0].id )
commented: D'oh! Thanks. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.