chibicitiberiu
New Member
Hello everyone.
I'm new to these forums.
Now about my program, I compiled it with MS Visual Studio 6.0 on an older computer. It's a console application. This is the source code:
This is what it should do:
But, as always, problems occur. In my case, the problem is in this area of the program:
The problem is that somehow the "b" variable is lost and the program returns an error that "Integer value can't be divided by zero". What should I do to fix this error? I tried replacing the first while with a goto and some "if"s, but no result:
As i said, this modification doesn't do anything.
Can anyone help me finding the error please? Any help is usefully.
I'm new to these forums.
Now about my program, I compiled it with MS Visual Studio 6.0 on an older computer. It's a console application. This is the source code:
Code:
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<conio.h>
using namespace std;
int rand_0toN1(int n);
int main()
{
// declaration of the variables
int questions, limit, a, b, op, result, final=0;
// set the random seed for the random function
srand(time(NULL));
// get number of questinons and limit
cout<<endl<<"How many questions should be asked: ";cin>>questions;
cout<<"What should be the maximum number allowed: ";cin>>limit;
for(int i=1;i<=questions;i++)
{
op=rand_0toN1(4);
if(op==0)
{
a=b=limit;
while(a+b>limit)
{
a=rand_0toN1(limit+1);
b=rand_0toN1(limit+1);
}
cout<<i<<") "<<a<<" + "<<b<<" = ";cin>>result;
if(result == a+b)
final++;
}
else if(op==1)
{
a=limit-1;
b=limit;
while(a<=b)
{
a=rand_0toN1(limit+1);
b=rand_0toN1(limit+1);
}
cout<<i<<") "<<a<<" - "<<b<<" = ";cin>>result;
if(result == a-b)
final++;
}
else if(op==2)
{
a=b=limit;
while(a*b>limit || a==0 || b==0 || a==1 || b==1)
{
a=rand_0toN1(limit+1);
b=rand_0toN1(limit+1);
}
cout<<i<<") "<<a<<" x "<<b<<" = ";cin>>result;
if(result == a*b)
final++;
}
else if(op==3)
{
a=0; b=0;
while(a==0 || b==0)
{
a=limit+1;
b=limit+2;
while(b>=a || a>limit || a%b!=0 || b==1)
{
a=rand_0toN1(limit+1);
b=rand_0toN1(limit+1);
}
}
cout<<i<<") "<<a<<" : "<<b<<" = ";cin>>result;
if(result == a/b)
final++;
}
}
// displaying the final result
cout<<"You answered correctly "<<final<<" questions out of "<<questions<<".";
double percentage;
percentage=(static_cast<double>(final)/static_cast<double>(questions))*100;
cout<<"You answered correctly "<<int(percentage)<<"% .";
// exiting
cout<<endl<<endl<<"Press ENTER to exit.";
getch();
return 0;
}
int rand_0toN1(int n)
{
return rand() % n;
}
This is what it should do:
Code:
How many questions should be asked: 5
What should be the maximum number allowed: 10
1) 6 + 3 = 9
2) 5 * 2 = 10
3) 5 - 3 = 3
4) 2 + 5 = 7
5) 8 / 4 = 2
You answered correctly 4 questions out of 5. You answered correctly 80% .
But, as always, problems occur. In my case, the problem is in this area of the program:
Code:
else if(op==3)
{
a=0; b=0;
while(a==0 || b==0)
{
a=limit+1;
b=limit+2;
while(b>=a || a>limit || a%b!=0 || b==1)
{
a=rand_0toN1(limit+1);
b=rand_0toN1(limit+1);
}
}
cout<<i<<") "<<a<<" : "<<b<<" = ";cin>>result;
if(result == a/b)
final++;
}
The problem is that somehow the "b" variable is lost and the program returns an error that "Integer value can't be divided by zero". What should I do to fix this error? I tried replacing the first while with a goto and some "if"s, but no result:
Code:
else if(op==3)
{
again:
a=limit+1;
b=limit+2;
while(b>=a || a>limit || a%b!=0 || b==1)
{
a=rand_0toN1(limit+1);
b=rand_0toN1(limit+1);
}
if(a==0) goto again;
if(b==0) goto again;
cout<<i<<") "<<a<<" : "<<b<<" = ";cin>>result;
if(result == a/b)
final++;
}
Can anyone help me finding the error please? Any help is usefully.
Last edited: