这是一段从钱能C++上摘下的,为什么达不到预期效果

来源:百度知道 编辑:UC知道 时间:2024/09/24 19:13:02
#include<iostream>
using namespace std;
void main(void)
{
char key[]={'a','c','b','c','d'};
char c;
int ques=0,numques=5,numcorrect=0;
cout<<"Enter the"<<numques<<"questiontests"<<endl;
while(cin.get(c))
{
if(c!='\n')
if(c==key[ques])
{numcorrect++;cout<<" ";
}
else cout<<"*";
else
{
cout<<"\nscore"<<float(numcorrect/numques)*100<<"%";
ques=0;
numcorrect=0;
cout<<endl;
continue;
}
ques++;
}
}
请各位学长帮我查一下,万分感谢!

cout<<"\nscore"<<float(numcorrect/numques)*100<<"%"; 这里你打错括号了,应该是cout << "\nscore " << float(numcorrect) / numques*100 << "%"; float应该是强制转换numcorrect才对的。给你改过来了,顺便修改了一下,输入答案正确的显示!号,不正确显示*号
全部代码如下:
#include<iostream>

using namespace std;
void main(void)
{
char key[] = {'a', 'c', 'b', 'c', 'd'};
char c;
int ques = 0, numques = 5, numcorrect = 0;
cout << "Enter the " << numques << " questiontests" << endl;

while (cin.get(c))
{
if (c != '\n')
if (c == key[ques])
{
numcorrect++;
cout << "!";
}
else cout << "*";
else
{
cout