Counting Points

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tr1n1x

    #1

    Counting Points

    I'm trying to finish a program that gives a test for the
    user. The test has four options to choose from,
    1. addition
    2. subtraction
    3. multiplication
    4. division.

    Once the user picks one of the choices above the
    program will give a specific amount of mathematical
    problems using one of the choices above, for example a
    user picks addition. 1 + 9 =.
    When the user gets an answer right the computer saves
    it as a 'point.' After the test is done his/her score is
    divided by the total amount of questions asked. If the
    user get's a .75 or below the computer will print " You're
    score is too low, try again."
    What my problem is - is that the computer only counts
    one correct answer, even if there is more than one. Here
    is my code with comments, if you would like a clearer
    look, email me and I will send you an attachment as a text
    file. Thank you for your help. And, Ofcourse, if you have
    any suggestions for me go right ahead.


    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    int Random_num ( void );
    int Calculation ( int );
    int main()
    {

    unsigned int counter, // This counts how many questions
    asked.
    userchoice, // This is the choice the user uses to
    determine the kind of mathematical sign to use.
    point; // This is what counts the answers the
    user gets correct.

    double final_score; // This is the Score the user gets
    after finishing Test.





    cout << " Welcome to the Elementary Math Help. This
    program will test your n/"
    << " skills in Addition, Subtraction, Multiplication, and
    Division."<< endl;
    cout << " Choose 1 for addition \n"
    << " Choose 2 for subtraction \n"
    << " Choose 3 for multiplication \n"
    << " Choose 4 for Division \n "
    << " or press 0 for the computer to choose \n";
    cin >> userchoice;

    counter = 0;

    while ( counter != 5 ){
    point = Calculation ( userchoice );

    counter++;



    }

    final_score = static_cast <double> ( point ) / 5;

    if ( final_score <= .75 )
    cout << " You scored low, please try again. " << endl;

    cout << "Your score is " << setprecision (2)
    << setiosflags ( ios :: fixed | ios :: showpoint )
    << final_score << endl;
    return 0;
    }
    int Random_num ( void )
    {
    int num;
    num = 1 + rand() % 9;

    return num;
    }
    int Calculation ( int x )
    {
    int Choice = x,
    number1,
    number2,
    User_answer, // This is the answer the user inputs.
    point;
    double R_answer; // This is the correct answer to the
    question.

    number1 = Random_num();
    number2 = Random_num();
    point = 0;
    enum { add = 1, subtract, multiply, division };
    switch ( Choice ){
    case add:
    R_answer = number1 + number2;
    cout << number1 << " + " << number2 << " = ";
    cin >> User_answer;
    break;
    case subtract:
    R_answer = number1 - number2;
    cout << number1 << " - " << number2 << " = ";
    cin >> User_answer;
    break;
    case multiply:
    R_answer = number1 * number2;
    cout << number1 << " * " << number2 << " = ";
    cin >> User_answer;
    break;
    case division:
    R_answer = number1 / number2;
    cout << number1 << " / " << number2 << " = ";
    cin >> User_answer;
    break;
    default: cout << " Program Bug. ";
    break;
    }
    if ( User_answer == R_answer )
    point++;
    return point;
    }


  • Mihajlo Cvetanovic

    #2
    Re: Counting Points

    Tr1n1x wrote:[color=blue]
    > unsigned int counter, userchoice, point;[/color]

    You need to declare at least one of "counter" and "point" to double.
    Otherwise when dividing the app will perform integer division.
    [color=blue]
    > while ( counter != 5 ){
    > point = Calculation ( userchoice );[/color]

    Put "+=" instead of "=". Note that all local variables in the function
    are precicley that: local. When the function gets out of scope so does
    all local variables, such is "point", which is declared in two places.
    The "point" in the function will be set to 0 every time, so increasing
    it at the end of the function is pointless.
    [color=blue]
    > if ( User_answer == R_answer ) point++;
    > return point;[/color]

    return ( User_answer == R_answer ) ? 1 : 0;

    Comment

    Working...