Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
自从我完成C ++以来已经有一段时间了,但是我试图将该程序分为三个文件。 (Driver.cpp,Powerball.cpp和Powerball.h),我认为一切都在正确的位置。使用当前版本,在运行它时会收到以下错误消息。任何帮助或指示都是有帮助的!谢谢!

失误

驱动程序
(56):错误C2660:“游戏”:函数未采用1个参数

(58):错误C2360:“ caseType”标签跳过了“ gameType”的初始化

(55):参见“ gameType”的声明

(61):错误C2361:“默认”标签跳过了“ gameType”的初始化

(55):参见“ gameType”的声明

驱动程序

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <conio.h>
#include <ctime>

using namespace std;

int main()
{
void header();
int menu();
void game();

cout.setf(std::ios::fixed);  // Remove counter's E notation
cout.precision(0);   // Don't display decimal

bool gameType = false;
// true - jackpot game
// false - standard game

while (true)
{

    switch (menu()) // Display menu & get users selection
    {
    case 1:  // Show instructions
        cout << "You will be asked to pick a total of 6 numbers.\n"
            << "The first five numbers must be a value between 1 and 55.\n"
            << "The last number, the powerball, must be\n"
            << "between 1 and 42, and may repeat a previously picked value.\n"
            << "All picks must be whole number values.\n\n"
            << "There are 9 possible winning combinations:\n"
            << "5 matches + powerball  $23,000,000  Odds 1 in 175,223,510\n"
            << "5 matches              $1,000,000   Odds 1 in 153,632.65\n"
            << "4 matches + powerball  $10,000      Odds 1 in 648,975.96\n"
            << "4 matches              $100         Odds 1 in 19,078.53\n"
            << "3 matches + powerball  $100         Odds 1 in 12,244.83\n"
            << "3 matches              $7           Odds 1 in 360.14\n"
            << "2 matches + powerball  $7           Odds 1 in 706.43\n"
            << "1 match + powerball    $4           Odds 1 in 110.81\n"
            << "Powerball only         $4           Odds 1 in 55.41\n\n"
            << "Press any key to return to the main menu\n\n";
        _getch();
        break;
    case 2:  // Game
        bool gameType = false;
        game(gameType);
        break;
    case 3:  // Exit
        return 0;
        break;
    default:    // Invalid choice
        cout << "Invalid menu choice.\n\n";
        break;
    }
}




}


强力球

void header();
int menu();
/* Main menu function
** Outputs menu options and returns the player's choice
** 1 = show instructions
** 2 = play standard game
** 3 = exit
*/

void game(const bool& type);
/* Main game function
** boolean argument determines the type of game
*/

short* makePicks(const short& size);
/* Prompts the user to input their choices
** checks that those choices are valid, then
** returns a pointer to the array of picks
*/

short* makePicksRand(const short& size);
/* Selects random numbers, checks them for validity
** then returns pointer to the array
*/

bool validate(const short& num, const short* picks);
/* Checks the passed array for validity.
** see instructions for the criteria to determine
** if a number is valid
*/

bool checkWin(const short* player, const short* random, const bool& type);
/* Checks the users selections against the random selections
** if standard gametype, returns true whenever a match is found
** if jackpot gametype, returns true *only* when all numbers match
*/


强力球

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <conio.h>
#include <ctime>
#include "Powerball.h"

using namespace std;

const short LOTTO_SIZE = 6;  // Size of arrays for tracking picks

// Used to store the text output shown when the player wins
const char* const winTxt[] = {
"Winner! - Jackpot! $23,000,000",    // 5 + powerball
"Winner! - $1,000,000!!",    // 5 white balls
"Winner! - $10,000!!",       // 4 white balls + Powerball
"Winner! - $100!",           // 4 or 3 white balls + Powerball
"Winner! - $7!",             // 3 or 2 white balls  + Powerball
"Winner! - $4!",             // 1 white ball + Powerball
"Winner! - $4!" };           // Powerball

struct pbSet
{
    float alone;
    float match1;
    float match2;
    float match3;
    float match4;
    float match5;
};

// Used to keep tally of wins if playing until jackpot
struct winCount
{
    pbSet powerball;
    float match3;
    float match4;
    float match5;
} winnings = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Counter to track the winnings


void header()
{
    cout << "Sean Kilbane C++ II"
         << "\nPowerball: Program 1";
}

int menu()
{
    short temp = 0;
    cout << endl
        << "1. View instructions\n"
        << "2. Play standard game\n"
        << "3. Exit\n";
    cin >> temp;
    cout << endl;
    if (!cin)
    {
        cin.clear();
        cin.ignore();
        return 0;
    }
    else
        return temp;
}

void game(const bool& type)
{
    short *playerPicks,  // numbers chosen by the player
        *randPicks;  // random numbers (winning picks)
    float counter = 1.0f;    // counter of number of tries
    bool win = false;    // tracks win condition
    playerPicks = makePicks(LOTTO_SIZE);
    cout << endl
        << "You've chosen: ";
    for (short i = 0; i < LOTTO_SIZE; i++)
        cout << playerPicks[i] << " ";
    cout << endl
        << "Press any key to begin playing.\n";
    _getch();
    {
        while (!win)
        {
            randPicks = makePicksRand(LOTTO_SIZE);
            cout << "Try " << counter << ": ";
            for (short i = 0; i < LOTTO_SIZE; i++)
                cout << randPicks[i] << " ";
            cout << endl;
            win = checkWin(playerPicks, randPicks, type);
            counter++;
            delete[] randPicks;
        }
    }   // end section to be timed, timer ends via destructor
    cout << endl;
    if (type)
        cout << "Before you hit the Jackpot, you also collected the following wins:\n"
        << "Powerball only - " << winnings.powerball.alone << endl
        << "Powerball + 1 match - " << winnings.powerball.match1 << endl
        << "Powerball + 2 matches - " << winnings.powerball.match2 << endl
        << "3 matches - " << winnings.match3 << endl
        << "Powerball + 3 matches - " << winnings.powerball.match3 << endl
        << "4 matches - " << winnings.match4 << endl
        << "Powerball + 4 matches - " << winnings.powerball.match4 << endl
        << "5 matches - " << winnings.match5 << endl
        << endl;
    cout << "Press any key to return to the main menu.\n";
    _getch();
    delete[] playerPicks;
}

short* makePicks(const short& size)
{
    short *temp = new short[size];
    bool repeat = false;
    cout << "Pick your first 5 numbers. Choices must be from 1-55, and may not repeat\n";
    for (short i = 0; i < LOTTO_SIZE;) // increment counter manually, later
    {
        if ((i == 5) && (!repeat))
        {
            cout << "Now, pick your powerball number. Choice must be from 1-42, and may\n"
                << "repeat any previous pick.\n";
            repeat = true;
        }
        cout << "Pick " << (i + 1) << ": ";
        cin >> temp[i];
        if (!cin)
        {
            cin.clear();
            cin.ignore();
            cout << "Invalid input.\n";
        }
        else
        {
            if (validate(i, temp))
                i++;
            else
                cout << "Pick " << (i + 1) << " conflicts with a previous choice or is invalid.\n";
        }
    }
    return temp;
}

short* makePicksRand(const short& size)
{
    short *temp = new short[size];
    for (short i = 0; i < LOTTO_SIZE;)   // will increment counter manually
    {
        if (i == 5)
            temp[i] = (rand() % 42) + 1;
        else
            temp[i] = (rand() % 55) + 1;
        if (validate(i, temp))
            i++;
    }
    return temp;
}

bool validate(const short& num, const short* picks)
{
    if (num == 5)   // when checking the last number (powerball)
    {
        if ((picks[num] < 1) || (picks[num] > 42))
            return false;
        else
            return true;
    }
    else     // checks all other numbers
    {
        if ((picks[num] > 55) || (picks[num] < 1))
            return false;
        else if (num > 0)
        for (short i = 0; i <= num; i++)
        if (picks[i] == picks[i + 1])
            return false;
        return true;
    }
}

bool checkWin(const short* player, const short* random, const bool& type)
{
    bool pbMatch = false;
    short matches = 0;
    for (short i = 0; i < LOTTO_SIZE; i++)
    {
        if (player[i] == random[i])
        {
            if (i == 5)
                pbMatch = true;
            else
                matches++;
        }
    }
    if (pbMatch)
        switch (matches)
    {
        case 0:  // 4
            cout << winTxt[6] << endl;
            if (type)
            {
                winnings.powerball.alone++;
                return false;
            }
            else
                return true;
            break;
        case 1:  // 4
            cout << winTxt[5] << endl;
            if (type)
            {
                winnings.powerball.match1++;
                return false;
            }
            else
                return true;
            break;
        case 2:  // 7
            cout << winTxt[4] << endl;
            if (type)
            {
                winnings.powerball.match2++;
                return false;
            }
            else
                return true;
            break;
        case 3:  // 100
            cout << winTxt[3] << endl;
            if (type)
            {
                winnings.powerball.match3++;
                return false;
            }
            else
                return true;
            break;
        case 4:  // 10k
            cout << winTxt[2] << endl;
            if (type)
            {
                winnings.powerball.match4++;
                return false;
            }
            else
                return true;
            break;
        case 5:  // jackpot
            cout << winTxt[0] << endl;
            return true;
    }
    else
        switch (matches)
    {
        case 3:  // 7
            cout << winTxt[4] << endl;
            if (type)
            {
                winnings.match3++;
                return false;
            }
            else
                return true;
            break;
        case 4:  // 100
            cout << winTxt[3] << endl;
            if (type)
            {
                winnings.match4++;
                return false;
            }
            else
                return true;
            break;
        case 5:  // 200k
            cout << winTxt[1] << endl;
            if (type)
            {
                winnings.match5++;
                return false;
            }
            else
                return true;
            break;
    }
    return false;
}

最佳答案

问题出在以下方面:
在main函数中,函数声明应为

void game(const bool& type); instead of
void game();



将案例2的括号括起来:

case 2:  // Game
        {
        bool gameType = false;
        game(gameType);
        }

关于c++ - C++ Powerball-文件拆分和错误(gameType),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21052213/

10-09 03:57