• C

Array of Structures Help

I'm trying to pass a value (student_id) from a function back into main and put it in an array of structure (called struct student). Our array is called student_info.

Here is our mess below:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>


#define EXAM1WEIGHT .35
#define EXAM2WEIGHT .25
#define PROJWEIGHT .40
#define MAX 20
#define MAXN 25
#define TEST 3

struct student
{
      long student_id;
    char name[MAX];
      short Sscore;
    float course_avg;
      char grade;
} student_info[MAX];









// Function Prototypes
void Greeting(void);
char Menu(void);
long GetId(void);

int main(void)
{


       short Acount=0,Bcount=0,Ccount=0,Dcount=0,Fcount=0;
     int t=0,t1,t2;
     double course_avg[MAX];
     int best=0,worst=0;
     char grade[MAX],name[MAXN][MAX],x;
     long student_id[MAX];
     short Sscore[MAX][TEST],tracker=0;
     double exam_avg[MAX];
     short difference[MAX];
     struct student id;
     for (;;)
     {
          system("cls");
         

               
              student_info.student_id=GetId();
         
             
              if (student_id[tracker]==0)
                    continue;
               

                             
     }
         return 0;
}


long GetId(void)
{
     long student_id;
     
     for (;;)
     {
          printf("\nPlease enter the student ID: ");
          scanf("%ld",&student_id);
          fflush(stdin);
          if (student_id>0 && student_id<=999999999)
               break;
          printf("\n*** Error! Student ID of %ld is out of range! ***\n",
student_id);
     }
     return (student_id);
}

ismgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ready4DisCommented:
//Example on using array of structures...
//pretty much self explanatory...
#include <stdio.h>

struct testing
{
  int x, y;
} first_struct;

main ()
{
 testing array[10];
  int ctr;
 for (ctr=0;ctr<10;ctr++)
 {
   array[ctr].x=ctr;
   array[ctr].y=ctr*2;
 }
 for (ctr=0;ctr<10;ctr++)
   printf("%d:=<>=:%d\n",array[ctr].x,array[tr].y)
 return 0;
}
0
RONSLOWCommented:
missed answering :-(

Here is your problem

You have:
  student_info.student_id=GetId();
  if (student_id[tracker]==0) continue;

but student_info is an array, so you need:

  student_info[tracker].student_id=GetId();

next you haven't told the compiler where student_id is .. it is a member of an element the student_info array.  And student_id itself isn't and arary, so you don't but [] on it. You need to say

  if (student_info[tracker].student_id ==0) continue;

next your continue should be a break (so you will exit the loop) .. that makes it

  if (student_info[tracker].student_id ==0) break;

and finally, you need to increment your subscript (tracker)

  tracker++;

Please reject the previous answer if you find this one more helpful

0
seedyCommented:
ismg,
So, this is homework!  Apply RONSLOW's answer and then answer
yourself these questions.
Will the GetId ever return '0' to break out of the 'for' loop
in main?  Look at the 'if' statement in function GetId.
0
RONSLOWCommented:
Yeup - there's that one too.  You'll probably find a few problems as you go.  If so, post separate questions so each one can be answered and everyone gets a fair go.

0
RONSLOWCommented:
Can I answer this then?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.