2016-04-30 62 views
1

我的代碼有點麻煩。它幾乎應該打開兩個文件,並將文件「StudentAnswers.txt」的第20行([作爲char輸入到char數組]中)與另一個文件的每行中的char值進行比較「CorrectAnswers.txt」在另一個陣列中的相同位置(索引)。這就像一個線性搜索,但在陣列中的位置相同。然後應該顯示一個報告,詳細說明學生錯過了哪個問題,給出的答案,正確答案,以及學生是否通過了(> 70%),如下所示:多次讀入數組

學生X報告: 2(A/D),3(C/D),5(D/A) 這位學生通過了考試!

然後它應該清除SAArray,並從StudentAnswers.txt中提供下20行,並重新開始整個過程​​。我想程序必須從('StudentAnswers.txt'文件/ 20行)確定學生人數。

我在顯示報告時遇到問題,並且在程序之後讓陣列自行清除。我猜測這可以通過一個while循環和一個累加器來完成(由上面的公式確定)。 此外,Visual Studio似乎去「錯過__問題總共___%」,然後保持循環-858993460。

任何幫助,將不勝感激。

#include <iostream> 
#include <fstream> 
#include <string> 
#include <array> 
#include <algorithm> 

using namespace std; 

void GradeReturn(char[], char[], int, int, int); 
string PassFail(float); 

int main() 
{ 
    ifstream SA("StudentAnswers.txt"); 
    ifstream CA("CorrectAnswers.txt");char CAArray[20]; 
    char SAArray[20]; 
    // char SA2Array[20]; 
    bool isCorrect; 
    int correct; 
    int incorrect; 
    int counter; 

    correct = 0;incorrect = 0; 
    counter = 0; 

    cout << endl; 


    if (!SA.fail()) 
    { 
     cout << "'StudentAnswers.txt' file opened successfully." << endl; 
     cout << "'CorrectAnswers.txt' file opened successfully." << endl << endl; 

     int a = 0; 
     int b = 0; 

     while (a < 20) 
     { 
      CA >> CAArray[a]; 
      a++; 
     } // while loop to feed char into the array 

     while (b < 20) 
     { 
      SA >> SAArray[b]; 
      b++; 

     } 
    } // while loop to feed char into array 

    CA.close(); // closing "CorrectAnswers.txt" 
    SA.close(); // closing "StudentAnswers.txt" 

    GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter); 

    return 0; 
} 

void GradeReturn(char CAArray[], char SAArray[], int correct, int incorrect, int counter) 
{ 
    float percent; 
    float hundred; 
    int student; 
    int catcher[20]; 
    int writeCatcher; int starter; 
    int catcher_size; 

    student = 0; 
    writeCatcher = 0; 
    catcher_size = ((sizeof catcher)/4); 

    while (counter < 20) 
    { 

     if ((CAArray[counter]) == (SAArray[counter])) 
     { 
      correct++; 
      cout << "Good job!" << endl; 
     } // correct handling 
     else 
     { 
      incorrect++; 
      cout << "You got question " << counter << " wrong." << endl; 
      counter >> catcher[writeCatcher]; 

      writeCatcher++; 
     } // incorrect handling 

     counter++; 
    } // while loop to determine if a student got a question right or wrong 

    static_cast <float> (incorrect); // float conversion 

    cout << endl; // for cleanliness 

    percent = ((static_cast <float> (correct))/20); // percentage 
    hundred = percent * 100; 

    PassFail(percent); 


    if (PassFail(percent) == "pass") 
    { 
     student++; 
     cout << "Report for Student " << student << ":" << endl; 
     cout << "-----------------------------" << endl; 

     cout << "Missed " << incorrect << " questions out of 20 for "; 
     cout << hundred << " % correct." << endl << endl; 

     starter = 0; 

     while (starter < (sizeof catcher) 
     { 
      if(1=1) 
      { 
       catcher_size 
      } 

      else 
      { 
       cout << ""; 
       starter++; 
      } 
     } 

    } 
    else if (PassFail(percent) == "fail") 
     { 
      student++; 
      cout << "Missed " << incorrect << " questions out of 20 for "; 
      cout << hundred << " % correct." << endl << endl; 

      while (starter < catcher_size) 
      { 
       if ((catcher[starter]) == -858993460) 
       { 
        starter++; 
       } 
       else 
       { 
        cout << ""; 
        starter++; 
       } 
      } 

     } 

    return; 
} 

string PassFail(float percent) 
{ 
    if (percent >= 0.70) // if <pass> 
    { 
     return "pass"; 
    } 
    else // if <fail> 
    { 
     return "fail"; 
    } 
    cout << endl; 
} 
+0

顯示標籤C++沒有C. – fluter

+0

你可以考慮直接返回True或False來通過或失敗並跳過字符串比較。如果您想「讓它看起來更好」,請考慮使用枚舉映射回來並通過失敗。 – ti7

回答

0

要得到一個循環,你應該保持流打開,而不是在閱讀20行後關閉它們。

僞代碼將是:

a = 0; 
while(streams_not_empty) 
{ 
    CA >> CAArray[a]; 
    SA >> SAArray[a]; 
    ++a; 
    if (a == 20) 
    { 
     GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter); 

     a = 0; // Reset a 
    } 
} 

CA.close(); // closing "CorrectAnswers.txt" 
SA.close(); // closing "StudentAnswers.txt" 

您還需要按引用傳遞correct, incorrect, counter使得GradeReturn可以改變他們的價值,他們通過做積累。

像:

void GradeReturn(char CAArray[], char SAArray[], int& correct, int& incorrect, int& counter) 

而且你不應該依賴於能夠從文件中每次讀取準確NX20線。一個文件可能有,例如108(5x20 + 8)行,所以你的代碼應該只能處理8行。換句話說,不要像while (counter < 20)那樣在你的函數中硬編碼20。而是傳遞要處理的行數,並執行while (counter < number_to_handle)

事情是這樣的僞代碼:

a = 0; 
while(streams_not_empty) 
{ 
    CA >> CAArray[a]; 
    SA >> SAArray[a]; 
    ++a; 
    if (a == 20) 
    { 
     GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter, a); 
                           //^

     a = 0; // Reset a 
    } 
} 

if (a != 0) 
{ 
    // Process the rest 
    GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter, a); 
} 

CA.close(); // closing "CorrectAnswers.txt" 
SA.close(); // closing "StudentAnswers.txt" 
0

一個問題你已經是你想比較的==運營商C風格的字符串。這將比較它們,就好像它們是指向char的指針,即比較它們是否指向內存中的相同位置,而不是比較字符串的內容。我強烈建議你查看數組衰減和c字符串變量以瞭解更多信息。

具體來說,if (PassFail(percent) == "pass")是不會做你想做的。 strcompdoc,strncmpdoc使用std::string變量而不是c風格的字符串都可以工作,但是將percent直接與if(percent >= 0.70直接比較可能會更好,而不是調用PassFail並比較字符串。

這裏還有很多其他問題,你在一個地方調用PassFail,但是對返回值沒有任何作用。 PassFail的唯一影響是cout << endl,如果這是你的意圖,這是一個糟糕的決定,很難讀取在控制檯上放置換行符的方式。

嘗試詢問您的編譯器是否有更多警告,這通常有助於發現這些類型的問題。 -Wall -Wextra爲gcc工作,你可能需要閱讀你的編譯器手冊......