Can anyone help me fix these?Program displaying and writing symbols to my file... And also after calculating GPA , when choose function read or display all it is illustrated as null....help plz

# 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 Mark1;
        float Mark2;
        float Mark3;
        float Mark4;
        float GPA;       
} student;



const int num_of_students=10;

int stu_num=0;

//*************************************************************
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].Mark1 << endl;
         studentfile << temp_array[stu_num-1].Mark2<< endl;
         studentfile << temp_array[stu_num-1].Mark3 << endl;
         studentfile << temp_array[stu_num-1].Mark4 << endl;
        studentfile << temp_array[stu_num-1].GPA<< endl;
         }
      else cout << "Error opening file.";
      }//writing to a file
      
//*************************************************************

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 addStudent(student temp_array[]) {
     
     string tempValue;
     char ok;
     float total  ;

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

         
//************** 
        
        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');

        
//**************
         
                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');

         
//**************
         
                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');

         
//**************   
         
      do{  // validation to verify input is a numerical
       printf("\nEnter 1st Mark which is out of 100: ");           
       cin>>tempValue;
       temp_array[num_of_students].Mark1 = atof(tempValue.c_str());
       if (temp_array[num_of_students].Mark1 = atof(tempValue.c_str())){
           ok = 'T';
       }else{
             ok='F';
             printf("\nInvalid Numeric Value!");
             }
       }while (ok!= 'T');
       
//**************     
      
      do{  // validation to verify input is a numerical
       printf("\nEnter 2nd Mark which is out of 100: ");           
       cin>>tempValue;
       temp_array[num_of_students].Mark2 = atof(tempValue.c_str());
       if (temp_array[num_of_students].Mark2 = atof(tempValue.c_str())){
           ok = 'T';
       }else{
             ok='F';
             printf("\nInvalid Numeric Value!");
             }
       }while (ok!= 'T');
       
//**************
     
      do{  // validation to verify input is a numerical
       printf("\nEnter 3rd Mark which is out of 100: ");           
       cin>>tempValue;
       temp_array[num_of_students].Mark3 = atof(tempValue.c_str());
       if (temp_array[num_of_students].Mark3 = atof(tempValue.c_str())){
           ok = 'T';
       }else{
             ok='F';
             printf("\nInvalid Numeric Value!");
             }
       }while (ok!= 'T');
      
//**************      
      
      do{  // validation to verify input is a numerical
       printf("\nEnter 4th Mark which is out of 100: ");           
       cin>>tempValue;
       temp_array[num_of_students].Mark4 = atof(tempValue.c_str());
       if (temp_array[num_of_students].Mark4 = atof(tempValue.c_str())){
           ok = 'T';
       }else{
             ok='F';
             printf("\nInvalid Numeric Value!");
             }
       }while (ok!= 'T');
       
//**************
       do{  // validation to verify input is a numerical
       total = temp_array[num_of_students].Mark1 + temp_array[num_of_students].Mark2 +temp_array[num_of_students]. Mark3 +temp_array[num_of_students]. Mark4;
       temp_array[num_of_students].GPA = total / 100.00;
       
       

       printf("\nHence the GPA is: %.2f. " ,temp_array[num_of_students].GPA );
                
       cin>>tempValue;
       temp_array[num_of_students].GPA = atof(tempValue.c_str());
       if (temp_array[num_of_students].GPA = atof(tempValue.c_str())){
           ok = 'T';
       }else{
             ok='F';
             printf("\nInvalid Numeric Value!");
             }
       }while (ok!= 'T');                
//**************
         stu_num +=  1;
         writetofile(temp_array);

}
     else 
     {
                  
     
         printf("Array is full. No more records can be added.");
     }
}

//*************************************************
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("\n1st Mark: %.2f", temp_array[i].Mark1);
                   printf("\n2nd Mark: %.2f", temp_array[i].Mark2);
                   printf("\n3rd Mark: %.2f", temp_array[i].Mark3);
                   printf("\n4th Mark: %.2f", temp_array[i].Mark4);
                   printf("\nGPA: %.2f", 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("\n1st Mark: %.2f", temp_array[i].Mark1);
              printf("\n2nd Mark: %.2f", temp_array[i].Mark2);
              printf("\n3rd Mark: %.2f", temp_array[i].Mark3);
              printf("\n4th Mark: %.2f", temp_array[i].Mark4);
               printf("\nGPA: %.2f", temp_array[i].GPA);
              printf("\n*******************");
     }
}//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

Recommended Answers

All 2 Replies

Your readfromfile( ) does nothing but read through the file, it does not store anything to your array of students.

In the addStudent, what's wrong with the following bit?

cin>>tempValue;
temp_array[num_of_students].Mark1 = atof(tempValue.c_str());

Where are you attempting to store data?

It is writing to file but can you tell me how to read and insert into the temp array?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.