在方法c ++之间传递值

时间:2013-11-19 11:33:29

标签: c++ class parameter-passing

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

我知道这段代码目前有很多不好的语法等,但它目前只是粗略的工作代码(即在选择随机或自己的数字时尚无验证)

我在这里问的是如何获得int k中的任何值(从compareNumbers1()和compareNumbers2())到results()。

就我的想法而言,它需要以某种方式使用指针传递,或者将compareNumbers从void更改为int并返回k。然而,在玩这两种方式后,我仍然无法让它发挥作用。

1 个答案:

答案 0 :(得分:1)

一种选择是使用参数。将值作为参数传递给结果:

void results(int k);

这就留下了如何从compareNumbers1()中解脱出来的问题。你可以从那个函数返回它:

int compareNumbers1();

如果您需要将其放入compareNumbers2(),请让该方法对其进行修改,然后允许调用者查看您传递对变量的引用的修改:

void compareNumbers2(int& k);

你显然需要在某处声明变量。因为它是从compareNumbers1()返回的,所以你会声明它并像这样分配它:

int k = compareNumbers1();

将所有内容放在通话网站上,您可以:

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

参数的明显替代方法是使变量成为类的私有成员变量。