初学者C ++程序中的分数组件

时间:2014-01-05 03:50:43

标签: c++

我正在做一项任务,我应该编写一个程序来测试用户的数学技能。这是我现在的代码:

using namespace std;
void addition()
{
    int Value, Value2, Answer;
    bool gotAnswer;

    for (int i=1; i<=10; i++)
    {
        Value = 1 + (rand()%10);
        Value2 = 1 + (rand()%10);

        gotAnswer = false;
        cout << Value << " + " << Value2 << " = ";

        for (int a=1; (a<=3 && gotAnswer==false); a++)
        {
            cin >> Answer;
            if (Answer==(Value+Value2))
            {
                cout << "CORRECT" << endl << endl;
                gotAnswer = true;
            }
            else
            {
                cout << "WRONG Try again." << endl;
                if (a==3)
                {
                    cout << "You have missed 3 times. The answer is " << Value+Value2 << "." << endl << endl;
                }
            }
        }
    }    
}

void substraction()
{
    int Value, Value2, Answer;
    bool gotAnswer;

    for (int i=1; i<=10; i++)
    {
        Value = 1 + (rand()%10);
        Value2 = 1 + (rand()%10);

        gotAnswer = false;
        cout << Value << " - " << Value2 << " = ";

        for (int a=1; (a<=3 && gotAnswer==false); a++)
        {
            cin >> Answer;
            if (Answer==(Value-Value2))
            {
                cout << "CORRECT" << endl << endl;
                gotAnswer = true;
            }
            else
            {
                cout << "WRONG Try again." << endl;
                if (a==3)
                {
                    cout << "You have missed 3 times. The answer is " << Value-Value2 << "." << endl << endl;
                }
            }
        }
    }
}

void Multiplication()
{
    int Value, Value2, Answer;
    bool gotAnswer;

    for (int i=1; i<=10; i++)
    {
        Value = 1 + (rand()%10);
        Value2 = 1 + (rand()%10);

        gotAnswer = false;
        cout << Value << " x " << Value2 << " = ";

        for (int a=1; (a<=3 && gotAnswer==false); a++)
        {
            cin >> Answer;

            if (Answer==(Value*Value2))
            {
                cout << "CORRECT" << endl << endl;
                gotAnswer = true;
            }
            else
            {
                cout << "WRONG Try again." << endl;
                if (a==3)
                {
                    cout << "You have missed 3 times. The answer is " << Value*Value2 << "." << endl << endl;
                }
            }
        }
    }
}

int main()
{
    int number;

    cout << "Enter the number for the problem type desired:"<<endl;
    cout << "  1. Addition"<<endl;
    cout << "  2. Subtraction"<<endl;
    cout << "  3. Multiplication"<<endl;

    cin >> number;

    if (number == 1)
    {
        addition();
    }
    else if(number == 2)
    {
        substraction();
    }
    else if (number ==3)
    {
        Multiplication();
    }
}

程序运行正常。但是,应该有一个得分组件,用户在第一次尝试时获得10分,第二次尝试获得5分,在第三次尝试/错误时获得0。我不知道如何在10个问题的末尾混合得分组件和显示。提示好吗?

非常感谢。

1 个答案:

答案 0 :(得分:2)

您应该在每个功能中保留一个分数变量,根据需要添加到分数变量,然后返回分数变量。

所以这些功能不再是空洞,它们将是整体的。然后,您可以在结尾处获得分数并将其打印出来。

我不会为你编写任何代码,因为它是为了一项任务:P