1

I have an input files with chars (grades) in it and am trying to input them into an array using functions and input files. I don't beleive I am declaring / passing the input file (iFile) correctly. Any help with my sytax and parameters would be great.

Error(s): 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]' is private

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void readInput(char gradeArray[][3], ifstream iFile);
void spitOutpt(char gradeArray[][3], ifstream iFile);

int main()
{
    ifstream iFile;
    iFile.open("grades.txt");
    char gradeArray[5][3];

    readInput(gradeArray, iFile);
    spitOutpt(gradeArray, iFile);

    return 0;
}

void readInput(char gradeArray[][3], ifstream iFile)
{
    for(int r = 0; r < 5; r++)
        {
            for(int c = 0; c < 3; c++)
            {
            iFile >> gradeArray[r][c];
            }
        }

    return;
}

void spitOutpt(char gradeArray[][3], ifstream iFile)
{
    cout << "All Grades" << endl;
    cout << left << setw(10) << "Student";
    cout << left << setw(10) << "English";
    cout << left << setw(10) << "History";      
    cout << left << setw(10) << "Math";
    cout << endl;


    for(int r = 0; r < 5; r++)
        {
        cout << "#" << left << setw(10) << r;
            for(int c = 0; c < 3; c++)
            {
            cout << left << setw(10) << gradeArray[r][c];
            }
        cout << endl;
        }

    return;
}

1 Answer 1

2

Pass std::ifstream and std::ofstream by reference.

They don't have copy constructors.

void readInput(char gradeArray[][3], ifstream& iFile);
void spitOutpt(char gradeArray[][3], ifstream& iFile);

Suggestion for cleanup

Your use of ifstream as argument to spitOutpt does not seem right. Perhaps you meant that to be an ofstream.

void spitOutpt(char gradeArray[][3], ofstream& oFile);

and then change main to:

int main()
{
    ifstream iFile;
    iFile.open("grades.txt");
    char gradeArray[5][3];

    readInput(gradeArray, iFile);


    ofstream oFile;
    oFile.open("grades-out.txt");
    spitOutpt(gradeArray, oFile);

    return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Why is that? Never really understood what 'copy constructors' and 'passing by value/reference' truly mean.
@Stubbsy, why is what?
@R Sahu, sorry for the very basic questions. Only my 2nd week at this. Why does the & make it work? I understand its use but not it's true definition. Thanks again.
When the argument type is ifstream&, the input argument is passed as a reference, not by value. Passing by value will make a copy. Passing by reference does not make a copy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.