- #1
Nusc
- 760
- 2
Hi. I am required to ask the user to open a file multiple times before exiting the program. How do I do this?
void ReadData(vector <student> & section, int& num_Students,string& filename)
{
ifstream in;
cout << "Please enter file name:" << endl;
cin >> filename;
in.open(filename.c_str());
if(in.fail())
{
// cout << "Input file does not exist. Please enter the correct file name again:" << endl;
// while (i < = 3)
// {
cout << "Input file does not exist. Now exiting program." << endl;
exit(1);
}
else
{
if(!in.eof())
{
in >> num_Students;
cout <<"You have successfully opened "<< filename <<" and there are "<< num_Students <<" students in file."<< endl;
section.resize(num_Students);
for (int i=0; i<section.size();i++)
{
in >> section.at(i).first >> section.at(i).last >> section.at(i).id;
in >> section.at(i).final >> section.at(i).midterm;
section.at(i).lab_scores.resize(5);
for (int j=0;j<5;j++)
in>>section.at(i).lab_scores.at(j);
}
}
}
in.close();
}
void ReadData(vector <student> & section, int& num_Students,string& filename)
{
ifstream in;
cout << "Please enter file name:" << endl;
cin >> filename;
in.open(filename.c_str());
if(in.fail())
{
// cout << "Input file does not exist. Please enter the correct file name again:" << endl;
// while (i < = 3)
// {
cout << "Input file does not exist. Now exiting program." << endl;
exit(1);
}
else
{
if(!in.eof())
{
in >> num_Students;
cout <<"You have successfully opened "<< filename <<" and there are "<< num_Students <<" students in file."<< endl;
section.resize(num_Students);
for (int i=0; i<section.size();i++)
{
in >> section.at(i).first >> section.at(i).last >> section.at(i).id;
in >> section.at(i).final >> section.at(i).midterm;
section.at(i).lab_scores.resize(5);
for (int j=0;j<5;j++)
in>>section.at(i).lab_scores.at(j);
}
}
}
in.close();
}