help please!


I'm trying to get my information from the file stored in my array and printed in an outputfile, but when i open up my output file, it is blank. please help. The file information is at the end.

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#define SIZE 20
using namespace std;


struct student{
string name;
long id;
int units;
float gpa;
};

string menu(void);
void read(ifstream &fin, ofstream &fout, student list[], float &total_gpa, int i, int &num_students);
bool verify_units(int units);
void save_file(student list[], int num_students);
bool verify_grade(char grade);
void open_file(string file_name, ifstream &fin);
float assign_grade_points(char grade);

int main()
{
ifstream fin;
ofstream fout;
string file_name, selection, student_name;
student list[SIZE];
long id;
int num_students, units, total_units=0, student_grade_points, student_units=0, i=0, counter = 0;
float gpa, total_gpa;

cout<<"Please enter a letter and press enter to begin."<<endl<<endl;
do{
selection=menu();
if(selection=="O" || selection=="o")
{
cout<<"\nEnter student file: ";
cin>>file_name;
open_file(file_name, fin);
fin.clear();
getline(fin, list[i].name);
while(!fin.eof())
{
cout<<list[i].name<<endl;
fin>>id;
cout<<id<<endl;
read(fin, fout, list, total_gpa, i, num_students);
i++;
fin.ignore('\n', 10);
counter++;
getline(fin, list[i].name);
cout<<endl;
}

cout<<"What would you like to do next?"<<endl;
}
save_file(list, num_students);
cout<<"Thank you for using my program! Have a wonderful day."<<endl;
break;
}
else
cout<<"\nInvalid input. Please try again.\n\n";

}
while(selection!="X" || selection!="x");

return 0;
}



string menu(void)
{
string selection;
cout<<"'O' - Open an existing student's file"<<endl;//unfinished
cout<<"'A' - Add new person to the current list"<<endl;//need to finish input of grade & units(or leave as is)
cout<<"'R' - Remove a person from the current list"<<endl;//run to see if correct
cout<<"'P' - Print one student's info in the current list"<<endl;//finish parts of table and file
cout<<"'E' - Edit information for 1 person"<<endl;
cout<<"'S' - Search"<<endl;//almost done
cout<<"'T' - Sort the list"<<endl;//check to see if works
cout<<"'X' - Exit the program\n\n";//done
cout<<"Selection: ";
cin>>selection;
while(selection.length()!=1)
{
cout<<"Only one character may be entered at a time."<<endl;
cout<<"Re-enter selection: ";
cin.ignore();
cin>>selection;
}
return selection;
}

void open_file(string file_name, ifstream &fin)
{
fin.open(file_name.c_str());
}

bool verify_grade(char grade)
{
if(grade == 'A' || grade== 'a')
return true;
else if(grade == 'B' || grade == 'b')
return true;
else if(grade == 'C' || grade == 'c')
return true;
else if(grade == 'D' || grade == 'd')
return true;
else if(grade == 'F' || grade == 'f')
return true;
else if(grade == '*')
return false;
else
return false;
}

bool verify_units (int units)
{
if (units > 0 && units <=5)
return true;
else
return false ;
}

float assign_grade_points(char grade)
{
float score;
if(grade == 'A' || grade=='a')
score=4.0;
else if(grade == 'B' || grade == 'b')
score=3.0;
else if(grade == 'C' || grade == 'c')
score=2.0;
else if(grade == 'D' || grade == 'd')
score=1.0;
else if(grade =='F' || grade =='f')
score=0.0 ;
else
score=-1;
return score ;
}

void read(ifstream &fin, ofstream &fout, student list[], float &total_gpa, int i, int &num_students)
{
char grade=0;
string name, file_name;
long id;
bool grade_verify, units_verify;
float gpa, units, student_grade_points = 0, student_units = 0, total_grade_points = 0, total_units = 0, student_multi = 0;


fin>>grade;
while(grade != '*')
{
gpa=0;
cout<<grade<<" ";
fin>>units;
cout<<units<<" ";

if(verify_grade(grade) == true)
{
student_multi = assign_grade_points(grade);
units_verify = verify_units(units);
}
else
{
student_grade_points += 0;
student_units += -0;
}
if(verify_units(units) == true)
{
student_grade_points += (student_multi * units);
student_units += units;
}
else
{
student_grade_points += 0;
student_units += -0;
}
fin>>grade;
num_students++;

}
if(student_units == 0)
gpa = 0;
else
{
cout<<setprecision(2)<<showpoint<<fixed;
gpa=student_grade_points / student_units;
list[i].gpa=student_grade_points / student_units;
list[i].units=student_units;
cout<<gpa;
}
}



Smith, John
12345678 A 3 b 4 b 2 x 4 C 4 *
Brown Jones, Nancy
22345678 a 5 B 4 a 3 B 4 *
Bad Data2, Joe
88887777 X 4 *
Adamson Jr., Jerry
99887766 A 5 B 4 a 3 b 4 *
Shore, John
12345668 A 3 b 4 b 2 b 4 C 4 *
Jonsen, Lori
22445678 a 4 C 4 D 3 B 4 *
Blackston Jr., Jerry
99887666 A 5 K 4 D 3 A 4 *
Bad Data, Joe
22223333 *
Partison, John
12346678 A 2 b 3 h 5 x 4 C 4 *
Brownson, Adam
22345688 a 3 C 4 b 2 C 4 *
Bad Data1, Joe
99999999 A 7 *
Johnson, Jerry
99877766 A 5 B 4 B 3 C 4 *
I don't see you ever doing anything with fout... Am I missing something?
It seems you didn't implement the function "save_file"...
It looks like homework. I've seen and done similar programs in my classes. I think they're looking for the way to take the output and save it in a new file. SaveFile was never even defined. I assume they want someone to write the function for them.
void save_file(student list[], int num_students)
{
ofstream fout;
string file_name;

cout<<"Enter file you would like to save data to: ";
cin.ignore();
getline(cin, file_name);
fout.open(file_name.c_str()); //open output file
while(fout.fail())
{
cout<<"Bad file name entered. Re-enter: ";
getline(cin, file_name);
fout.open(file_name);
}
for(int i=0; i<num_students; i++)
{
fout<<list[i].name<<endl;
fout<<list[i].id<<endl;
fout<<list[i].units<<endl;
}
fout.close();
}
Topic archived. No new replies allowed.