lundi 23 novembre 2015

c++ asking the user for a file to imput?

So my program involves pointers, however my c++ instructor wants us to let the user specify in the console what data to load first. I am familiar with just simple input/output of file data. However, is there a way that when the user enters a certain file name that It can just load whatever it asks for, even if its not saved before hand inside my visual studio project folder? Here is my code:

int main()
{
    // variable declaration:
    int number = 0;
    int *index = 0;
    int *value = 0;
    string filename;
    fstream indata;
    double NumberArray[1];

    // 1.) user choses which file to upload
    cout << "Hello user. Type the file you wish to load." << endl;
    cin >> filename; // user types in filename
    if (filename == "data4.dat") // if file is data4.dat
    {
        cout << "file recognized...loading" << endl;
        indata.open("data4.dat");
        indata >> number;
        for (int i = 0; i > 0; i++)
        {
            *value = number;
            indata >> NumberArray[i];

        }
    }
    else if (filename == " // some file name here ") // if the file is not the data4.dat file, then upload whatever the user choses.
    {
        indata.open("  some file name here"); // open some other file not listed inside the visual studio project folder.
        indata >> number;
    }
    else
        cout << "error";
    return 0;
}

To sum up my question: can the user actually name a file of data to find, then have the program actually make its own file of that instance if it has to in order to let it open properly, even if it didnt exist in visual studio beforehand when you make the data to imput? Hope this helps explain my problem in question. Thanks.

Aucun commentaire:

Enregistrer un commentaire