user3001499
user3001499

Reputation: 811

Passing values between methods c++

Game.h

#include "RandomNumberGenerator.h"


class Game
{
private:
    int myNumbers[6];
    int userRandomNumbers[6];
    int lotteryRandomNumbers[6];
    int prizeMoney;

public:

    void generateRandomNumbersLottery();
    void generateRandomNumbersUser();
    void userInputNumbers();
    void compareNumbers1();
    void compareNumbers2();
    void results();
};

Game.cpp

#include "Game.h"

void Game::compareNumbers1()
{
    int k = 0;

    for (int i = 0; i < 6; ++i)
    {
        for (int j = 0; j < 6; ++j)
        {
            if (myNumbers[i] == lotteryRandomNumbers[j])
            {
                k++;
            }
        }
    }

    if (k > 0)
    {
        std::cout << "Congratulations you matched: " << k << " number(s)";
    }
    if (k == 0)
    {
        std::cout << "Unfortunatly you matched: " << k << " numbers";
    }

}

void Game::compareNumbers2()
{
    int k = 0;

    for (int i = 0; i < 6; ++i)
    {
        for (int j = 0; j < 6; ++j)
        {
            if (userRandomNumbers[i] == lotteryRandomNumbers[j])
            {
                k++;
            }
        }
    }


    if (k > 0)
    {
        std::cout << "Congratulations you matched: " << k << " number(s)";
    }
    if (k == 0)
    {
        std::cout << "Unfortunatly you matched: " << k << " numbers";
    }
}


void Game::results()
{
    if (k == 0)
    {
        prizeMoney = 0;
        std::cout << "Unfortunatly you need to match atleast 2 numbers to win a prize.";
    }
    if (k == 1)
    {
        prizeMoney = 0;
        std::cout << "Unfortunatly you need to match atleast 2 numbers to win a prize.";
    }
    if (k == 2)
    {
        prizeMoney = 3;
        std::cout << "Congratulations, you have won: 3 pounds."; 
    }
    if (k == 3)
    {
        prizeMoney = 30;
        std::cout << "Congratulations, you have won: 30 pounds."; 
    }
    if (k == 4)
    {
        prizeMoney = 3000;
        std::cout << "Congratulations, you have won: 3,000 pounds."; 
    }
    if (k == 5)
    {
        prizeMoney = 30000;
        std::cout << "Congratulations, you have won: 30,000 pounds."; 
    }
    if (k == 6)
    {
        prizeMoney = 3000000;
        std::cout << "Congratulations, you have won: 3,000,000 pounds."; 
    }
}

Main.cpp

#include "Game.h"

    std::cout << "Do you wish to enter your own numbers or generate them randomly?: ('own',     'random') ";
    std::getline (std::cin, choice);
    if (choice == "random") 
    { 
        play.generateRandomNumbersUser(); 
        std::cout << std::endl << std::endl;

        play.generateRandomNumbersLottery();
        std::cout << std::endl << std::endl;

        play.compareNumbers2(); 
    }

    if (choice == "own") 
    { 
        play.userInputNumbers(); 
        std::cout << std::endl << std::endl;
        play.generateRandomNumbersLottery(); 
        std::cout << std::endl << std::endl;

        play.compareNumbers1(); 
    }

    play.results();

    system("pause");
    return 0;
}

I'm aware this code has a lot of poor syntax etc in it at the moment but it currently just rough working code (i.e. no validation yet when choosing random or own numbers)

What I'm asking here is how do I get whatever value is in int k (from compareNumbers1() and compareNumbers2()) into results().

As far as my thinking goes it needs to be passed using a pointer somehow, or changing compareNumbers from void to int and return k. However after playing around with both these ways I still cannot get it to work.

Upvotes: 1

Views: 801

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

One option is to use parameters. Pass the value as a parameter into results:

void results(int k);

Which leaves the question of how to get it out of compareNumbers1(). You could return it from that function:

int compareNumbers1();

If you need to put it into compareNumbers2(), let that method modify it, and then allow the caller to see the modifications you'd pass a reference to the variable:

void compareNumbers2(int& k);

And you'd clearly need to declare the variable somewhere. Since it is returned from compareNumbers1() you would declare it and assign it like this:

int k = compareNumbers1();

Put it all together at the call site and you have:

int k = compareNumbers1();
compareNumbers2(k);
results(k);

The obvious alternative to parameters is to make the variable be a private member variable of the class.

Upvotes: 1

Related Questions