所以我正在努力從文件中讀取文本(逐行)並輸出ID
,ID
之後的4個等級的平均值以及字母等級。因此,任何平均等級爲50或以上的信件等級爲S
,低於50的信件等級爲U
,並且2個免責的等級導致信件等級I
。 (如果超過或等於2,則不用S或U)。從文本中讀取數字並輸出平均值
因此,可以說文件具有數字:
42 50 51 57 52
48 90 -1 60 -1
40 46 -1 59 45
47 50 -1 49 50
輸出應該是這個樣子:
ID=42 Avg=52.5 Grade=S
ID=48 Excused=2 Grade=I
ID=40 Avg=50.0 Grade=S
ID=47 Avg=49.7 Grade=U
類型
S U I
2 1 1
This is the output I am receiving from my code
的等級數它讀取所有的數字,但我需要它讀取第一個數字作爲ID
和4個數字作爲等級。
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream in;
ofstream results;
string filename;
char grade;
int S = 0, U = 0, I = 0, i = 0, ID, excused = 0, avg;
double allscores = 0;
cout << "Enter the name of the file that has the scores of students: " << endl;
cin >> filename;
cout << "Enter the number of scores for each student: " << endl;
cin >> ID;
in.open(filename);
results.open("results.txt");
if (in)
{
while (in >> ID)
{
int first = 0
for (i = 0; i<=4; i++)
{
if (first == -1)
excused++;
else
allscores += first;
}
if (first > 4)
{
avg = allscores/(4 - excused);
if (avg >= 50.0)
{
grade = 'S';
S++;
cout << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
}
else
{
grade = 'U';
U++;
cout << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
}
}
else
{
grade = 'I';
I++;
cout << "ID=" << ID << " Excused=" << excused << " Grade =" << grade << endl;
}
}
}
else
{
cout << "Couldn't open file\n";
}
cout << "Number of Grades of Type" << endl;
cout << "S " << "U " << "I" << endl;
cout << S << " " << U << " " << I << endl;
in.close();
results.close();
system("pause");
return 0;
}
你只是讀ID而不是數字的其餘部分 – Pooya
我能做些什麼來解決這個問題?我對整個文件很陌生......我應該添加另一個else語句嗎? – xdigit
原諒是什麼意思? – v7d8dpo4