检查字符数组 - returns 每次都是相同的答案
Checking character arrays - returns same answer each time
所以我一直在为一项作业编程——我应该创建一个程序来询问问题的数量、正确答案以及为 test.In 程序打分的答案写了,我总是得到相同的 return 分数值:4199676
谁能告诉我为什么我得到这个 return 值?非常感谢。
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,这正是你想要做的。因此,您从一个未知的起点递增。
所以我一直在为一项作业编程——我应该创建一个程序来询问问题的数量、正确答案以及为 test.In 程序打分的答案写了,我总是得到相同的 return 分数值:4199676
谁能告诉我为什么我得到这个 return 值?非常感谢。
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,这正是你想要做的。因此,您从一个未知的起点递增。