After adding information, when i choose the option "Display all" it crashes right before it's going to display GPA... can anyone help me...also can anyone help me read from the file into the array... plzz...this is really frustrating...any help greatly appreciated.
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <fstream>
#include <stdlib.h>
using namespace std;
typedef struct {
char fname[20];
char lname[20];
char IDNum[20];
char DOB [20];
float GPA;
} student;
const int num_of_students=10;
int stu_num=0;
//*************************************************************
//*************************************************************
void readfromfile (student temp_array[]) {
string line;
ifstream studentfile ("studentfile.txt");
if (studentfile.is_open())
{
while (! studentfile.eof() )
{
getline (studentfile,line);
cout << line << endl;
}
studentfile.close();
}
else cout << "Unable to open file";
}
//*************************************************
void writetofile(student temp_array[]) {
ofstream studentfile ("studentfile.txt", ios::app|ios::out);
if (studentfile.is_open()){
studentfile << temp_array[stu_num-1].fname << endl;
studentfile << temp_array[stu_num-1].lname << endl;
studentfile << temp_array[stu_num-1].IDNum << endl;
studentfile << temp_array[stu_num-1].DOB << endl;
studentfile << temp_array[stu_num-1].GPA << endl;
}
else cout << "Error opening file.";
}//writing to a file
//*************************************************
void addStudent(student temp_array[]) {
string tempValue;
char ok;
int total = 0 ;
int k;
int mark [4];
int i;
if (stu_num<num_of_students){
printf("\nAdd Student %d",stu_num+1,"\n");
do{//validation to check field length
printf("\nEnter First Name: ");
cin>>tempValue; //input
if(tempValue.length()>20){
ok='F';
printf("\nFirst Name Too Long!");
}else{
ok='T';
}
}while(ok!='T');
strcpy(temp_array[stu_num].fname, tempValue.c_str());
//**************
do{ //validation to check field length
printf("\nEnter Last Name: ");
cin>>tempValue;
if(tempValue.length()>20){
ok='F';
printf("\nLast Name Too Long!");
}else{
ok='T';
}
}while(ok!='T');
strcpy(temp_array[stu_num].lname, tempValue.c_str());
//**************
do{//validation to check field length
printf("\nEnter ID Number: ");
cin>>tempValue;
if(tempValue.length()>20){
ok='F';
printf("\nID Number Too Long!");
}else{
ok='T';
}
}while(ok!='T');
strcpy(temp_array[stu_num].IDNum, tempValue.c_str());
//**************
do{//validation to check field length
printf("\nEnter DOB: ");
cin>>tempValue;
if(tempValue.length()>20){
ok='F';
printf("\nDOB Number Too Long!");
}else{
ok='T';
}
}while(ok!='T');
strcpy(temp_array[stu_num].DOB, tempValue.c_str());
//**************
printf( "\nEnter 4 marks to compute a GPA. \n");
for( k = 1; k <= 4; k++){
printf("Enter mark %d : ", k);
scanf( "%d" , &mark[k]);
}
printf("\nThe marks were :\n ");
for ( k = 1; k<= 4; k++){
printf("%d : %d \n ",k,mark[k]);
total += mark[k];
}
printf("\nThe total is: %d\n" , total );
temp_array[stu_num].GPA =total / 100.00;
printf("\nThe GPA is: %.2f. " ,temp_array[stu_num].GPA);
stu_num += 1;
writetofile(temp_array);
}else {
printf("\nArray Full. No more students can be added at this time.");
}
}
//*************************************************
//*************************************************
void findStudent (student temp_array[])
{
char searchlname[20];
bool found;
int diff;
int i;
printf("\n**********FIND STUDENT**********") ;
printf("\nEnter Students Last Name:");
scanf( "%s", searchlname);
found = false;
for(i=0; i<num_of_students; i++) {
diff = strcmp(searchlname,temp_array[i].lname);
if ( diff == 0) {
found = true;
printf("\nFirts Name: %s", temp_array[i].fname);
printf("\nLast Name: %s",temp_array[i].lname);
printf("\nID Number: %s", temp_array[i].IDNum);
printf("\nDOB: %s", temp_array[i].DOB);
printf("\nGPA: %s", temp_array[i].GPA);
}
}
if (found == false) {
printf("\nNo Matches Found.");
}
}//end of findStudent
//*************************************************
//*************************************************
void displayAll (student temp_array[]) {
int i;
printf("\n******DISPLAY ALL******");
printf("\n");
for(i=0; i<stu_num; i++){
printf("\nFirts Name: %s", temp_array[i].fname);
printf("\nLast Name: %s", temp_array[i].lname);
printf("\nID Number: %s", temp_array[i].IDNum);
printf("\nDOB: %s", temp_array[i].DOB);
printf("\nGPA: %s", temp_array[i].GPA);
}
}//end of displayAll
//*************************************************
//*************************************************
void bubble_sort(student temp_array[num_of_students]) {
int i, j, flag = 1;
char tempfname[20];
char templname[20];
char tempIDNum[20];
char tempDOB [20];
float tempGPA;
for(i = 1; (i <= num_of_students) && flag; i++)
{
flag = 0;
for (j=0; j < (num_of_students -1); j++)
{
if (temp_array[j+1].GPA > temp_array[j].GPA)
{
strcpy(tempfname, temp_array[j].fname);
strcpy(temp_array[j].fname, temp_array[j+1].fname);
strcpy(temp_array[j+1].fname, tempfname);
strcpy(templname, temp_array[j].lname);
strcpy(temp_array[j].lname, temp_array[j+1].lname);
strcpy(temp_array[j+1].lname, templname);
strcpy(tempIDNum, temp_array[j].IDNum);
strcpy(temp_array[j].IDNum, temp_array[j+1].IDNum);
strcpy(temp_array[j+1].IDNum, tempIDNum);
strcpy(tempDOB, temp_array[j].DOB);
strcpy(temp_array[j].DOB, temp_array[j+1].DOB);
strcpy(temp_array[j+1].DOB, tempDOB);
tempGPA = temp_array[j].GPA;
temp_array[j].GPA= temp_array[j+1].GPA;
temp_array[j+1].GPA = tempGPA;
flag = 1;
}
}
}
return;
}
//*************************************************
//*************************************************
int main (void){
student student_array[num_of_students];
char choice;
printf("\n");
printf("\n******MAIN MENU******");
printf("\n Choose One of the Following:");
printf("\n (1) Add Student");
printf("\n (2) Find Student");
printf("\n (3) Display All");
printf("\n (4) Read saved data");
printf("\n (5) Sort students by marks");
printf("\n (6) Exit\n");
scanf("%c", &choice);
if (choice=='6'){
exit(0);
}
while(choice != '6') {
switch (choice) {
case '1':
addStudent (student_array);
break;
case '2':
findStudent (student_array);
break;
case '3':
displayAll (student_array);
break;
case '4':
readfromfile (student_array);
case '5':
bubble_sort (student_array);
break;
case '6':
exit (0);
break;
}
printf("\n******MAIN MENU******");
printf("\n Choose One of the Following:");
printf("\n (1) Add Student");
printf("\n (2) Find Student");
printf("\n (3) Display All");
printf("\n (4) Read saved data");
printf("\n (5) Sort students by marks");
printf("\n (6) Exit\n");
cin >> choice;
}
return 0;
}//end of main