### Math Game c++ , division issue. Hello

Got a small problem here , I spent 2 days writing this with debugging sessions , alone, here is the thing its working and all but sometimes it just divide by 0 , I tried adding a
while(val2=0)
val2=//my random number generator.
but no luck .
and another thing is that what ever I do I cant restart the score after each stage completed.
for example ,
//operation enetered.
//diificulty stage chosen.
// first stage have score of more than 60 , pass to the next stage.
//next stage score should be 100 if u answer all questions , instead its 100 + prev score .

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119`` `````` #include  #include  #include #include using namespace std; // I will put diffrent functions _one for each operation_ here. int main() { int dif; cout << "Please choose the operation you want to practice today (+,-), or(/,*): "; char op; cin >> op; if (op != ('-') && op != ('+') && op != ('*') && op != ('/'))//if not a valid operator { cout << "Invalid operator" << endl; system("pasue"); exit(0); } cout << "Please enter the level of difficult (1 and up): "; cin >> dif; srand(time(0)); int min, max;//generate two random numbers in the range min and max of digit size as dif int numr = 0; int numw = 0; int i; int val1, val2; int res, ans; double score; bool correct; char con; int chances; do { for (i = 0; i < 5; i++)//5 operations { chances = 3; min = int(pow(10, dif -1)); max = int(pow(10, dif) - 1); val1 = min + rand() % ((max + 1) - min); val2 = min + rand() % ((max + 1) - min); if (op == '/')//for division operator if second value is zero then repeatedly generate second number until it is not a zero { while (val2 != 0) val2 = min + rand() % ((max + 1) - min)+1; } do { //res stores user's answer, ans has correct answer for(j=0:j<3.j++) cout << "what is " << val1 << " " << op << " " << val2 << ": "; if (op == '*') ans = val1 * val2; else if (op == '/') ans = val1 / val2; else if (op == '+') ans = val1 + val2; else if (op == '-') ans = val1 - val2; cin >> res; if (res == ans) { cout << "correct answer" << endl; numr++; correct = true; } else { cout << "Incorrect!"; numw++; correct = false; chances--; } } while (!correct && chances != 0); } score = ((double)numr / (numr + numw)) * 100; cout << "score: " << score << "%" << endl;//no of correct/total cout << "Continue? [Y/N]" << endl; cin >> con; if (con == 'N'||con=='n') { system("pause");// considering break; return 0; } if (score >= 80) { dif++; score = 0; } else if (score >= 60) { dif++; score = 0; } else { cout << "try again" << endl; } score = 0; } while (con == 'Y'||con=='y'); system("pause"); return 0; } `````` ``12`` `````` while (val2 != 0) val2 = min + rand() % ((max + 1) - min)+1;``````

loops endles. How about while (val2 == 0)? remove the while. min is based off difficulty which appears to be positive (not enforced but requested in the text).

as written, the unchanged logic (which is probably wrong) would be
val2 = 0;
as probably intended, the code would be
if(!val2) val2 = min + rand() % ((max + 1) - min)+1;

the while does the same as the if, but intent is muddled reading it.
Last edited on ``12`` ``````if(!val2) val2 = min + rand() % ((max + 1) - min) + 1;``````

I did this and it works , tried it for 20 times and it never divides by 0.
is there a way I can shorten my double variable ?
because when it gives me something like 1/9 and I give it 0.11 it never gets it even if I put the full number which is 0.1111111111 .
here is the output .

Please choose the operation you want to practice today (+,-), or(/,*): /
Please enter the level of difficult (1 and up): 1
what is 6 / 8: 0.75
Incorrect!what is 6 / 8:

same thing happened with me with ( 1/9 ).
I completed all the other operations and they work perfectly , the stages too. what is 6 / 8:

It's 0. Either (i) learn about integer division; or (ii) use doubles. it still won't work.

minor: its <ctime> and <cmath> you are using C language header files. They work, until they don't, it can cause problems.

major:
as lastchance said you need to understand how the computer does integeer math.
first, 6/8 is smaller than 1, so it is truncated down to zero. 100/11 is 9, for the same reasons.

second, even if you change to doubles, your == is going to give trouble. some numbers cannot be represented exactly as doubles, so even if the human types in the correct answer, it will not match. I haven't memorized specific examples, you can find plenty on google, but it "like" if you computed 6/8 and the actual answer ways 0.7499999999999999998 but you typed 0.75 and it changed that to 0.7500000000000000000001 instead. Again, made up the numbers, but they are not equal, just very close ...

so you need to switch to doubles and then handle matching that with the user's input. a simple way to do that is simply to see if abs(userinput - calculated result) <= 0.001 (or whatever precision you expect).

Last edited on
Registered users can post here. Sign in or register to post.