Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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

0
ismg
Asked:
ismg
  • 3
1 Solution
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now