1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ - File opening

  1. Apr 8, 2007 #1
    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();

    }
     
  2. jcsd
  3. Apr 8, 2007 #2
    No one knows?
     
  4. Apr 9, 2007 #3

    siddharth

    User Avatar
    Homework Helper
    Gold Member

    Put it in a loop?

    Code (Text):

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

    \\ Code here

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

    }
     
     
  5. Apr 9, 2007 #4
    I got it don't worry about it.

    Thanks
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?