I'm trying to read a file into a vector using structure...
#include <fstream>
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
struct StudentRecord
{
int ID;
char FirstName[30];
char LastName[30];
char Grade[1];
};
void read_file(char FileName[20],vector<StudentRecord> &students)
{
ifstream Students(FileName, ios::in);
char S_ID[10];
/* char FName[30];
char LName[30];
char S_Grade[1];*/
StudentRecord entry;
do
{ Students.getline(S_ID,10, '-');
Students.getline(entry.FirstName,30, '-');
Students.getline(entry.LastName,30, '-');
Students.getline(entry.Grade,1, '-');
entry.ID = atoi(S_ID);
students.push_back(entry);
}
while(!Students.eof());
}
void print_order(vector<StudentRecord> &students)
{
cout << "\n\Panther ID\t First Name\tLast Name\tGrade" << endl;
vector<StudentRecord>::iterator iter;
for(iter = students.begin();iter < students.end(); iter++)
{
cout << iter->ID << "\t" << iter->FirstName << "\t" << iter->LastName<< "\t" << iter->Grade <<endl;
}
cout << "-------------------------------------------------------" << endl;
}
int main(int argc, char *argv[])
{
vector<StudentRecord> students;
char choice;
char FileName[20];
do
{
cout << "\n1. Enter a file name to read from" << endl;
cout << "2. Print the Order Record" << endl;
cout << "Q. Quit" << endl;
cin >> choice;
switch(choice)
{
case '1':
cout << "Please enter the name of a file: " << endl;
cin >> FileName;
read_file(FileName, students);
break;
case '2':
print_order(students);
break;
case 'q':
choice = 'Q';
case 'Q':
break;
default:
cout << "Invalid Choice" << endl;
}
}
while(choice != 'Q');
system("PAUSE");
return EXIT_SUCCESS;
}
/* cout << "Enter the name of the file you want to open: ";
cin >> FileName;
ifstream Students(FileName);
while (!Students.eof())
{
Students >> ID >> FirstName >>LastName >> Grade;
cout << "\nPanther ID: " << ID;
cout << "\nFirst Name: " << FirstName;
cout << "\nLast Name: " << LastName;
cout << "\nGrade: "<< Grade;
cout << "\n";
Students.get();
}
return 0;
}
*/