检查字符数组 - 每次返回相同的答案

时间:2015-05-27 14:44:43

标签: c++ arrays char

所以我一直在为一项作业编程 - 我应该创建一个程序,询问问题的数量,正确的答案以及为测试提供的答案。在我写的程序中,我总是获得相同的得分回报值: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);
}

}

1 个答案:

答案 0 :(得分:1)

您尚未初始化corr_counter

你将它声明在顶部,但你从未将它初始化为0,这就是你的意思。因此,您从一个未知的起点开始递增。