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

标签 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 个问题的末尾混合分数组件和显示。请提示?

提前致谢。

最佳答案

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

所以这些函数不再是 void,它们将是 int。然后你可以在最后得到分数并打印出来。

我不会为你写任何代码,因为它是一个作业:P

关于c++ - 初学者 C++ 程序中的分数组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20929835/

相关文章:

C++ - Clutter 1.0 - 从线程调用函数导致段错误

c++ - Qt中如何在循环中从单独运行的子线程中调用父线程函数

c++ - 从函数返回时写入内存

C++如何处理通用捕获处理程序中抛出的异常

c++ - std::cout 会影响编译结果吗?

c++ - 处理对象问题

c++ - 在 Windows 10 中将 GSL 库链接到 RcppGSL

c++ - 如何计算 mac M1 和 x86-64 上的前导零?

c++ - 3D 数组到 3D std::vector

c++ - 动态创建数组 - 无法推断 'T' 的模板参数 - C++