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

Recommended Answers

All 3 Replies

My compiler's code critique:

main.cpp: In function `void addStudent(student*)':
main.cpp:89: warning: too many arguments for format
main.cpp:83: warning: unused variable 'i'
main.cpp: In function `void findStudent(student*)':
main.cpp:229: warning: format argument is not a pointer (arg 2)
main.cpp: In function `void displayAll(student*)':
main.cpp:255: warning: format argument is not a pointer (arg 2)

Read this http://www.daniweb.com/forums/announcement8-3.html
and figure out how to use code tags (there's a preview button, use that too).
I didn't look at the code, because it has NO indentation.

Second, figure out whether you want to be a C programmer or a C++ programmer.
Because this random mix of "cin" and "printf" you've got going on is a disaster waiting to happen.

Chash happens because everytime you want to print GPA, you try to print out a float number as a string.

printf("\nGPA: %s", temp_array[i].GPA);

Instead of %s use %f.

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.