I’ve been trying to self-teach myself how to code but this practice problem is a toughie! Any help would be greatly appreciated. I have the code and logic done but there is just one isuee.
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ifstream fin;
int id, prev, score, scoree, high = 0, low = 0, total = 0, count = 1, counter = 0, counterr = 0;
double avg = 0;
fin.open("C:\\Users\\E\\Desktop\\Quizzes.dat");
fin >> prev;
fin >> score;
low = score;
high = score;
total = score;
while(!fin.eof())
{
fin >> id;
fin >> scoree; //Problem is that when the ID changes it still inputs a score which gets ignored
if ( counter == counterr && counter != 0 ) //Could of used a BOOL here...
{
high = scoree;
low = scoree;
total = 0;
count = 0;
}
if ( prev == id )
{
if (scoree > high)
{
high = scoree;
}
if (scoree < low)
{
low = scoree;
}
total = total + scoree;
count++;
counter = 0;
}
else
{
total = total - (high+low);
count = count - 2;
avg = total / count;
cout << "ID: " << prev << " " << "AVG: " << avg << endl;
prev = id;
counter++;
counterr++;
}
}
}
The .dat file is just a text file with an ID number and a Score. The idea here is that it reads in a score and id if the ID is the same it checks the scores. The highest and lowest score should be thrown out and the rest averaged. So What my logic is that it adds up all the scores regardless, and only after the ID changes do you subtract the highest and lowest score from total and then – 2 from the count to avg it out. The issue im having is that when I input an ID and its different It also inputs a score and that first score of the new id get skipped. Any help would be greatly appreciated.
Change this
To this:
Btw:
count,counter,counterr-> naming variables like this is sure recipe for losing your hair