Thread: Help on Structures in C

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Help on Structures in C

    In our programming class, we are asked to make a program on structures..and here are the pieces of code that our instructor gave us,we only have to provide for the statements inside the function.Also given to us is the output.This is a structure of student record.



    Code:
    
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype.h>
    
    struct studRec{
      
    char myname[30];
    char korsyr[20];
    char gender;
    int totsubj;
    float unit,fgrade;
    char subj[30];
    };
    
    void heading();
    void readStudRec(struct studRec *a);
    void printStudRec(struct studRec *a);
    float getSubj(struct studRec *a);
    
    main(){
    
    struct studrec ME;

    the output should be:

    Code:
    I.D. No.:  <input: 34572jt>
    NAME:  <input: John Smarz>
    COURSE & YR.:  <input: BS COE 2>
    SEX<F/M>:  <input: M>
    Total Subjects enrolled: <input: 2>
    once the user finishes entering the required inputs, it should again print the entered data.So it should look like this:

    Code:
    I.D. No: 34572jt
    NAME:  John Smarz
    COURSE & YR.: BS COE 2
    SEX<F/M.: M
    Total Subjects enrolled: 2
    
          SUBJECT                                UNIT                       FGRADE
    
            COE 1                              3                              3.7
            E.S 211                            3                              3.8
    
                    General Point Average(GPA):
    inputs of the subject name,unit,fgrade;depends on the total sujects enrolled.

    i'm not aksing someone to make the whole program. I just want to ask the ff. questions:
    1. In my readStudRec function, are the inputs be made in these function?What I mean is: it would look like this:
    Code:
     void readStudRec(struct studRec *a){
        printf("I.D No.:   ");
        scanf("%s",a->idno);
        printf("NAME: ");
        scanf("%s",a->myname);
        and so on....
        ....
    }
    and be called from main?
    i'm a bit confused on where to put the statements for the inputs whether in the main or in the readStudRec function.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Yes since the function recieves a pointer to the struct it would make sense to have the function get the input from the user and store it into the struct. By the way scanf() is kindof tricky to work with and not the suggested means of retrieving strings. Take a look at fgets.

    Also the way you accept input may cause problems for you. For instance to input an int you'll need:
    Code:
    scanf("%d", &(a->myInt));
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM