This c++ program opens input files and reads lines of names and grades and then
ID: 3590283 • Letter: T
Question
This c++ program opens input files and reads lines of names and grades and then outputs a list of averages to a specified output file. When I try to compile the program, I get a list of errors that I don't know how to fix.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
cout << endl;
if(argc != 3)
{
cout << "Program reguires 2 arguments: "
<< "one input filename and one output file name" << endl << endl
<< "Program usage is:" << endl
<< argv[0] << "InputFileName OutputFileName" << endl;
return 1;
}
}
string inFileName = argv[1];
ifstream inFile;
infile.open(inFileName.c_str(), ios::in);
cout << "Opening the Input File: " << inFileName << endl << endl;
while(!inFile)
{
cout << string(15, '*') << "ERROR WHILE OPENING FILE "
<< string(15, '*') << endl
<< "Input file failed to open properly" << endl
<< "Attempted to open file: " << inFileName << endl
<< "Try another input file: " << endl
<< string(47, '*') << end << endl
<< "Enter the input file name: ";
cin >> inFileName;
inFile.open(inFileName.c_str(), ios::in);
cout << inFileName << endl << endl;
}
string outFileName = argv[2];
ofstream outfile;
outFile.open(outFileName.c_str(), ios::out);
cout << "Opening the Output File: " << outFileName << endl << endl;
string output = " ";
while (!outFile)
{
cout << string(15, '*') << "ERROR WHILE OPENING FILE "
<< string (15, '*') << endl
<< "Output file failed to open properly" << endl
<< "Attempted to open file: " << outFileName << end
<< "Try another output file name " << endl
<< string(47, '*') << endl << endl
<< "Enter the output file name: ";
cin >> outFileName;
outFile.open(outFileName.c_str(), ios::out);
cout << outFileName << endl << endl;
output = outFileName
}
int QuizNum;
int HomeworkNum;
int TestNum;
inFile >> QuizNum >> HomeworkNum >> TestNum;
if(inFile.eof())
{
cout << string(13, '*') << "The Input File is Empty "
<< string(13, '*') << endl
<< "Terminating the program" << endl
<< string (47, '*') << endl << endl;
outFile <, "Input file; " <, inFileName << "is empty";
return 1;
}
inFile.ignore(8);
int MaxQuizScore = 0;
int MaxHomeworkScore = 0;
int MaxTestScore = 0;
for(int i = 0; i < QuizNum; i++)
{
int quiz = 0;
inFile >> quiz;
MaxQuizScore += quiz;
}
for(int i = 0; i < HomeworkNum; i++)
{
int homework = 0:
inFile >> homework;
MaxHomeworkScore += homework;
}
for(int i = 0; i < TestNum; i++)
{
int test = 0;
inFile >> test;
MaxTestScore += test;
}
int TotalMaxScore = MaxQuizScore + MaxHomeworkScore + MaxTestScore;
outFile << "# Last First Quiz HW Test Total Average " << endl
<< "- ---------- ----- ---- --- ---- ----- -------" << endl
<< " Key Key " << left << setw(4) << MaxQuizScore << " "
<< setw(3) << MaxHomeworkScore << " "
<< setw(4) << MaxTestScore << " "
<< setw(5) << TotalMaxScore << " "
<< setw(7) << "100.00 " << endl
<< "- ---------- ----- ---- --- ---- ----- ------- " endl;
float ClassAverage
int StudentNum = 0;
while(true)
{
string LastName;
inFile >> LastName;
if(inFile.eof())
{
break;
}
StudentNum++;
string FirstName;
inFile >> FirstName;
int TotalQuizScore = 0;
for (int i = 0; i < QuizNum; i++)
{
int QuizScore = 0;
inFile >> QuizScore;
TotalQuizScore += QuizScore;
}
int TotalHomework Score = 0;
for(int i = 0; i < HomeworkNum; i++)
{
int HomeworkScore = 0;
inFile >> HomeworkScore;
TotalHomeworkScore =+ HomeworkScore;
}
int TotalTestScore = 0;
for(int i = 0; i < TestNum; i++)
{
int TestScore = 0;
inFile >> TestScore;
TotalTestScore += TestScore;
}
int TotalScore = TotalQuizScore + TotalHomeworkScore + TotalTestScore;
float TotalAverage = (float(TotalScore)/float(TotalMaxScore)*100.0);
ClassAverage += Total Average;
outFile << setprecision(0) << StudentNum << " " << left
<< setw(10) << LastName.substr(0,10) << " "
<< setw(5) << FirstName.substr(0,5) << " "
<< setw(4) << TotalQuizScore << " "
<< setw(3) << TotalHomeworkScore << " "
<< setw(4) << TotalTestScore << " "
<< setw(5) << TotalScore << " "
<< fixed << setprecision(2) << setw(7) << TotalAverage << " " << endl;
if(!StudentNum)
{
cout << "Input file contains no data" << endl;
outfile << "Input file contains no data";
}
else
{
ClassAverage /= StudentNum;
outFile << "- ---------- ----- ---- --- ---- ----- ------- " << endl
<< endl
<< "Class Average = " << ClassAverage;
}
inFile.close();
outFile.close();
return 0;
}
Explanation / Answer
There was a lots of typing error. See the corrected one bellow.
If you have nay dout ask in comment;
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
int main(int argc, char* argv[])
{
cout << endl;
if(argc != 3)
{
cout << "Program reguires 2 arguments: "
<< "one input filename and one output file name" << endl << endl
<< "Program usage is:" << endl
<< argv[0] << "InputFileName OutputFileName" << endl;
return 1;
}
string inFileName = argv[1];
ifstream inFile;
inFile.open(inFileName.c_str(), ios::in);
cout << "Opening the Input File: " << inFileName << endl << endl;
while(!inFile)
{
cout << string(15, '*') << "ERROR WHILE OPENING FILE "
<< string(15, '*') << endl
<< "Input file failed to open properly" << endl
<< "Attempted to open file: " << inFileName << endl
<< "Try another input file: " << endl
<< string(47, '*') << endl << endl
<< "Enter the input file name: ";
cin >> inFileName;
inFile.open(inFileName.c_str(), ios::in);
cout << inFileName << endl << endl;
}
string outFileName = argv[2];
ofstream outFile;
outFile.open(outFileName.c_str(), ios::out);
cout << "Opening the Output File: " << outFileName << endl << endl;
string output = " ";
while (!outFile)
{
cout << string(15, '*') << "ERROR WHILE OPENING FILE "
<< string (15, '*') << endl
<< "Output file failed to open properly" << endl
<< "Attempted to open file: " << outFileName << endl
<< "Try another output file name " << endl
<< string(47, '*') << endl << endl
<< "Enter the output file name: ";
cin >> outFileName;
outFile.open(outFileName.c_str(), ios::out);
cout << outFileName << endl << endl;
output = outFileName;
}
int QuizNum;
int HomeworkNum;
int TestNum;
inFile >> QuizNum >> HomeworkNum >> TestNum;
if(inFile.eof())
{
cout << string(13, '*') << "The Input File is Empty "
<< string(13, '*') << endl
<< "Terminating the program" << endl
<< string (47, '*') << endl << endl;
outFile << "Input file; " << inFileName << "is empty";
return 1;
}
inFile.ignore(8);
int MaxQuizScore = 0;
int MaxHomeworkScore = 0;
int MaxTestScore = 0;
for(int i = 0; i < QuizNum; i++)
{
int quiz = 0;
inFile >> quiz;
MaxQuizScore += quiz;
}
for(int i = 0; i < HomeworkNum; i++)
{
int homework = 0;
inFile >> homework;
MaxHomeworkScore += homework;
}
for(int i = 0; i < TestNum; i++)
{
int test = 0;
inFile >> test;
MaxTestScore += test;
}
int TotalMaxScore = MaxQuizScore + MaxHomeworkScore + MaxTestScore;
outFile << "# Last First Quiz HW Test Total Average " << endl
<< "- ---------- ----- ---- --- ---- ----- -------" << endl
<< " Key Key " << left << setw(4) << MaxQuizScore << " "
<< setw(3) << MaxHomeworkScore << " "
<< setw(4) << MaxTestScore << " "
<< setw(5) << TotalMaxScore << " "
<< setw(7) << "100.00 " << endl
<< "- ---------- ----- ---- --- ---- ----- ------- " <<endl;
float ClassAverage;
int StudentNum = 0;
while(true)
{
string LastName;
inFile >> LastName;
if(inFile.eof())
{
break;
}
StudentNum++;
string FirstName;
inFile >> FirstName;
int TotalQuizScore = 0;
for (int i = 0; i < QuizNum; i++)
{
int QuizScore = 0;
inFile >> QuizScore;
TotalQuizScore += QuizScore;
}
int TotalHomeworkScore = 0;
for(int i = 0; i < HomeworkNum; i++)
{
int HomeworkScore = 0;
inFile >> HomeworkScore;
TotalHomeworkScore =+ HomeworkScore;
}
int TotalTestScore = 0;
for(int i = 0; i < TestNum; i++)
{
int TestScore = 0;
inFile >> TestScore;
TotalTestScore += TestScore;
}
int TotalScore = TotalQuizScore + TotalHomeworkScore + TotalTestScore;
float TotalAverage = (float(TotalScore)/float(TotalMaxScore)*100.0);
ClassAverage += TotalAverage;
outFile << setprecision(0) << StudentNum << " " << left
<< setw(10) << LastName.substr(0,10) << " "
<< setw(5) << FirstName.substr(0,5) << " "
<< setw(4) << TotalQuizScore << " "
<< setw(3) << TotalHomeworkScore << " "
<< setw(4) << TotalTestScore << " "
<< setw(5) << TotalScore << " "
<< fixed << setprecision(2) << setw(7) << TotalAverage << " " << endl;
if(!StudentNum)
{
cout << "Input file contains no data" << endl;
outFile << "Input file contains no data";
}
else
{
ClassAverage /= StudentNum;
outFile << "- ---------- ----- ---- --- ---- ----- ------- " << endl
<< endl
<< "Class Average = " << ClassAverage;
}
}
inFile.close();
outFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.