Solved

Array of Structures Help

Posted on 1998-04-22
5
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

734 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