2016-03-01 41 views
-2

對於我來說,我負責創建一個具有一組預先給定的正確答案的多項選擇測試。提示用戶輸入20個答案,然後該程序應該將正確的答案與學生輸入的答案進行比較。我很困惑重寫數組的大小,因爲我試圖將所有學生的正確和不正確答案放入兩個新數組中,然後輸出給用戶以查看他/她得到的正確答案。在C++中使用數組進行多項選擇測試的困惑

我的代碼是:

#include<iostream> 
#include<string> 
using namespace std; 

int main() 
{ 
char answers[20] = { 'A','D','B','B','C','B','A','B','C','D','A','C','D','B','D','C','C','A','D','B' }; 
char studentAnswers[20]; 
int correctlyAnswered[20]; 
int falselyAnswered[20]; 
for (int i = 0; i <= 19; i++) 
{ 
    cout << "Please enter your answer for question #" << i+1 << ": "; 
    char choice; 
    cin >> choice; 
    if (choice != 'A' || choice != 'B' || choice != 'C' || choice != 'D') 
    { 
     cout << "Error: Answer must be A,B,C or D. Please enter a new answer: "; 
     cin >> choice; 
    } 
    studentAnswers[i] = choice; 
    cin.sync(); 
    if (answers[i] = studentAnswers[i]) 
    { 
     studentAnswers[i] = correctlyAnswered[i]; 
    } 
    else 
    { 
     studentAnswers[i] = falselyAnswered[i]; 
    } 


} 
cout << "You answered " << sizeof(correctlyAnswered[20]) << " questions correctly out of 20" << endl; 
system("pause"); 
return 0; 

}

代碼心不是完全完成,但我想出來繼續之前解決這問題。我相信我的問題源於將正確答覆和falselyAsnswered陣列的大小設置爲20,但我不確定該如何解決此問題。請幫忙!

+1

具體而言,您遇到什麼問題或錯誤?其次,我懷疑你想使用'if(answers [i] = studentAnswers [i])'這個語句,因爲'='是賦值,你可能在尋找一個相等檢查'=='。 – dylansturg

+0

對不起,我應該指定更多。我希望能夠將所有正確和不正確的答案保存到2個獨立的數組中,然後將其輸出供用戶查看。我很困惑這些數組的大小是多少,因爲每次程序運行時回答的正確/不正確數量的問題都會改變。 – sundowned

+0

你是否需要在數組中保留答案?考慮使用std :: map將問題編號映射到一個結構,該結構保存用戶給出的正確答案和答案。我認爲這更接近你的意圖。 – selyunin

回答

0

我建議每增加學生正確回答時間int

因此,我將修改代碼聲明:

int correctlyAnswered = 0; 

然後在for週期我想:

if (answers[i] == choice) 
    correctlyAnswered++; 

(注意:==沒有=
最後,你可以輸出:

cout << "You answered " << correctlyAnswered << " questions correctly... 

其他一些建議:
- 使用while週期來獲取用戶輸入

while(1) { 
    cin >> choice; 
    if (choice != 'A' || choice != 'B' || choice != 'C' || choice != 'D') 
    cout << "Error: Answer must be A,B,C or D. Please enter a new answer: "; 
    else 
    break; 
} 
  • 計算falselyAnswered:

    falselyAnswered = 20 - correctlyAnswered;

更新 爲了將sutdent答案正確的存儲問題,我將宣佈一個布爾值數組:

int correctlyAnswered = 0; 
bool correct[20]; 

然後修改,如果:

if (answers[i] == choice) { 
    correctlyAnswered++; 
    correct[i] = true; 
} else { 
    correct[i] = false; 
} 

然後打印出結果:

cout << "You answered " << correctlyAnswered << " questions correctly"<<endl; 
for(int i=0; i<20; i++) { 
    count << "Your answer to question " << i+1 << " is "; 
    if (correct[i]) 
     cout << "correct" << endl; 
    else 
     cout << "wrong" << endl; 
} 
+0

有沒有一種方法可以讓我仍然可以輸出哪些問題被錯過或使用您的方法正確猜測?我最初使用數組獲取正確和不正確答案的原因是爲了在代碼結束時輸出它們。 – sundowned

+0

是的,請參閱我的更新。 – Matteo

0

您的代碼有幾個問題。第一招:

if (answers[i] = studentAnswers[i]) 

這就賦予answers[i]studentAnswers[i]。你想使用== - 這是一個比較運算符。

然後,

studentAnswers[i] = correctlyAnswered[i]; 

並沒有真正意義。 correctlyAnswered是一個單元數據的數組,你爲什麼從它讀取任何東西?我假設,你想以某種方式設置studentAnswers[i]

cout << "You answered " << sizeof(correctlyAnswered[20]) << " 

sizeof(studentAnswersed[20])總是返回相同的值,也就是char大小,它總是存在保證爲1

+0

謝謝你的幫助!我想我很困惑,因爲每次運行代碼時,正確和不正確答案的數量都會改變,因此我不確定將正確答覆和錯誤答案數組的大小設置爲什麼。我想我可以使用一個變量,因爲它的大小會在每次運行代碼時被修改,儘管我並不完全知道如何去做。 – sundowned

0

我想這不是真正的問題,但我們都在這裏幫忙。你的問題是一個很基本的一個:

if (answers[i] = studentAnswers[i]) 

由於沒有分配和比較,C++之間的差異采用不同的運營商這一點。 (你必須習慣它),而不是使用一個單一的平等,比較是一個雙等號==

在這個例子中,你正在測試的任務。所以你分配studentAnswer回答,比檢查分配的東西是否與false不同(或者在這種情況下'\ 0')

我不知道你使用的是哪個編譯器和IDE,對此的警告:使用賦值結果作爲沒有括號的條件

這就是說,請停止編寫C. STL具有數組和矢量的容器。結合基於範圍的算法,...這段代碼將更容易理解。

+0

謝謝你的幫助。在我參加的社區學院課程中,這是迄今爲止我學到的唯一方法,我對這些混淆的代碼表示歉意。我會根據您的建議尋求改進,再次感謝! – sundowned

0

首先,您必須使用'=='而不是'='第一個是比較運算符,用於評估兩個聲明是否相等,第二個是將一個變量的值賦值給另一個的賦值運算符。

其次,您必須在您評估正確答案時交換順序。相反的:

if (answers[i] = studentAnswers[i]) 
{ 
    studentAnswers[i] = correctlyAnswered[i]; 
} 
else 
{ 
    studentAnswers[i] = falselyAnswered[i]; 
} 

你要做這樣的:

if (answers[i] = studentAnswers[i]) 
{ 
    correctlyAnswered[i] = studentAnswers[i]; 
} 
else 
{ 
    falselyAnswered[i] = studentAnswers[i]; 
} 

三,聲明的sizeof(correctlyAnswered [20])將始終輸出80,that's bacause的sizeof()返回大小以字節爲單位,在這種情況下爲80。

那麼,應該這樣做。

現在,如果你想顯示你應該增加這個好答案:

int correctNumbers[20] = {};//this initializes the 20 numbers with 0 
if語句

然後:

if (answers[i] = studentAnswers[i]) 
{ 
    correctlyAnswered[i] = studentAnswers[i]; 
    correctNumbers[i] = i + 1;//stores the number of the good answeded                             //question 
} 

最後以顯示:

cout << "You answeded well the next questions:\n" 
for(i = 0; i < 20; ++i) 
    if(correctNumber[i] != 0) 
     cout << correctNumber[i] << ' '; 

那工作,有一個更好的方法來解決這個問題,但是這對你正在努力解決的問題已經足夠了:)

0

我按如下格式化了最終代碼。

#include<iostream> 
#include<string> 
using namespace std; 

int main() 
{ 
char answers[20] = { 'A','D','B','B','C','B','A','B','C','D','A','C','D','B','D','C','C','A','D','B' }; 
char studentAnswers[20]; 
int correctlyAnswered = 0; 
bool correct[20]; 
int falselyAnswered[20]; 
for (int i = 0; i <= 19; i++) 
if (answers[i] == choice) 
    correctlyAnswered++; 
{ 
    cout << "Please enter your answer for question #" << i+1 << ": "; 
    char choice; 
    cin >> choice; 
    if (choice != 'A' || choice != 'B' || choice != 'C' || choice != 'D') 
    cout << "Error: Answer must be A,B,C or D. Please enter a new answer: "; 
    cin >> choice; 
    else 
    break; 
} else { 
    cout << "You answered " << correctlyAnswered << " questions correctly... 
    } 
    studentAnswers[i] = choice; 
    cin.sync(); 
    if (answers[i] == choice) { 
     correctlyAnswered++; 
     correct[i] = true; 
} else { 
     correct[i] = false; 
    } 

     studentAnswers[i] = correctlyAnswered[i]; 
    } 
    else 
    { 
     studentAnswers[i] = falselyAnswered[i]; 
    } 
     falselyAnswered = 20 - correctlyAnswered; 

} 
cout << "You answered " << correctlyAnswered << " questions correctly"<<endl; 
     for(int i=0; i<20; i++) { 
    count << "Your answer to question " << i+1 << " is "; 
     if (correct[i]) 
     cout << "correct" << endl; 
    else 
     cout << "wrong" << endl; 
} 

system("pause"); 
return 0;