okay finally got some source code to show i still am having some issues with arrays and pointers...but this is as far as i have got...
student.h (aka header file)
Code:
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "~/CSC60/lab5/out.txt"
#define MAX_SIZE 29
#define SCORE_TEST 3
#define SCORE_PROGRAM 5
#define ID 9
#define SIZE 3
typedef struct the_student {
char idNumber[ID], name[MAX_SIZE], gender,
classLvl, age[SIZE], testScore[SCORE_TEST],
programScore[SCORE_PROGRAM];
}student;
void addStudent();
void deleteStudent(char[], int);
void addTestScore(char[], int);
void addProgramScore(char[], int);
void outputClassReport();
test.c (aka the main method)
Code:
#include <stdio.h>
#include <stdlib.h>
#include "student.h"
int main(){
int num, quit = 0;
char id [];
while(quit != 6){
printf("Please enter the number of the option you wish to run:\n");
printf("1. add student\n2. delete student\n3. add test score\n");
printf("4. add program score\n5. output class status\n 6. quit\n");
scanf("%d\n", &num);
switch(num){
case 1: addStudent();
break;
case 2: printf("Please enter the 9-digit idNumber of the student");
scanf("%s" &id);
deleteStudent(id, sizeof(id));
break;
case 3: printf("Please enter the 9-digit idNumber of the student");
scanf("%s" &id);
addTestScore(id, sizeof(id));
break;
case 4: printf("Please enter the 9-digit idNumber of the student");
scanf("%s" &id);
addProgramScore(id, sizeof(id));
break;
case 5: outputClassReport();
break;
case 6: quit = 6;
break;
}//end switch statement
}//end while loop
return 0;
}//end main
addP.c (aka adding students with no scores)
Code:
#include <stdio.h>
#include <stdlib.h>
#include "student.h"
void addProgramScore(char id[], int size){
int found =0, i=0,z=0, pos=0;
char chars[SCORE_PROGRAM];
FILE* FILE_PTR;
FILE_PTR = fopen(FILE_NAME, "rb+");
fseek(FILE_PTR, 0, SEEK_END);
pos = ftell(FILE_PTR);
rewind(FILE_PTR);
student *tmp, array[pos];
tmp = malloc(sizeof(student));
tmp->idNumber = id;
fread(array, sizeof(student), pos, FILE_PTR);
printf("What number Program score do you wish to enter: ");
scanf("%d", &z);
printf("What is score you are inputing to program score number %d :" &z);
scanf("%s\n", chars);
for(i =0; i<pos; i++){
if(array[i]->idNumber == tmp->idNumber){
array[i]->programScore[z] = chars;
}//end if statement
}//end for loop
fclose(FILE_PTR);
}//end deleteStudent
delete.c (aka deleting students)
Code:
#include <stdio.h>
#include <stdlib.h>
#include "student.h"
void deleteStudent(char num[], int size){
FILE* FILE_PTR;
int found = 0, pos, i=0, z=0;
student *find;
FILE_PTR = fopen(FILE_NAME, "ab+");
find = malloc(sizeof(student));
find->idNumber = num;
fseek(FILE_PTR, 0, SEEK_END);
pos = ftell(FILE_PTR);
rewind(FILE_PTR);
student array[pos];
fread(array, sizeof(student), pos, FILE_PTR);
while(z++ < pos){
if(array[z]->idNumber == find->idNumber){
for(i=z; i< pos; i++){
array[z] = array[z+1];
}//end for loop
}//end if statement
fseek(FILE_PTR, 1, SEEK_CUR);
}//end while loop
fclose(FILE_PTR);
}//end deleteStudent
addT.c (aka adding test scores to students by idnumber)
Code:
#include <stdio.h>
#include <stdlib.h>
#include "student.h"
void addTestScore(char *id[], int size){
int found =0, i=0,z=0, pos;
char chars[SCORE_TEST];
FILE *FILE_PTR;
FILE_PTR = fopen(FILE_NAME, "rb+");
fseek(FILE_PTR, 0, SEEK_END);
pos = ftell(FILE_PTR);
rewind(FILE_PTR);
student *tmp, *array[pos];
tmp = malloc(sizeof(student));
tmp->idNumber = id;
fread(array, sizeof(student), pos, FILE_PTR);
printf("What number Test score do you wish to enter: ");
scanf("%d", &z);
printf("What is score you are inputing to test score number %d :", &z);
scanf("%s", chars);
for(i =0; i<pos; i++){
if(array[i]->idNumber == tmp->idNumber){
array[i]->testScore[z] = chars;
}//end if statement
}//end for loop
fclose(FILE_PTR);
}//end addTestScores
addP.c (same as above except dealing with program scores instead)
Code:
#include <stdio.h>
#include <stdlib.h>
#include "student.h"
void addProgramScore(char id[], int size){
int found =0, i=0,z=0, pos=0;
char chars[SCORE_PROGRAM];
FILE* FILE_PTR;
FILE_PTR = fopen(FILE_NAME, "rb+");
fseek(FILE_PTR, 0, SEEK_END);
pos = ftell(FILE_PTR);
rewind(FILE_PTR);
student *tmp, array[pos];
tmp = malloc(sizeof(student));
tmp->idNumber = id;
fread(array, sizeof(student), pos, FILE_PTR);
printf("What number Program score do you wish to enter: ");
scanf("%d", &z);
printf("What is score you are inputing to program score number %d :" &z);
scanf("%s\n", chars);
for(i =0; i<pos; i++){
if(array[i]->idNumber == tmp->idNumber){
array[i]->programScore[z] = chars;
}//end if statement
}//end for loop
fclose(FILE_PTR);
}//end addProgramScore
outputC.c (aka printing out the report card)
Code:
#include <stdio.h>
#include <stdlib.h>
#include "student.h"
void outputClassReport(){
int pos, i;
FILE *FILE_PTR;
FILE_PTR = fopen(FILE_NAME, "rb");
student *tmp;
tmp = malloc(sizeof(student));
fseek(FILE_PTR, 0, SEEK_END);
pos = ftell(FILE_PTR);
student array[pos];
rewind(FILE_PTR);
fread(array, sizeof(student), pos, FILE_PTR);
printf("\t\t\tCLASS \t\t TEST");
printf("\tID \t NAME \t LVL GENDER AGE SCORES \t PROGRAM SCORES");
printf("----------------------------------------------------------------------------");
for(i=0; i<pos; i++)
printf("%9s %s %5s %2s %4s %2s %2s", array[i]->idNumber, array[i]->name,
array[i]->classLvl, array[i]->gender,array[i]->age, array[i]->testScore,
array[i]->programScore);
}//end while loop
fclose(FILE_PTR);
}//end outputClassReport