vendredi 20 décembre 2019

Unexpected loop behaviour in c++

// 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 " ";
    }
}  

When I compile and run this program, I expect it to loop only 10 times but it loops more than 10 times, I'm thinking it has to do with the switch statement in the responses function but I do not understand why it should be causing a problem. Any explanation would be greatly appreciated. I have modified the while loop condition in the main function to loop different times but it always loops to display all the possible responses in the switch statement. Screen shot of results attached, I modified the while statement to loop only twice but I still had all my responses showing so it ended up looping 4 times. program execution while set to loop twice

Aucun commentaire:

Enregistrer un commentaire