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;
}
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 *
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();
}