测验程序总是评估答案是错误的
Quiz program always evaluating answer to be wrong
class Question{
protected:
int op1;
int op2;
string operate;
public:
Question();
};
class generateRandomQuiz:Question{
public:
generateRandomQuiz();
int getp1();
int getp2();
string getOp();
};
class checkAnswer:generateRandomQuiz{
private:
int Ans;
public:
checkAnswer(int ans);
};
Question::Question()
{
op1=23;
op2=12;
operate="/";
}
generateRandomQuiz::generateRandomQuiz():Question()
{
op1=rand()%50;
op2=rand()%50;
string s="+-/*";
int n=rand()%4;
operate=s[n];
}
int generateRandomQuiz::getp1()
{
return op1;
}
int generateRandomQuiz::getp2()
{
return op2;
}
string generateRandomQuiz::getOp()
{
return operate;
}
checkAnswer::checkAnswer(int ans):generateRandomQuiz()
{
Ans=ans;
string operate=getOp();
int op1=getp1();
int op2=getp2();
if (operate=="+")
{
if (op1+op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
if (operate=="-")
{
if (op1-op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
if (operate=="*")
{
if (op1*op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}if (operate=="/")
{
if (op1/op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
}
int main()
{
cout<<"This quiz is about evaluating an expression which is being generatedrandomly"
<<endl;
generateRandomQuiz Q;
int answer;
int op1=Q.getp1();
int op2=Q.getp2();
string opr=Q.getOp();
cout<<"What is: "<<op1<<op2<<op2<<"=?"<<endl;
cin>>answer;
checkAnswer A(answer);
system("PAUSE");
return 0;
}
我正在编写一个程序,该程序随机生成一个测验,并要求用户提供这样的答案:什么是:15/43 = ?运算符和数字是随机生成的。但是当我给出正确答案时,即使这样也会打印错误答案的评论。我已经写了很清楚的条件。有人可以指出来吗?谢谢
要检查答案,您依赖于checkAnswer
继承自generateRandomQuiz
的事实。
但是当你实际检查答案时,你使用的实例与随机生成的测验不同,所以你不能通过做int op1=getp1();
之类的事情来获得生成的测验。
通过这样做,您将仅获得默认的构造值,并且由于构造函数进行随机化,因此您将得到一个完全不同的问题。所以检查机制是正确的,它只是检查一个不同的问题。我建议你重新考虑你的代码结构,它看起来很尴尬。
相关文章:
- 使用 bfs 解决连接组件问题时得到错误的答案
- 递归求和任务的错误答案
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 函数从指针 c++ 中获取错误的值并返回错误的答案
- C ++分割错误,为什么使用"long long"我没有得到答案?
- 乘法宏给出错误的答案
- 谷歌启动B轮巴士路线问题错误的答案?
- 欧拉项目 #8 C++ 得到错误的答案
- 寻找最小楼梯成本的动态规划问题的错误答案
- 为什么可变大小的数组会导致Codechef出现错误答案
- 位集上的按位或给出错误的答案
- 在 c++ 中将模板与类一起使用时,类成员函数的答案错误?
- SPOJ PLD的答案错误
- UVA 3n+1(概率 100)答案错误,但所有测试用例均已通过
- 散列!!答案错误,想不通
- 指数的最后一位——答案错误
- 在计算cpp中数字的n次方根时,答案错误
- Euler项目27的答案错误