C/C++ C++: Open File Multiple Times Before Exiting

  • Thread starter Thread starter Nusc
  • Start date Start date
  • Tags Tags
    C++ File
AI Thread Summary
The discussion revolves around a programming challenge where a user needs to repeatedly open a file until they choose to exit the program. The initial code snippet demonstrates how to open a file and read student data, but it lacks a mechanism for prompting the user to enter a new file name multiple times. A suggested solution involves implementing a loop that continues to ask the user if they want to open another file, using a character input to control the loop's continuation. The user confirms they have resolved the issue, indicating they found a satisfactory solution.
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;
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();

}
 
Technology news on Phys.org
No one knows?
 
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top