c ++

时间:2019-12-20 14:58:07

标签: c++ c++11

// cai.cpp (Computer Assisted Instruction)
// This program uses random number generation to assist students learn multiplication

#include <iostream>
#include <iomanip>
#include <cstdlib> // contains prototypes for srand and rand
#include <ctime>
#include <cctype>
using namespace std;

int main() {
    int question();
    string status;
    int score{0};

    cout << "\nThis program will present you with 10 multiplication problems\n"
        << "enter the correct answer after the prompt\n"
        << "Enter Y for YES and N for NO\n"
        << "Do you want to try a game?";
    cin >> status;


    while(status == "Y" || status == "y") {
        for(int x{0}; x < 11; x++) {
            question();
            score = score + question();
        }
        // report total score
        cout << "\nTotal score is " << score << " out of 10";

        cout << "\nWould you like to play again?";
        cin >> status;
        if(status == "n" || status == "N") {
            break;
        }
    }
    cout << endl;
}

int question() {
    string responses();
    // use srand to generate the random nmber for the various problems
    srand(static_cast<unsigned int> (time(0)));
    int number1 = 1 + rand() % 12; // initialize random number
    int number2 = 1 + rand() % 12; // initialize random number
    int total = number1 * number2;
    int response;
    int score{0};

    cout << "\nWhat is " << number1 << + " times " << + number2 << + " ?";
    cin >> response;
    while (response != total) { // while answer is wrong, repeat question and wait for response
        cout << " \nThat is incorrect, try again: ";
        cin >> response;
    }
    if ( response == total) {
        cout << responses();
        score++; // increment score after each correct answer
    }

    return score;


}

string responses() {
    string res1 = "Well done, that is correct!\n";
    string res2 = "Congratulations, that is very accurate!\n";
    string res3 = "Wow!, I'm impressed\n";
    string res4 = "You're doing great! Keep up the good work.\n";
    srand(static_cast<unsigned int> (time(0)));
    int select{1 + rand() % 4};

    switch(select) {
        case 1: return res1;
        break;
        case 2: return res2;
        break;
        case 3: return res3;
        break;
        case 4: return res4;
        break;
        default: return " ";
    }
}  

当我编译并运行该程序时,我希望它只能循环执行10次,但是它可以循环执行10次以上,我认为它与responses函数中的switch语句有关,但我不明白为什么会这样应该引起问题。任何解释将不胜感激。我已经修改了main函数中的while循环条件以循环不同的时间,但是它总是循环显示switch语句中所有可能的响应。附上结果的屏幕截图,我将while语句修改为仅循环两次,但是我仍然显示所有响应,因此最终循环了4次。 program execution while set to loop twice

2 个答案:

答案 0 :(得分:3)

  

期望它仅循环10次,但循环10次以上

在您的循环中:

for(int x{0}; x < 11; x++)

x从0到10,因此它循环了11次。

答案 1 :(得分:1)

一些注意事项。您的for循环上升到< 11,表示10。因此,从x0(包括)的10实际上是11倍。您需要将条件更改为< 10,或将x更改为从1开始。

第二个问题,在for循环中,您实际上两次调用了question函数;忽略第一个通话的结果。这就是为什么您会得到4个问题,每个问题都能正确回答,但得分仅为2的原因。

第三,不是什么大问题,但是有点多余。在while循环的最后,您检查status == "n" || status == "N",但这是不必要的,因为while循环的条件已经检查了yY

我在这里清理了while循环:

    while(status == "Y" || status == "y") {
        for(int x = 0; x < 10; ++x) {
            score += question();
        }
        // report total score
        cout << "\nTotal score is " << score << " out of 10";

        cout << "\nWould you like to play again?";
        cin >> status;
    }

无关的东西也要清理。您无需在srand函数中调用question(),而只需在main中调用一次。另外,由于这是C ++,因此您可以从<random>标头中使用更新的随机数生成器。