2016-04-09 32 views
0

所以我正在努力從文件中讀取文本(逐行)並輸出IDID之後的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; 
} 
+0

你只是讀ID而不是數字的其餘部分 – Pooya

+0

我能做些什麼來解決這個問題?我對整個文件很陌生......我應該添加另一個else語句嗎? – xdigit

+0

原諒是什麼意思? – v7d8dpo4

回答

0

你的變量int first=0沒有被分配在你的代碼0以外的任何值,因此聲明if(first>4)將永遠是假的,導致你所得到的輸出。

你可以做這樣的事情:

while (in >> ID)//this is supposed to get the ID of each student, in order for that to happen you need to read all the 4 scores inside this loop before coming here and reading the next ID,otherwise everything will read as an ID 
    { 
     int first = 0 
     excused=0;//you need to reset excused on every iteration 
     allscores=0;//you need to reset allscore on every iteration 
     for (i = 0; i<4; i++) 
     {//here you need to get all the 4 scores 
      in>>first;//get each score in first 
      if (first == -1) 
      excused++; 
      else 
      allscores += first; 
     } 

     if(excused<2)//instead of if(first>4) 
      ...//do your calculation for average score 
     else 
      ...//set the grade to 'I'... 
    } 
0

這是我最終的解決方案:

#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; 
      excused = 0; 
      allscores = 0; 
      for (i = 0; i < 4; i++) 
      { 
       in >> first; 
       if (first == -1) 
        excused++; 
       else 
        allscores += first; 
      } 

      if (excused < 2) 
      { 
       avg = allscores/(4 - excused); 

       if (avg >= 50.0) 
       { 
        grade = 'S'; 
        S++; 
        results << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl; 

       } 
       else 
       { 
        grade = 'U'; 
        U++; 
        results << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl; 
       } 
      } 
      else 
      { 
       grade = 'I'; 
       I++; 
       results << "ID=" << ID << " Excused=" << excused << " Grade =" << grade << endl; 
      } 
     } 
    } 

    else 
    { 
     cout << "Couldn't open file\n"; 
    } 

    results << "Number of Grades of Type" << endl; 
    results << "S " << "U " << "I" << endl; 
    results << S << " " << U << " " << I << endl; 

    in.close(); 
    results.close(); 

    system("pause"); 
    return 0; 
} 

的代碼後,我把它輸出到一個名爲「結果」的文件。
感謝您的幫助。我想我的while循環是最大的錯誤。
尤其不要在in >> first部分添加。

相關問題