Solved

Array of Structures Help

Posted on 1998-04-22
5
176 Views
Last Modified: 2010-04-15
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
Comment
Question by:ismg
  • 3
5 Comments
 
LVL 2

Expert Comment

by:Ready4Dis
ID: 1252894
//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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1252895
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
 
LVL 2

Expert Comment

by:seedy
ID: 1252896
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1252897
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
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1252898
Can I answer this then?
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now