#include <fstream>
#include <iostream>
#include <iomanip> // for output formatiing
#include <cstdlib> // for exit(), EXIT_SUCCESS,
EXIT_FAILURE
#include <cctype> // for isspace()
using namespace std;
int main()
{
int checkInputFile();
if( ! checkInputFile() )
{
cout << "Input file is not
in correct format." << endl;
exit(EXIT_FAILURE);
}
ifstream fin("./scores.dat", ios::in);
ofstream fout("./output.dat", ios::out);
if( fin.fail() )
{
cout << "Error opening input
Score file scores.dat" << endl;
exit(EXIT_SUCCESS);
}
if( fout.fail() )
{
cout << "Error opening output
Score file output.dat" << endl;
exit(EXIT_SUCCESS);
}
int lineNum = 0, numOfValues, value, numOfTests;
int leastScore = 0, secondLeastScore = 0, stdID, totalScore
= 0;
fin >> ws;
// "eat" up any preceding whitespaces
while ( ! fin.eof() ) // as long as eof has
not been encountered
{
lineNum++;
fin >> value;
if(lineNum == 1)
{
numOfTests = value;
fout <<
"Maximum number of scores each student can have is " << numOfTests
<< "." << endl;
fout <<
endl;
fout <<
setiosflags(ios::fixed)
<< setw(20)
<< "Student ID"
<< setw(20)
<< "Quizzes Taken"
<< setw(20)
<< "Quizzes Missed"
<< setw(20)
<< "Scores Average"
<< resetiosflags(ios::fixed)
<< endl;
fout <<
setiosflags(ios::fixed)
<< setw(20)
<< "----------"
<< setw(20)
<< "--------------"
<< setw(20)
<< "--------------"
<< setw(20)
<< "--------------"
<< resetiosflags(ios::fixed)
<< endl;
continue;
}
numOfValues = 1;
stdID = value;
while ( fin.peek() != '\n'
)
// next character is not "newline"
{
if ( isspace(
fin.peek() ) || fin.peek() == '$' ) // next character is whitespace
fin.ignore();
// read and discard next character
else
{
// next character is part of a value
fin >> value;
totalScore += value;
if(numOfValues == 1)
{
leastScore = value;
numOfValues++;
continue;
}
if(numOfValues == 2)
{
secondleastScore = value;
numOfValues++;
continue;
}
if(secondleastScore > value)
{
if(leastScore > value)
{
leastScore = value;
}
else
{
secondleastScore = value;
}
}
numOfValues++;
}
}
float actualScore = 0, actualAvg
= 0;
numOfValues--;
if(numOfValues == numOfTests)
{
actualScore =
totalScore - leastScore - secondleastScore;
actualAvg = (float)actualScore
/ (numOfTests - 2);
fout <<
setiosflags (ios::fixed | ios::showpoint)
<< setprecision(1)
<< setw(20)
<< stdID
<< setw(20)
<< numOfValues
<< setw(20)
<< (numOfValues - numOfTests)
<< setw(20)
<< actualAvg
<< resetiosflags(ios::fixed)
<< endl;
}
elseif( numOfValues == (numOfTests
- 1) )
{
actualScore =
totalScore - leastScore;
actualAvg = (float)actualScore
/ (numOfTests - 1);
fout <<
setiosflags (ios::fixed | ios::showpoint)
<< setprecision(1)
<< setw(20)
<< stdID
<< setw(20)
<< numOfValues
<< setw(20)
<< (numOfTests - numOfValues)
<< setw(20)
<< actualAvg
<< resetiosflags(ios::fixed)
<< endl;
}
elseif( numOfValues <= (numOfTests
- 2) )
{
actualAvg = (float)totalScore
/ (numOfTests - 2);
fout <<
setiosflags (ios::fixed | ios::showpoint)
<< setprecision(1)
<< setw(20)
<< stdID
<< setw(20)
<< numOfValues
<< setw(20)
<< (numOfTests - numOfValues)
<< setw(20)
<< actualAvg
<< resetiosflags(ios::fixed)
<< endl;
}
totalScore = 0;
fin >> ws;
}
fin.close();
fout.close();
return EXIT_SUCCESS;
}
// Check Input file.
int checkInputFile()
{
ifstream fin("./scores.dat", ios::in);
if( fin.fail() )
{
cout << "Error opening input
Score file scores.dat" << endl;
exit(EXIT_FAILURE);
}
fin >> ws;
int lineNum = 0, tests = 0,stdID = 0, value, numOfVal,
eolFlag = 0;
while( ! fin.eof() )
{
lineNum++;
fin >> value;
if(lineNum == 1)
{
tests = value;
numOfVal = 1;
while(fin.peek()
!= '\n')
{
if( isspace( fin.peek() ) )
{
fin.ignore();
}
else
{
numOfVal++;
}
}
if(numOfVal !=
1)
{
cout << "First line of the input file should contain only the no.
of tests." << endl;
exit(EXIT_FAILURE);
}
continue;
}
numOfVal = 0;
eolFlag = 0;
while ( fin.peek() != '\n' )
{
// next character
is not "newline"
if ( isspace(
fin.peek() ) ) // next character is whitespace
fin.ignore();
// read and discard next character
else
// next character is part of a value
{
fin >> value;
numOfVal++;
if(value == '$')
{
eolFlag = 1;
continue;
}
if(value < 0 || value > 100 || eolFlag == 1)
{
cout << "Input file contains invalid Student Scores." << endl;
exit(EXIT_FAILURE);
}
}
}
}
fin.close();
return 1;
} |