C++强力球 - 文件拆分和错误(游戏类型)

C++ Powerball - File Splitting & Error (gameType)

本文关键字:错误 游戏 游戏类 类型 拆分 文件 C++      更新时间:2023-10-16

我已经有一段时间没有做C++了,但我正在尝试将该程序拆分为三个文件。(Driver.cpp,Powerball.cpp和Powerball.h),我认为一切都在正确的地方。使用当前版本,我在运行它时收到以下错误消息。任何帮助或指示都是有帮助的!谢谢!

错误

驱动程序.cpp(56):错误 C2660:"游戏":函数不接受 1 个参数

(58):错误 C2360:"案例"标签跳过"游戏类型"的初始化

(55):参见"游戏类型"的声明

(61):错误 C2361:"默认"标签跳过"游戏类型"的初始化

(55):参见"游戏类型"的声明

驱动程序.cpp

#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 ben"
            << "between 1 and 42, and may repeat a previously picked value.n"
            << "All picks must be whole number values.nn"
            << "There are 9 possible winning combinations:n"
            << "5 matches + powerball  $23,000,000  Odds 1 in 175,223,510n"
            << "5 matches              $1,000,000   Odds 1 in 153,632.65n"
            << "4 matches + powerball  $10,000      Odds 1 in 648,975.96n"
            << "4 matches              $100         Odds 1 in 19,078.53n"
            << "3 matches + powerball  $100         Odds 1 in 12,244.83n"
            << "3 matches              $7           Odds 1 in 360.14n"
            << "2 matches + powerball  $7           Odds 1 in 706.43n"
            << "1 match + powerball    $4           Odds 1 in 110.81n"
            << "Powerball only         $4           Odds 1 in 55.41nn"
            << "Press any key to return to the main menunn";
        _getch();
        break;
    case 2:  // Game
        bool gameType = false;
        game(gameType);
        break;
    case 3:  // Exit
        return 0;
        break;
    default:    // Invalid choice
        cout << "Invalid menu choice.nn";
        break;
    }
}


}

强力球.h

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
*/

强力球.cpp

#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 instructionsn"
        << "2. Play standard gamen"
        << "3. Exitn";
    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 repeatn";
    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 mayn"
                << "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;
}

问题出在以下方面:在主函数中,函数声明应该是

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

和将案例 2 的大括号括起来:

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