0
所以我一直在爲一個任務編寫程序 - 我應該創建一個程序,要求提供多少個問題,正確的答案以及提供給測試評分的答案。在我寫的程序中,我總是得到相同的得分值:4199676檢查字符數組 - 每次返回相同的答案
有誰能告訴我爲什麼我得到這個返回值嗎?非常感謝。
int main(){
int qnum = 1;
int counter;
int corr_counter;
char correct[10000];
char answer[10000];
while(qnum != 0){
cout<<"Enter the number of questions on the test (0 to exit).\n";
cin>>qnum;
while(qnum < 0){
cout<<"Please enter a valid number of questions.\n";
cin>>qnum;
}
for(counter = 0; counter < qnum; counter++){
cout<<"Enter the correct answer for question "<<counter<<". The answer can be A, B, C, D, or E.\n";
cin>>correct[counter];
toupper(correct[counter]);
while(correct[counter] != 'A' && correct[counter] != 'B' && correct[counter] != 'C' && correct[counter] != 'D' && correct[counter] != 'E'){
cout<<"Please enter either A, B, C, D, or E.\n";
cin>>correct[counter];
toupper(correct[counter]);
}
}
for(counter = 0; counter < qnum; counter++){
cout<<"Enter the student's answer for question "<<counter<<". The answer can be A, B, C, D, or E.\n";
cin>>answer[counter];
toupper(answer[counter]);
while(answer[counter] != 'A' && answer[counter] != 'B' && answer[counter] != 'C' && answer[counter] != 'D' && answer[counter] != 'E'){
cout<<"Please enter either A, B, C, D, or E.\n";
cin>>answer[counter];
toupper(answer[counter]);
}
}
for(counter = 0; counter < qnum; counter++){
if(answer[counter] == correct[counter]){
corr_counter++;
}
}
cout<<"Score: "<<corr_counter<<"\n";
return(0);
}
}
您沒有初始化'corr_counter',所以變量似乎在給定的內存位置有任何值。只需將它初始化爲'0'即可。 – foobar
但是這就是爲什麼「不調試」是關閉問題的原因。如果你調試了你的代碼,你應該馬上看到了這個問題。下一次,請調試您的代碼。 – PaulMcKenzie