lundi 10 avril 2017

C++ 2D array comparison not working

I'm new at this so please excuse my lack of experience. My code seems to work fine except the (gradeMe) function doesn't score the user correctly when taking the test. After the user chooses Switch/Case statement #2, it asks the user random times table questions base on user's choice, then in the (gradeMe) function it evaluates the 2D array and scores the user based on the correct answers. However, the scores are not working correctly. Does anyone have any advice for me?

#include<iostream>
#include<cstdlib>
#include<iomanip>
#include <ctime>
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
    srand(time(NULL));
    int myTable[10][3] = {0};
    int testTable[10][3] = {0};
    int ansTable[10][3] = {0};
    int menuItem = 0;
    int 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);
        gradeMe (testTable, ansTable);
        break;
    case 3: //Enter a new multiplication table option
        displayTable(myTable);
        break;
    case 4: //Quit program menu option
        cout << "Program ending.\n";
        return 0;
    default:
        cout << "You entered an invalid item number. Please enter a number 
    from 1 to 4.\n";
        cout << endl;
    }   
} while (menuItem != 4);

    return 0;
}
//Function definitions:
//Display table function
void displayTable (int thisTable[10][3])
{
    int num, i = 0;     //initialize local variables    
//Ask the user what times table they would like to review
cout << "What times table would you like to review?" << endl;;
cout << "Please enter a value from 1 to 12 > \n";
cout << "\n";
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 
)
{
 //initialize local variables
   int num = 0;
   int i = 0;   
   int answer = 0;
   srand(time(NULL));
cout << "What times table test would you like to take? > ";
cin >> num; 
//Create test table
for (int i = 0; i < 10; i++)
{
    testTable[i][0] = i + 1;
    testTable[i][1] = num;
    testTable[i][2] = testTable[i][0] * testTable[i][1];
}
    for (int i = 0; i < 10; i++)
    {
        testTable[i][0]=rand()%10;
        cout << testTable[i][0] << " * " << testTable[i][1] << " = ";
        cin >> answer;
    }
//Create answer table   
for (int i = 0; i < 10; i++)
{
ansTable[i][0] = i + 1;
ansTable[i][1] = num;
ansTable[i][2] = ansTable[i][0] * ansTable[i][1];
}
    for (int i = 0; i < 10; i++)
    {
    ansTable[i][0]=rand()%10;
    }
cout << endl;
}
//Test me function
bool testMe (int testTable[10][3])
{   
int i;
testTable[i][2] = testTable[i][0] * testTable[i][1];
return true;
}
//Grade me function
void gradeMe (int testTable[10][3], int ansTable[10][3])
{
int i;
if (ansTable[i][2] == testTable[i][2])
{
    cout << "Congradulations! You got a perfect score!" << endl;
}
if (ansTable[i][2] < testTable[i][2] -3)
{
    cout << "You passed." << endl;
}
else
{
    cout << "You failed. Try again." << endl;
}
cout << 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 > "; 
}




Aucun commentaire:

Enregistrer un commentaire