Home > database >  Please help me find the error! I'm new to functions and I can't figure out how to correct
Please help me find the error! I'm new to functions and I can't figure out how to correct

Time:10-21

This is the code I currently have:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

int random (int sum);
bool isValid(int userNum, int sum);

int main() {
    int whoTurn, playAgain = 1, userNum, sum;
    cout << "\nLet's play a game of 100!\n"
            "In a game of 100 we start with a total of 0\n"
            "and we take turns adding numbers from 1-10\n"
            "to that total. Whoever is able to add a number\n"
            "to the total to get 100 is the winner.\t"
            "Ready?\n";
    while ((playAgain = 1)) {
        bool win = false;
        cout << "Do you want to go first or should I? [1 = you, 2 = me(computer)]: ";
        cin >> whoTurn;
        while (not win) {
            if (whoTurn == 1) {
                sum = userNum   sum;
                cout << "Number you want to add (1-10): ";
                cin >> userNum;
                if (isValid(userNum, sum)) {
                    cout << "You add " << userNum << ", total is now " << sum << "\n";
                    whoTurn = 2;
                }
                else {
                 continue;
                }
            }
            if (whoTurn == 2) {
                sum = userNum   sum;
                    cout << "I add " << int (random (sum))  << ", total is now " << sum << "\n";
                    whoTurn = 1;
                    if (sum == 100.0) {
                        cout << "Computer wins!";
                        win = true;
                    }
                }
            }
            cout << "Do you want to play again (1 = yes, 2 = no): ";
            cin >> playAgain;
        }
        return 0;
}


//validating userNum function
bool isValid(int userNum, int sum) {

    if (1.0 <= userNum && userNum <= 10.0 && userNum   sum <= 100.0) {
        return true;
    } else {
        return false;
    }
}

//computer random/win turn function
    int random(int sum) {
        int randTurn = rand() % 10   1;
        srand((unsigned) time(NULL));
        if (sum >= 90) {
            return 100-sum;
        }
        else {
            return randTurn;
        }
    }

This is the output that I'm getting, notice the error in sum calculation:

Let's play a game of 100!
In a game of 100 we start with a total of 0
and we take turns adding numbers from 1-10
to that total. Whoever is able to add a number
to the total to get 100 is the winner.  Ready?
Do you want to go first or should I? [1 = you, 2 = me(computer)]: 1
Number you want to add (1-10): 59
Number you want to add (1-10): 3
You add 3, total is now 62
I add 8, total is now 65
Number you want to add (1-10): 

I know I've got the calculation for the sum wrong somewhere in the code, but I can't figure out where I need to fix it. Sorry if this is a simple fix or just product of bad code, I'm still pretty new to all of this!

CodePudding user response:

I think you are just printing the sum before you add the userNum to the sum in the next loop. You are just one loop behind with your output :)

Can I also direct your attention to the game state of your main loop when the computer plays, whoTurn == 2 I think you are not adding the random value you generate to your sum.

  • Related