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

const int numRows=5;
const int numCol=7;
void openfile(ifstream& , ofstream& );
void initialize(ifstream& , ofstream& );
void ave(ifstream& ,ofstream& , double , double,string );
void printout(ifstream& , ofstream&  ,string ,double ,double );

int main(){
ifstream infile;
ofstream outfile;
string names[numRows];
double results[numRows][numCol];
double average[numRows];



openfile(infile,outfile);
initialize(infile,outfile);
//ave(infile,outfile,results,average,names);
//printout(infile,outfile,names ,results,average); 

return 0;
}

//this function is used to open up files and call on initilize
void openfile(ifstream& infile, ofstream& outfile){
infile.open("runnerslog.txt");
outfile.open("results.txt");
initialize(infile,outfile);
}

//this function initilizes my arrays and the calls on the average and print statements
void initialize(ifstream& infile, ofstream& outfile){
int i;
int j;
char names[numRows];
double results[numRows][numCol];
double average[numRows];

for(i=0; i<numRows; i++){
infile>>names[i];
}
for(i=0;i<numRows;i++){
for(j=0;j<numCol;i++)
infile>>results[i][j];
cout<<endl;
}
for(i=0;i<numRows;i++)
{
infile>>average[i];
}

}


void ave(ifstream& infile,ofstream& outfile, double results[numRows][numCol], double average[numRows],string names[numRows]){
int i;
int j;
int sum=0; 
for(i=0; i<numRows;i++)
{
sum=0;
for(j=0;j<numCol;j++)
average[i]=average[i]+(results[i][j]/7);
printout(infile,outfile, names ,results,average);
}



}


void printout(ifstream& infile, ofstream& outfile ,string names[numRows],double results[numRows][numCol],double average[numRows]){
int i;
int j;
for(i=0; i<numRows; i++){
outfile<<names[i];
}
for(i=0;i<numRows;i++){
for(j=0;j<numCol;i++)
outfile<<results[i][j];
cout<<endl;
}
for(i=0;i<numRows;i++)
outfile<<average[i];


infile.close();
outfile.close();


}

I have coding in C++ that is suppose to read from a file that has five runners and their amount of miles they ran for seven days. I am suppose to then find the average and total miles they have ran. The error message I am getting is in line 70 for trying to output to the output file. It says error C2664: 'printout' : cannot convert parameter 3 from 'std::string []' to 'std::string' line 70

The error says it all,
the actual and formal parameter for printout() at line 70 at names is wrong as it is an array

Edited 4 Years Ago by zeroliken

line 11 should be

void printout(ifstream& infile, ofstream& outfile ,string names[numRows],double results[numRows][numCol],double average[numRows]);

Try printing to standard output instead. Perhaps the input data is not being read properly?

You can also check if the input and output file is open with is_open() member function.

I think it is the reading of my input file. I changed the for loops in the function printout to just print each value and nothing is appearing on the screen.

I have the file as runners.txt
And the names and numbers are set as:

Jason 2 3 2 4 5 1 5
Samantha 6 7 8 3 5 2 6
Ravi 9 2 5 1 5 7 4
Sheila 7 7 6 8 5 7
Ankit 5 7 5 6 8 5 9

It would be easier to read and understand your code if you indent on the for loops.

Your initialize() function should be like this:

I just realized your using the same iterator variable 'i' for each loop.

for(i=0; i<numRows; i++) {
    infile>>names[i];

    // you're using 'i' twice, use another variable otherwise
    // i=numRows and only the first line in the file is processed
    for(r=0;r<numRows;r++) {
        for(j=0;j<numCol;i++)
            infile>>results[r][j];
        cout<<endl;
    }

    // why add this? you have to calculate the average not store it
    /*
    for(i=0;i<numRows;i++)
    {
        infile>>average[i];
    }*/
}

Okay I think I changed it in the right place but still nothing is printing to the screen.

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

//Intializing the functions
const int NumRows=5;
const int NumColumns=7;
void openfile(ifstream& , ofstream& );
void initialize(ifstream& , ofstream& );
void runneraverage(ifstream& ,ofstream& , double , double,string );
void printing(ifstream& infile, ofstream& outfile ,string names[NumRows],double results[NumRows][NumColumns],double average[NumRows]);

//Main function
int main()
{
    //Declaring variables
    ifstream infile;
    ofstream outfile;
    string names[NumRows];
    double results[NumRows][NumColumns];
    double average[NumRows];


    //Calling the functions 
    openfile(infile,outfile);
    initialize(infile,outfile);


    return 0;
}

//This function is used to open up files and call on initilize
void openfile(ifstream& infile, ofstream& outfile)
{
    //Opens the file runners and opens a blank output file
    infile.open("runners.txt");
    outfile.open("runnersResults.txt");
    //Calling the function initialize
    initialize(infile,outfile);
}

//This function initilizes my arrays and the calls on the average and print statements
void initialize(ifstream& infile, ofstream& outfile)
{
    //Declaring the variables
    int r;
    int j;
    int i;
    char Names[NumRows];
    double Results[NumRows][NumColumns];
    double Average[NumRows];
    //Using the for loop to get the names from the file runners
    for(i=0; i<NumRows; i++) 
    {
        infile>>Names[i];
        for(r=0;r<NumRows;r++) 
        {
            for(j=0;j<NumColumns;i++)
                infile>>Results[r][j];
                cout<<endl;
        }
    }
}

//This function gets the sum of each runner
void runneraverage(ifstream& infile,ofstream& outfile, double results[NumRows][NumColumns], double average[NumRows],string names[NumRows])
{
    //Declaring the variables
    int i;
    int j;
    int Sum=0;
    //Using the for loop to get the average of each runner
    for(i=0; i<NumRows;i++)
    {
        Sum=0;
        for(j=0;j<NumColumns;j++)
            average[i]=average[i]+(results[i][j]/7);
            printing(infile,outfile, names ,results,average);
    }



}
//This function should be printing to the output file 
void printing(ifstream& infile, ofstream& outfile ,string Names[NumRows],double results[NumRows][NumColumns],double average[NumRows])
{
    //Declaring the variables
    int k;
    int l;
    //Uisng the for loop to get the names from the Names array
    for(k=0; l<NumRows; k++)
    {
        //Writing the names to the output file
        cout<<Names[k]<<endl;
    }
    //This for loop is getting the results which should be the total miles ran for each runner 
    for(k=0;k<NumRows;k++)
    {
        //Writing the results to the output file
        for(l=0;l<NumColumns;k++)
            cout<<results[k][l];
            cout<<endl;
    }
    //Writing the average of each runner to the output file
    for(k=0;k<NumRows;k++)
        cout<<average[k];

    //Closing the files
    infile.close();



}

You should remove lines 50-52 and pass the arrays to initialize() from main. Actually, you have two calls to initialize(), one in main and the other in openfile(). Remove one of them, preferably from inside openfile().

Pass the arrays to initialize(). Don't forget to modify the function prototypes.

ie.
initialize(infile, outfile, names, average, results);

Edited 4 Years Ago by dx9_programmer

This question has already been answered. Start a new discussion instead.