Help find what is wrong with this program C Sample Output Co

Help find what is wrong with this program?? C++

Sample Output: Console (given provided input file)


Damian Hess 6.5
Andrew Nielsen 6.2
Alfred Yankovic 6.9
Inga Scharf 7.6

Sample Output: “lab08_out.txt” (given provided input file)


Hess Damian 5 10 7 5 9 2 8 5 6 8 6.5
Nielsen Andrew 7 9 8 7 4 7 3 4 9 4 6.2
Yankovic Alfred 6 5 8 10 9 6 5 7 3 10 6.9
Scharf Inga 9 2 7 10 7 8 8 10 6 9 7.6

Provided sample input file:

Program:

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

//function prototype
void calcAvg(ifstream& in, ofstream& out);

int main()
{
   //variables that hold inputfile and outputfile
   ifstream infile;
   ofstream outfile;
  
   //open the input file
   infile.open(\"lab08_in.txt\");
  
   //check if file is open
   if(infile.is_open())
   {
       //if yes, create an output file
       outfile.open(\"lab08_out.txt\");
       //call the function calcAvg by passing infile and outfile as parameters
       calcAvg(infile,outfile);
      
       cout<<\"Output file generated successfully...\ \";
       //close input file and output file
       infile.close();
       outfile.close();  
   }
   //if file not opened print an error message
   else
   {
       cout<<\"File cannot be opened. Try again../n\";
   }
   return 0;
}

//function that takes inpit file and out put file as parameters
void calcAvg(ifstream& in, ofstream& out) <-- HERE is where I get the error in codeblocks that i cant put the function definition here.
{
  
   int i,sumOfScores,scores[10];
   string firstname,lastname;
   double avg;
   //read firstname
   in>>firstname;
  
   //run a loop until end of file is reached
   while(in)
   {
       //set sumofscores to 0
       sumOfScores=0;
       //read lastname
       in>>lastname;
      
       //run a loop for 10 times and read the scores into scores array
       for(i=0;i<10;i++)
       {
           in>>scores[i];
           //add current score to sumofscores
           sumOfScores=sumOfScores+scores[i];
       }
       //calculate average
       avg= sumOfScores/10.0;
      
       //write firstname and lastname to output file
       out<<firstname<<\" \"<<lastname<<\" \";
      
       //write scores to output file
       for(i=0;i<10;i++)
           out<<scores[i]<<\" \";
  
       //write average to output file
       out<<avg<<endl;
      
       //read next firstname
       in>>firstname;
   }
}

Solution

This should run fine . I just added cout command to output to console ..

//output to console
   cout<<lastname<<\" \"<<firstname<<\" \";

//output to console
cout<<avg<<endl;

I added above two statements to function calcAvg to print output to console.

Complete program which runs fine for reference. Pls copy below code and try to run...

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//function prototype
void calcAvg(ifstream& in, ofstream& out);
int main()
{
//variables that hold inputfile and outputfile
ifstream infile;
ofstream outfile;
  
//open the input file
infile.open(\"lab08_in.txt\");
  
//check if file is open
if(infile.is_open())
{
//if yes, create an output file
outfile.open(\"lab08_out.txt\");
//call the function calcAvg by passing infile and outfile as parameters
calcAvg(infile,outfile);
  
cout<<\"Output file generated successfully...\ \";
//close input file and output file
infile.close();
outfile.close();
}
//if file not opened print an error message
else
{
cout<<\"File cannot be opened. Try again../n\";
}
return 0;
}
//function that takes inpit file and out put file as parameters
void calcAvg(ifstream& in, ofstream& out) //HERE is where I get the error in codeblocks that i cant put the function definition here.
{
  
int i,sumOfScores,scores[10];
string firstname,lastname;
double avg;
//read firstname
in>>firstname;
  
//run a loop until end of file is reached
while(in)
{
//set sumofscores to 0
sumOfScores=0;
//read lastname
in>>lastname;
  
//run a loop for 10 times and read the scores into scores array
for(i=0;i<10;i++)
{
in>>scores[i];
//add current score to sumofscores
sumOfScores=sumOfScores+scores[i];
}
//calculate average
avg= sumOfScores/10.0;
  
//write firstname and lastname to output file
out<<firstname<<\" \"<<lastname<<\" \";

   //output to console
   cout<<lastname<<\" \"<<firstname<<\" \";
  
//write scores to output file
for(i=0;i<10;i++)
out<<scores[i]<<\" \";
  
//write average to output file
out<<avg<<endl;
   //output to console
cout<<avg<<endl;
//read next firstname
in>>firstname;
}
}

Help find what is wrong with this program?? C++ Sample Output: Console (given provided input file) Damian Hess 6.5 Andrew Nielsen 6.2 Alfred Yankovic 6.9 Inga S
Help find what is wrong with this program?? C++ Sample Output: Console (given provided input file) Damian Hess 6.5 Andrew Nielsen 6.2 Alfred Yankovic 6.9 Inga S
Help find what is wrong with this program?? C++ Sample Output: Console (given provided input file) Damian Hess 6.5 Andrew Nielsen 6.2 Alfred Yankovic 6.9 Inga S
Help find what is wrong with this program?? C++ Sample Output: Console (given provided input file) Damian Hess 6.5 Andrew Nielsen 6.2 Alfred Yankovic 6.9 Inga S

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site