Trying to fix my code C++ 2D array random multiplication quiz 1 through 10. User
ID: 3813704 • Letter: T
Question
Trying to fix my code C++ 2D array random multiplication quiz 1 through 10. User picks a number from 1 - 10 and a random quiz is given. At the end the user is scored based on how many they got wrong. Here is my code so far. Switch Case statements 1, 3, and 4 are working fine, but Case Statement 2 is not.
#include
#include
#include
#include
using namespace std;
//prototyping
void displayTable (int myTable[10][3]);
void createTables(int testTable[10][3], int ansTable[10][3], int usersChoice);
bool testMe (int testTable[10][3]);
void gradeMe (int testTable[10][3], int ansTable[10][3]);
void displayMenu();
int main()
{
//define variables and arrays
int myTable[10][3] = {0};
int testTable[10][3] = {0};
int ansTable[10][3] = {0};
int menuItem = 0;
int num, answer, usersChoice = 0;
do
{
displayMenu(); //call displayMenu function
cin >> menuItem; //read users input
cout << endl;
//validate menu selection with switch/case statements
switch(menuItem)
{
case 1: //Review Times Table option
displayTable(myTable);
break;
case 2: //Test user and capture return value from functions
createTables(testTable, ansTable, usersChoice);
testMe(testTable);
break;
case 3: //Enter a new multiplication table option
displayTable(myTable);
break;
case 4: //Quit program menu option
cout << "Program ending. ";
return 0;
default:
cout << "You entered an invalid item number. Please enter a number from 1 to 4. ";
cout << endl;
}
} while (menuItem != 4);
return 0;
}
//Function definitions:
//Display table function
void displayTable (int thisTable[10][3])
{
int num, i = 0, j = 0; //initialize local variables
cout << "What times table would you like to review?" << endl;;
cout << "Please enter a value from 1 to 12 > ";
cout << " ";
cin >> num;
cout << endl;
for (int i = 0; i < 10; i++)
{
thisTable[i][0] = i + 1;
thisTable[i][1] = num;
thisTable[i][2] = thisTable[i][0] * thisTable[i][1];
}
for (int i = 0; i < 10; i++)
{
cout << thisTable[i][0] << " * " << thisTable[i][1] << " = " << thisTable[i][2]< }
cout << endl;
}
//Create test and answer table functions
void createTables(int testTable[10][3], int ansTable[10][3], int usersChoice )
{
int i;
int choice = 0;
int answer = 0;
cout << "What times table test would you like to take? > ";
cin >> choice;
for (int i = 0; i < 10; i++)
{
cout << setw(3) << i + 1 << " X " << choice << " = " << answer * (i + 1)<< " " << endl;
}
}
//Test me function
bool testMe (int testTable[10][3])
{
int randomNum, maxVal = 10;
//create loop to continue until the test is completed...user answers all questions
srand ( time(NULL)); //generate random number
randomNum = rand() % maxVal;
testTable [randomNum] [0];
testTable [randomNum] [1];
testTable [randomNum] [2];
testTable [randomNum] [3];
testTable [randomNum] [4];
testTable [randomNum] [5];
testTable [randomNum] [6];
testTable [randomNum] [7];
testTable [randomNum] [8];
testTable [randomNum] [9];
return true;
}
//Grade me function
void gradeMe (int testTable[10][3], int ansTable[10][3])
{
// if (randomNum >= 7)
// cout << "Congradulations! You passed the test." << endl;
// if (randomNum == 10)
// cout << "You did an excellent job!! PERFECT SCORE!!" << endl;
// if (randomNum < 7)
// cout << "Sorry. You did not pass the exam. Try again." << endl;
}
//Display the menu function
void displayMenu()
{
cout << "******************************************************************************" << endl;
cout << " Multiplication Tables" << endl;
cout << endl;
cout << " 1. Review MyTable" << endl;
cout << " 2. Test Me" << endl;
cout << " 3. Enter a New Multiplication Table (1-12)" << endl;
cout << " 4. Quit" << endl;
cout << " Enter a Menu Item > " <<
endl;
}
Explanation / Answer
Highlighted the place where the change was done. Please check the output down below.
PROGRAM CODE:
#include
#include
#include
#include
using namespace std;
//prototyping
void displayTable (int myTable[10][3]);
void createTables(int testTable[10][3], int ansTable[10][3], int usersChoice);
bool testMe (int testTable[10][3]);
void gradeMe (int testTable[10][3], int ansTable[10][3]);
void displayMenu();
int main()
{
//define variables and arrays
int myTable[10][3] = {{0}};
int testTable[10][3] = {{0}};
int ansTable[10][3] = {{0}};
int menuItem = 0;
int num, answer, usersChoice = 0;
do
{
displayMenu(); //call displayMenu function
cin >> menuItem; //read users input
cout << endl;
//validate menu selection with switch/case statements
switch(menuItem)
{
case 1: //Review Times Table option
displayTable(myTable);
break;
case 2: //Test user and capture return value from functions
createTables(testTable, ansTable, usersChoice);
testMe(testTable);
break;
case 3: //Enter a new multiplication table option
displayTable(myTable);
break;
case 4: //Quit program menu option
cout << "Program ending. ";
return 0;
default:
cout << "You entered an invalid item number. Please enter a number from 1 to 4. ";
cout << endl;
}
} while (menuItem != 4);
return 0;
}
//Function definitions:
//Display table function
void displayTable (int thisTable[10][3])
{
int num, i = 0, j = 0; //initialize local variables
cout << "What times table would you like to review?" << endl;;
cout << "Please enter a value from 1 to 12 > ";
cout << " ";
cin >> num;
cout << endl;
for (int i = 0; i < 10; i++)
{
thisTable[i][0] = i + 1;
thisTable[i][1] = num;
thisTable[i][2] = thisTable[i][0] * thisTable[i][1];
}
for (int i = 0; i < 10; i++)
{
cout << thisTable[i][0] << " * " << thisTable[i][1] << " = " << thisTable[i][2]<<endl;
}
cout << endl;
}
//Create test and answer table functions
void createTables(int testTable[10][3], int ansTable[10][3], int usersChoice )
{
int choice = 0;
cout << "What times table test would you like to take? > ";
cin >> choice;
for (int i = 0; i < 10; i++)
{
cout << setw(3) << i + 1 << " X " << choice << " = " << choice * (i + 1)<< " " << endl; //made a change here to multiply with choice
}
}
//Test me function
bool testMe (int testTable[10][3])
{
int randomNum, maxVal = 10;
//create loop to continue until the test is completed...user answers all questions
srand ( time(NULL)); //generate random number
randomNum = rand() % maxVal;
testTable [randomNum] [0];
testTable [randomNum] [1];
testTable [randomNum] [2];
testTable [randomNum] [3];
testTable [randomNum] [4];
testTable [randomNum] [5];
testTable [randomNum] [6];
testTable [randomNum] [7];
testTable [randomNum] [8];
testTable [randomNum] [9];
return true;
}
//Grade me function
void gradeMe (int testTable[10][3], int ansTable[10][3])
{
// if (randomNum >= 7)
// cout << "Congradulations! You passed the test." << endl;
// if (randomNum == 10)
// cout << "You did an excellent job!! PERFECT SCORE!!" << endl;
// if (randomNum < 7)
// cout << "Sorry. You did not pass the exam. Try again." << endl;
}
//Display the menu function
void displayMenu()
{
cout << "******************************************************************************" << endl;
cout << " Multiplication Tables" << endl;
cout << endl;
cout << " 1. Review MyTable" << endl;
cout << " 2. Test Me" << endl;
cout << " 3. Enter a New Multiplication Table (1-12)" << endl;
cout << " 4. Quit" << endl;
cout << " Enter a Menu Item > " <<
endl;
}
OUTPUT:
******************************************************************************
Multiplication Tables
1. Review MyTable
2. Test Me
3. Enter a New Multiplication Table (1-12)
4. Quit
Enter a Menu Item >
1
What times table would you like to review?
Please enter a value from 1 to 12 >
9
1 * 9 = 9
2 * 9 = 18
3 * 9 = 27
4 * 9 = 36
5 * 9 = 45
6 * 9 = 54
7 * 9 = 63
8 * 9 = 72
9 * 9 = 81
10 * 9 = 90
******************************************************************************
Multiplication Tables
1. Review MyTable
2. Test Me
3. Enter a New Multiplication Table (1-12)
4. Quit
Enter a Menu Item >
2
What times table test would you like to take? > 3
1 X 3 = 3
2 X 3 = 6
3 X 3 = 9
4 X 3 = 12
5 X 3 = 15
6 X 3 = 18
7 X 3 = 21
8 X 3 = 24
9 X 3 = 27
10 X 3 = 30
******************************************************************************
Multiplication Tables
1. Review MyTable
2. Test Me
3. Enter a New Multiplication Table (1-12)
4. Quit
Enter a Menu Item >
3
What times table would you like to review?
Please enter a value from 1 to 12 >
8
1 * 8 = 8
2 * 8 = 16
3 * 8 = 24
4 * 8 = 32
5 * 8 = 40
6 * 8 = 48
7 * 8 = 56
8 * 8 = 64
9 * 8 = 72
10 * 8 = 80
******************************************************************************
Multiplication Tables
1. Review MyTable
2. Test Me
3. Enter a New Multiplication Table (1-12)
4. Quit
Enter a Menu Item >
4
Program ending.