#Error while Choosing 3 as UserChoice

34 messages · Page 1 of 1 (latest)

crimson terraceBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cold adder
#
string WhoWonTheRound(stRoundInfo RoundInfo)
{
    if (RoundWinner(RoundInfo) == enWinner::Player)
    {
        system("color 2F");
        return "[Player]";
    }    
    else if (RoundWinner(RoundInfo) == enWinner::Computer)
    {
        system("color 4F");
        return "[Computer]";
    }
    else if (RoundWinner(RoundInfo) == enWinner::Draw)
    { 
        system("color 6F");
        return "[No Winner]"; 
    }
        
}

stRoundInfo FillRoundInfo(short UserChoice)
{
    stRoundInfo RoundInfo;

    RoundInfo.PlayerChoice = (enChoice)UserChoice;
    RoundInfo.ComputerChoice = GetComputerChoice();
    RoundInfo.RoundWinner = WhoWonTheRound(RoundInfo);

    return RoundInfo;
}

void RoundInfoResults(stRoundInfo RoundInfo)
{
    cout << "Player1 Choice  : " << RoundInfo.PlayerChoice << "\n";
    cout << "Computer Choice : " << RoundInfo.ComputerChoice << "\n";
    cout << "Round Winner    : " << RoundInfo.RoundWinner;
}

void GameRound(short HowManyRounds)
{

    char PlayAgain = ',';

    do {

        for (short GameRound = 1; GameRound <= HowManyRounds; GameRound++)
        {
            cout << "\nRound [" << GameRound << "] begins: \n";
            short UserChoice = ReadPlayerChoice();

            cout << "\n\n______________________Round[" << GameRound << "]______________________\n\n";

            RoundInfoResults(FillRoundInfo(UserChoice));
            
            cout << "\n\n______________________________________________________________________\n\n";
        }

        cout << "Do you want to play again (Y/N) ?    ";
        cin >> PlayAgain;

    } while (PlayAgain == 'Y' || PlayAgain == 'y');
    
}

int main()
{
    srand((unsigned)time(NULL));

    short HowManyRounds = ReadNumberInRange(1, 10);

    GameRound(HowManyRounds);

    return 0;
}```

sorry didn't add the other part
lost obsidian
#

You’re calling grtcomputerchoice multiple times

#

Assign it to a short before you use it

cold adder
#

ah ok, should i assign it in the WhoWonTheRound() ?

lost obsidian
#

Within RoundWinner

cold adder
#

RoundWinner sory

#

thanks

lost obsidian
#

Very first thing is assign it to a short

#

Then use the local variable instead of the function call

cold adder
#

is it better to assign it whitin the main() or within RoundWinner() ?

lost obsidian
#

RoundWinner, as it changes each time you call it

#

Ideally you’d pass in both the computer and user choices in as parameters

cold adder
#

i already passed the struct, should i just change GetComputerChoice() into RoundInfo.ComputerChoice ?

lost obsidian
#

Sure yeah try that

cold adder
#

tysm

#

im being dumb actually XD

lost obsidian
#

Dw

cold adder
# lost obsidian Dw

should i keep dividing the program into functions and procedures that perform one task just like this ? or only when it's necessary? because sometimes i'm getting lost

#

i just started learning

lost obsidian
#

I think you’re subdividing a bit too much

#

It looks slightly over engineered, like that RoundInfo struct doesn’t seem wholly necessary to me

#

You have the right idea tho of subdividing

crimson terraceBOT
#

@cold adder Has your question been resolved? If so, type !solved :)

cold adder
lost obsidian
#

Less typing is generally easier to read

cold adder
#

Ok well i wll try not to overcomplicate things XD

#

thank u for the help

#

have a good day

#

!solved

crimson terraceBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

lost obsidian
#

Np gl

cold adder
#

@lost obsidian sry to bother you again, do you know where the bug is ? i can't find it