please help...when case 2, 3, or 4 are chosen the program goes into an infinite loop except for case 1 which works fine....
//this program performs addition (grades 1,2,3,4), subtraction (grades 2,3,4), multiplication (grades 3,4), and division(grade 4)
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned)time(0));
int grade, operation2, operation3, operation4;
int random_integer1, random_integer2;
int userans, compans;
char ans;
do
{
cout << "If you are in 1st Grade, type '1'" << endl;
cout << "If you are in 2nd Grade, type '2'" << endl;
cout << "If you are in 3rd Grade, type '3'" << endl;
cout << "If you are in 4th Grade, type '4'" << endl;
cout << "Type '5' if you don't feel like having fun with ";
cout << "numbers right now :-D" << endl;
cout << "Type in your choice here--> ";
cin >> grade;
cout << endl;
switch (grade)
{
case 1:
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%10)+1;
random_integer2 = (rand()%10)+1;
compans = random_integer1 + random_integer2;
cout << random_integer1 << " + " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
case 2:
cout << "Type A for addition or type S for subtraction: ";
cin >> operation2;
switch (operation2)
{
case 'A':
case 'a':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%100)+1;
random_integer2 = (rand()%100)+1;
compans = random_integer1 + random_integer2;
cout << random_integer1 << " + " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
case 'S':
case 's':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%100)+1;
random_integer2 = (rand()%100)+1;
compans = random_integer1 - random_integer2;
cout << random_integer1 << " - " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
default:
cout << "That choice is not acceptable.";
}
case 3:
cout << "Type A for addition, S for subtraction, M for multiplication: ";
cin >> operation3;
switch (operation3)
{
case 'A':
case 'a':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%1000)+1;
random_integer2 = (rand()%1000)+1;
compans = random_integer1 + random_integer2;
cout << random_integer1 << " + " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
case 'S':
case 's':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%1000)+1;
random_integer2 = (rand()%1000)+1;
compans = random_integer1 - random_integer2;
cout << random_integer1 << " - " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
case 'M':
case 'm':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%100)+1;
random_integer2 = (rand()%10)+1;
compans = random_integer1 * random_integer2;
cout << random_integer1 << " * " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
default:
cout << "That choice is not acceptable.";
}
case 4:
cout << "Type A for addition, S for subtraction, \n";
cout << "M for multiplication, or D for division: ";
cin >> operation4;
switch (operation4)
{
case 'A':
case 'a':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%10000)+1;
random_integer2 = (rand()%10000)+1;
compans = random_integer1 + random_integer2;
cout << random_integer1 << " + " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
case 'S':
case 's':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%10000)+1;
random_integer2 = (rand()%10000)+1;
compans = random_integer1 - random_integer2;
cout << random_integer1 << " - " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
case 'M':
case 'm':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%100)+1;
random_integer2 = (rand()%100)+1;
compans = random_integer1 * random_integer2;
cout << random_integer1 << " * " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
case 'D':
case 'd':
do
{
for(int index=0; index<20; index++)
random_integer1 = (rand()%20)+1;
random_integer2 = (rand()%10)+1;
compans = random_integer1 / random_integer2;
cout << random_integer1 << " / " << random_integer2 << " = ";
cin >> userans;
if (userans == compans)
cout << "That's correct! Good job!\n";
else
cout << "Good try, but the correct answer is " << compans << endl;
cout << "Do you want to try another one?\n";
cout << "Type y for yes, and n for no: ";
cin >> ans;
cout << endl;
}
while (ans =='y' || ans == 'Y');
cout << "That was awesome! :-D\n";
break;
default:
cout << "That choice is not acceptable.";
}
case 5:
cout << "Ok, we'll have fun with numbers some other time.\n";
cout << "Good-bye for now! :-D\n";
exit(1);
default:
cout<<"That value does not exist";
}
}while (grade != 5);
return 0;
}
Code tags added. -Narue