1

I am new here, I have been trying to teach myself C++, and I am having a hoot. I have been actively learning for 5 months now and my focus is on game development. I don't seek a career in it, it is just for fun. For the past weeks I have been working on a simple dice console game to see if my basics are good. I was doing ok until today. I have run in some problems:

#pragma once
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>


class MexGame
{

public:
    MexGame();
    void setName();
    void Roll();
    void Display();
    void Reset();
    int SumScore();
    std::string GetName();
    ~MexGame();
private:
    int Dice1;
    int Dice2;
    int TotalScore;
    std::string PlayerName;
};

MexGame::MexGame() : Dice1(0), Dice2(0), TotalScore(0), PlayerName("")
{}

void MexGame::setName()
{
    std::string PlayerName;

    std::cout << "Hi what is your name?" << std::endl;

    std::cin >> PlayerName;

    std::cout << "Welcome "<< PlayerName;

}

void MexGame::Roll()
{
    int Dice1;
    int Dice2;
    int TotalScore;
    int i;
    char ThrowAgain;


    for (i = 1; i <= 3; ++i)
    {
        std::cout << "Press enter to roll: " << std::endl;
        std::cin.get();
        Dice1 = (rand() % 6) + 1;
        Dice2 = (rand() % 6) + 1;
        TotalScore = Dice1 + Dice2;

        std::cout << "You rolled " << Dice1 << " and " << Dice2 << std::endl;
        std::cout << "Do want to throw again? \n";
        std::cin >> ThrowAgain;

        //TODO FIX extra roll returning 0
        if ((ThrowAgain == 'y') || (ThrowAgain == 'Y'))
        {
            i = +i;
        }

        else
        {
            i = 3;
        }

    }

    if (TotalScore == 3)
    {
        TotalScore = TotalScore * 1000;
        std::cout << "MEX \n\n";
    }

    else if (Dice1 == Dice2)
    {
        TotalScore = (Dice1 + Dice2) * 10;
    }

    std::cout << "Your score is " << TotalScore << std::endl;

}
int MexGame::SumScore()
{
    TotalScore = Dice1 + Dice2;

    if (TotalScore == 3)
    {
        TotalScore = TotalScore * 1000;
        std::cout << "MEX \n\n";
    }

    else if (Dice1 == Dice2)
    {
        TotalScore = (Dice1 + Dice2) * 10;
    }

    return TotalScore;
}

std::string MexGame::GetName()
{
    std::cout << PlayerName;
    return std::string(PlayerName);
}

MexGame::~MexGame()
{
}

void MexGame::Display()
{
    std::cout << "Your roll was " << Dice1 << " and " << Dice2 << " .\n";
}

void MexGame::Reset()
{
    Dice1 = 0;
    Dice2 = 0;
    TotalScore = 0;
    PlayerName = "";
}

In the code above I have my Game Class in it Set the players name and get it, in my main.cpp( I will only include a fragment of it because it is to many lines. I call GetName twice, the first time the name gets printed the second time it prints nothing

case 2:

        cout << "Two players, allright lets begin!\n\n";

        MGamep1.setName();

        cout << **MGamep1.GetName**()<<" please press enter to roll dice: " << endl;
        cin.get();

        MGamep1.Roll();
        MGamep1.Display();
        cout << "Totaling " << MGamep1.SumScore() << "\n\n";

        MGamep2.setName();

        cout << MGamep2.GetName()<<" please press enter to roll dice: " << endl;
        cin.get();

        MGamep2.Roll();
        MGamep2.Display();
        cout << "Totaling " << MGamep2.SumScore() << "\n\n";

        //TODO fix getname returning empty
        if ((MGamep1.SumScore()) < (MGamep2.SumScore()))
        {
            cout << MGamep1.GetName()<< " lost, that is a shame, lets drink!!" << endl;
        }
        else
        {
            cout << **MGamep2.GetName**()<< " lost, come on drink up!!" << endl;
        }
        break;

I have bolded the two examples.I hope this is enough information, I look forward hearing from you

arbtrader
  • 11
  • 2
  • 4
    In the `setName` function you have *two* variable with the same name `PlayerName`. One local in the function, and one a member variable. It seems you could use [a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read about *scoping*. – Some programmer dude Sep 14 '17 at 16:02
  • offtopic: not sure how to point you in the right direction, but putting everything in one big class is no good structure. Think in smaller pieces, eg. your `MexGame` could have a member of type `Dice` with a member function `roll`. – 463035818_is_not_an_ai Sep 14 '17 at 16:06

0 Answers0