user65384
user65384

Reputation: 153

Better to use getline or cin to populate an array from input file?

My problem could be with the index or my syntax here. I'm trying to populate two
arrays names[] and shares[] from an input file.

One half of the program, manual entry, works fine. Every attempt at the fstream half
has printed an empty output table with -1.#IND for the percentage of each
shareholder.

I have tried cin >> name >> share, getline(shareFile, name), while(shareFile >> name >> share){ ... names[i] = name; shares[i] = share;, but I don't know how use getline the right way I think so I'm working with the first. Like I said,
I don't know if it's a problem with my index or the code here.

This is the function that (I believe) is the problem, but maybe it's just not passing things the right way.

void getFileInputs(string names[], int shares[], int size)
{
    string file;
    string name;
    int share = 0;
    bool isOpen = true;
    ifstream shareFile("inputfile.txt");

    cout << "*----------------------------------*\n"
        << "*     Input From File:             *\n"
        << "*----------------------------------*\n";
    cout << "Please enter the name of the file (.txt): ";
    cin >> file;
    cout << "\n";

    shareFile.open("inputfile.txt");

    if(file == "inputfile.txt" || file == "inputfile")
    {
        int i = 0;
        do{

        for(i = 0; i < size; i++)
            {
                shareFile >> names[i] >> shares[i];
            }

        if(shareFile.eof())
        {
            isOpen = false;
        }
    }while(isOpen == true && shareFile >> name >> share);
    }
    else
    {
        cout << "Unrecognized file name. \n" << endl;
        isOpen = false;
    }
}

Upvotes: 0

Views: 524

Answers (1)

David G
David G

Reputation: 96845

The immediate problem is the following line of code:

shareFile.open("inputfile.txt");

A few lines above, when you constructed the shareFile object, you opened it with a file name but did not close it. Calling open() on an already opened file stream sets an error in the file stream's error mask. As such, any attempt of performing input will fail until the error is cleared from the stream.

Moreover, since file is the name of the file, I'm assuming you meant to open it with that. If so, the above should be changed to:

std::ifstream shareFile; // default-construct; no file is opened yet
// ...
shareFile.open(file.c_str()); // open it with the value of user-input

Another problem is with the looping and how you're assigning the values from the file to the array. Firstly, you shouldn't be using a do {} while() but rather a for() loop. Also, if shareFile hits the end of the stream, then the loop will terminate; thus there's no reason to use a boolean isOpen variable:

for (int i = 0; shareFile >> name >> share; ++i)
{
    // ...
}

Now once the values are placed into both name and share successfully, you can assign them to the values in the array:

for (int i = 0; shareFile >> name >> share; ++i)
{
    names[i] = name; shares[i] = share;
}

Upvotes: 1

Related Questions