我一直在試圖自我教自己如何編寫代碼,但是這個練習題難度很大!任何幫助將不勝感激。我已經完成了代碼和邏輯,但只有一個isuee。Basic Prog Prob
#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++;
}
}
}
.dat文件只是一個帶有ID號和Score的文本文件。這裏的想法是,如果ID與檢查分數相同,則它讀入分數和ID。應該拋出最高分和最低分,其餘的平均分。所以,我的邏輯是它將所有分數相加,無論如何,只有在ID變化之後,您纔會從總數中減去最高和最低分數,然後從計數中減去-2以平均分數。我的問題是,當我輸入一個ID和它的不同它也會輸入一個分數,新ID的第一個分數會被跳過。任何幫助將不勝感激。