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