C++: Open File Multiple Times Before Exiting

  • Context: C/C++ 
  • Thread starter Thread starter Nusc
  • Start date Start date
  • Tags Tags
    C++ File
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 4K views
Nusc
Messages
752
Reaction score
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;
count << "Please enter file name:" << endl;
cin >> filename;
in.open(filename.c_str());
if(in.fail())
{
// count << "Input file does not exist. Please enter the correct file name again:" << endl;

// while (i < = 3)
// {



count << "Input file does not exist. Now exiting program." << endl;
exit(1);
}
else
{
if(!in.eof())
{
in >> num_Students;
count <<"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();

}
 
Physics news on Phys.org
Nusc said:
No one knows?

Put it in a loop?

Code:
char c='y';
while(c!='n')
{

\\ Code here

cout<<"Do you wish to open another file (y/n)?"<<endl;
cin>>c;

}
 
I got it don't worry about it.

Thanks