#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 (1);
}
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;
}
else if (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;
}
else if (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_SUCCESS);
}
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 (1);
}
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 (1);
}
}
}
}
fin.close();
return 1;
}
|