所以我正在尝试制作一个程序,它将生成随机的基本数学问题,如加法、减法等。
当我试图生成除法问题时,我做了一个 do-while 循环,该循环一直运行到满足除法问题的先决条件为止。
但是由于某种原因,当我要求它生成 20 个除法问题而没有余数时,它总是崩溃。
请帮忙
这是代码
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int correct,total,MAXVAL;
const char oper[4]={'/','/','/','/'};
typedef struct data{
int first,second;
char operation;
}data;
data datagenerator(){
data newData;
newData.first= (rand()%MAXVAL);
newData.operation=oper[rand()%4];
newData.second=(rand()%MAXVAL);
return newData;
}
bool corrector(data newData){
bool isture=false;
switch(newData.operation){
case '/':
if((newData.first%newData.second)==0){
isture=true;
}
break;
case '*':
isture=true;
break;
case '+':
isture=true;
break;
case '-':
isture=true;
break;
}
return isture;
}
void quizer(){
system("CLS");
for(int counter=1;counter<=total;counter++){
cout<<"Q"<<counter<<": ";
data newData;
do{
newData=datagenerator();
}while(!corrector(newData));
cout<<newData.first<<newData.operation<<newData.second<<"\n";
}
cout<<"\nYou got "<<correct<<"/"<<total<<"correct\n";
}
int main(){
//srand((unsigned)time(0));
char x;
cout<<"How many questions do you want?"<<endl;
cin>>total;
cout<<"Enter the maximum value\n";
cin>>MAXVAL;
cout<<"Are you ready?\n";
cin>>x;
quizer();
system("PAUSE");
return 0;
}