vendredi 21 juillet 2017

How do I get input for file streams correctly using a function with pointer and string parameters?

I am working on a file assignment for C++ and am having a difficult time distinguishing the test code in main, from what I'm supposed to have my logic doing in my functions. For instance, I need to implement the function:

Here is the code I'm using to test with:

int main()
{
    std::string filename = "cars.txt";
    std::ifstream fin;
    bool isOpen = GetInputFileStream(&fin, filename);
    if (isOpen == false)
    {
        std::cout << "Couldn't find file " << filename << "!" << std::endl;
    }
    else
    {
        double newTotalPrice = 0;
        double newTotalMileage = 0;
        double usedTotalPrice = 0;
        double usedTotalMileage = 0;
        int numUsed = 0;
        int numNew = 0;

        AnalyzeFile(fin, numUsed, numNew, newTotalPrice, newTotalMileage,
            usedTotalPrice, usedTotalMileage);
        PrintStatistics(std::cout, numUsed, numNew, newTotalPrice, newTotalMileage,
            usedTotalPrice, usedTotalMileage);
    }
}

Here is the function (one of many) I need to implement:

bool GetInputFileStream(std::ifstream * fin, std::string filename)
{

}

The caller will supply a pointer to an input file stream object. I will use this object to open an input file that has the name filename. If the file doesn’t exist, alert the user by returning false.

Aucun commentaire:

Enregistrer un commentaire