### 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
Hey guys thank you for your support , I did learn new stuff and yes I was focused on my logical prespective so much I forgot about the math one.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243`` ``````#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; void Intro() { PlaySound(TEXT("hello-4"), NULL, SND_SYNC); cout << endl; cout << "Welcome to the Mathematics Practice Software.\n" << endl; cout << endl; cout << "This program will help you practice math (+,-,*,/) by "; cout << "giving you with randomly generated questions,\n"; cout << "problems are at different level of difficulty.\nThe "; cout << "difficulty is determined by the number of digits in "; cout << "the question.\nYou can specify the initial "; cout << "number of digits. The program will also keep track "; cout << "of your progress.\nAt the end of each session, you "; cout << "may choose to continue or quit." << endl; cout << endl; cout << "Please choose the operation you want to practice today (+,-, /,or *): "; } void gen_numbers(int &min, int &max, int &val1, int &val2, int &dif) { min = int(pow(10, dif - 1)); max = int(pow(10, dif) - 1); val1 = min + rand() % ((max + 1) - min); val2 = min + rand() % ((max + 1) - min); } void ask_questions(bool &correct, int &chances, int &numr, int &numw, long double res, long double ans, char op) { if (cin.fail()) { cout << "Error\n"; system("pause"); exit(0); } else { if (op == '/') { ans = ans * 100; res = res * 100; ans = ceil(ans); if (res >= ans - 1 || res <= ans + 1) { cout << "correct answer" << endl; numr++; correct = true; PlaySound(TEXT("app.wav"), NULL, SND_SYNC); } else { cout << "Incorrect!"; numw++; correct = false; chances--; PlaySound(TEXT("sad.wav"), NULL, SND_SYNC); } } else if (op != '/') if (res == ans) { cout << "correct answer" << endl; numr++; correct = true; PlaySound(TEXT("app.wav"), NULL, SND_SYNC); } else { cout << "Incorrect!"; numw++; correct = false; chances--; PlaySound(TEXT("sad.wav"), NULL, SND_SYNC); } } } int main() { srand(time(0)); SYSTEMTIME time; while (true) { GetLocalTime(&time); wprintf(L"The local Time: %02d:%02d:%02d\n", time.wHour, time.wMinute, time.wSecond); int dif; Intro(); char op; cin >> op; if (op != ('-') && op != ('+') && op != ('*') && op != ('/'))//if not a valid operator { cout << "Invalid operator" << endl; system("pause"); exit(0); } cout << "each increment in difficulty increments the number of digits inside each part of the question.\n"; cout << "Please enter the level of difficult (1 and up): "; cin >> dif; int min, max;//generate two random numbers in the range min and max of digit size as dif int i; int val1, val2; double res; double ans; double score; bool correct; char con; int chances; do { int numr = 0; int numw = 0; for (i = 0; i < 5; i++)//5 operations { chances = 3; gen_numbers(min, max, val1, val2, dif); if (op == '/')//for division operator if second value is zero then repeatedly generate //second number until it is not a zero { if (!val2) val2 = min + rand() % ((max + 1) - min) + 1; } if (val1 == 0 || val2 == 0) { cout << "error\n"; system("pause"); exit(0); } if (dif > 10) { cout << "our data types ant take more than 10 digits per value.\n"; system("pause"); exit(0); } do { ans = 0; //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 == '/') { //divisionproblem(ans, val1, val2, dif); ans = val1 / double(val2); ans = ceil(ans * 100) / 100; } else if (op == '+') ans = val1 + val2; else if (op == '-') ans = val1 - val2; cin >> res; ask_questions(correct, chances, numr, numw, res, ans, op); } while (!correct && chances != 0); } score = ((double)numr / (numr + numw)) * 100; cout << "score: " << score << "%" << endl;//no of correct/total //callme(score); if (score >= 80) { cout << "Exelent\n"; } else if (score >= 60) { cout << "Work Harder!\n"; } else { cout << "try again" << endl; cout << "you Didnot Pass\n"; } cout << "Continue? [Y/N]" << endl; cin >> con; if (con == 'N' || con == 'n') { GetLocalTime(&time); wprintf(L"The local Time: %02d:%02d:%02d\n", time.wHour, time.wMinute, time.wSecond); PlaySound(TEXT("song.wav"), NULL, SND_SYNC); system("pause");// considering break; break; } if (score >= 80) { dif++; } else if (score >= 60) { dif++; } else cout << endl; cout << "next stage\n "; } while (con == 'Y' || con == 'y'); system("pause"); return 0; } }``````

here is the final code , its working for a maximum of 10 digits per value in each question , and I used a function to play sounds with .wav format .
this program got me to understand c++ more .
@jonnin I did a different thing you can see it in the functions above I multiplied by 100 and ceiled them then compared and if they are different with a certan range then its true else its wrong.
 ``12345678910`` ``````if (op == '/') { ans = ans * 100; res = res * 100; ans = ceil(ans); if (res >= ans - 1 || res <= ans + 1) { cout << "correct answer" << endl; numr++; correct = true;``````
Topic archived. No new replies allowed.