C++;Powerball-文件拆分&;错误(游戏类型) 自从我做C++以来,已经有一段时间了,但是我试着把这个程序分成三个文件。(Driver.cpp、Powerball.cpp和Powerball.h)我认为一切都在正确的位置。对于当前版本,我在运行时收到以下错误消息。任何帮助或指示都是有帮助的!谢谢
错误 Driver.cpp (56):错误C2660:“游戏”:函数不接受1个参数 (58):错误C2360:“案例”标签跳过了“游戏类型”的初始化 (55):参见“游戏类型”声明 (61):错误C2361:“默认”标签跳过了“游戏类型”的初始化 (55):参见“游戏类型”声明 Driver.cppC++;Powerball-文件拆分&;错误(游戏类型) 自从我做C++以来,已经有一段时间了,但是我试着把这个程序分成三个文件。(Driver.cpp、Powerball.cpp和Powerball.h)我认为一切都在正确的位置。对于当前版本,我在运行时收到以下错误消息。任何帮助或指示都是有帮助的!谢谢,c++,visual-studio-2013,filesplitting,C++,Visual Studio 2013,Filesplitting,错误 Driver.cpp (56):错误C2660:“游戏”:函数不接受1个参数 (58):错误C2360:“案例”标签跳过了“游戏类型”的初始化 (55):参见“游戏类型”声明 (61):错误C2361:“默认”标签跳过了“游戏类型”的初始化 (55):参见“游戏类型”声明 Driver.cpp #include <iostream> #include <iomanip> #include <fstream> #include <string>
#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;
}
}
}
Powerball.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 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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“Powerball.h”
使用名称空间std;
常量短乐透大小=6;//用于跟踪拾取的数组的大小
//用于存储玩家获胜时显示的文本输出
常量字符*常量winTxt[]={
“赢家!-头奖!23000000美元”//5+强力球
“赢家!-$1000000!!”,//5个白色球
“赢家!-$10000!!”,//4个白球+强力球
“赢家!-$100!”,//4或3个白色球+强力球
“赢家!-$7!”,//3或2个白球+强力球
“赢家!-$4!”,//1个白球+强力球
“赢家!-$4!”};//强力球
结构数据集
{
独自漂浮;
浮动匹配1;
浮动匹配2;
浮动匹配3;
浮动匹配4;
浮动匹配5;
};
//如果玩到头奖,用于记录赢款
结构winCount
{
pbSet动力球;
浮动匹配3;
浮动匹配4;
浮动匹配5;
}赢款={0,0,0,0,0,0,0,0,0,0};//跟踪奖金的计数器
空标题()
{
cout问题在于:
在main函数中,函数声明应该是
void game(const bool& type); instead of
void game();
及
为案例2附上支撑:
case 2: // Game
{
bool gameType = false;
game(gameType);
}
认真要求我们调试您的代码??更好的做法不是在driver.cpp中声明game()
it,而是包含Powerball.h。由于参数由const ref获取,因此您可以简单地执行game(false);
没有声明额外变量。@Wimmel:完全同意你的意见。也谢谢你!:)@Wimmel它是Powerball.cpp,作为Powerball.h给了我链接错误。@SeanK\uuu不,不要包含Powerball.cpp。若要修复链接器错误,请将Powerball.cpp添加到你的项目中。包括.cpp现在可以工作,但以后当您的项目将变得更大。
case 2: // Game
{
bool gameType = false;
game(gameType);
}