我不断收到错误"变量'b'未初始化正在使用,我不确定如何解决它
I keep getting the error "The variable 'b' is being used without being initialized, and I'm not sure how to fix it
//Benjamin McKinney
//CSCI 2010-10
//Spring 2015
//PASS 3
//Programmed on Windows 8.1 using Visual C++ 2010 Express
//This program plays the game MasterMind
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct Player
{
string Name;
int HighScores[6];
bool CheatOn;
};
struct Board
{
int NumHoles;
int Holes[6];
};
struct Guess
{
int Count;
int NumHoles;
int Holes;
};
void printHighScores(string);
void readHighScore(string);
void updateHighScore(string, int);
string getPlayer();
int getBoard();
void playGame(string);
void menu(string);
int main()
{
Player p;
srand((unsigned int)time(0));
cout << "!!! Benjamin McKinney's Master-MasterMind!!!n";
cout << "--------------------------------------------n";
getPlayer();
menu(p.Name);
cout << "Goodbye, " << p.Name << endl;
printHighScores(p.Name);
cout << "----------------------------------------------n";
cout << "!!! Benjamin McKinney's Master-MasterMind!!!n";
system("PAUSE");
return 0;
}
void printHighScores(string name)
{
return;
}
void readHighScore(string)
{
return;
}
void updateHighScore(string, int)
{
return;
}
string getPlayer()
{
Player p;
cout << "What is your name?n";
cin >> p.Name;
cout << "Welcome, " << p.Name << endl;
p.CheatOn = false;
readHighScore(p.Name);
return p.Name;
}
int getBoard()
{
Board b;
cout << "Enter the number of holes you would like: ";
cin >> b.NumHoles;
if(b.NumHoles > 6 || b.NumHoles < 1)
{
cout << "Error! You must pick a number between 1 and 6! Try again!n";
}
for(int i=0;i<b.NumHoles;i++)
{
b.Holes[i] = rand() % 2 + 1;
}
return b.NumHoles;
}
void playGame(string)
{
Player p;
Board b;
Guess g;
getBoard();
g.Count=0;
for(int i=0;i<b.NumHoles;i++)
{
cout << "Enter your guess for the rown";
if(p.CheatOn == true)
{
for(int a=0;a<(sizeof(b.Holes)-1);a++)
{
cout << b.Holes[a];
}
}
cout << "Enter your guess for hole " << i << ": ";
cin >> g.Holes;
g.Count++;
}
return;
}
void menu(string)
{
Player p;
char choice;
cout << "Please choose an option below:n";
cout << "t P)laynt Q)uitntChoice: ";
cin >> choice;
if(choice == 'P')
playGame(p.Name);
else
if(choice == 'Q')
return;
else`enter code here`
if(choice == 'C')
{
p.CheatOn = true;
playGame(p.Name);
}
}
忽略三个高分函数,否则我无法让它工作......"运行时检查失败 #3 - 变量'b'未初始化而被使用"是我遇到的主要问题。如果有人能帮助我,我将不胜感激。谢谢!
在playGame
函数中:
void playGame(string)
{
Player p;
Board b; // <----- uninitialized
// ...
for(int i=0;i<b.NumHoles;i++)
// ^^^^^^^^^^
当您从未初始化过b
时,您可以使用b.NumHoles
。
我猜你打算getBoard()
神奇地对b
产生一些影响,但事实并非如此。getBoard
函数更新本地板,但从不对其执行任何操作。
要解决此问题,您可以更改getBoard
以返回整个板:
Board getBoard()
{
Board b;
// set up b...
return b;
}
然后在playGame
里面:
Board b = getBoard();
下面还有另一个错误:
for(int a=0;a<(sizeof(b.Holes)-1);a++)
sizeof
运算符给出大小(以字节为单位(。您实际上想要元素中的大小,因此您需要除以元素大小:
a < (sizeof b.Holes / sizeof b.Holes[0])
我也不确定-1
的目的是做什么,这只会导致您无法输出最后一个孔。
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 不确定如何装饰我的C++库代码以在 C 中使用
- C++功能泄漏内存,我是C++新手,不确定如何解决
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 为什么以下代码是不确定的?
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 误用指针会产生分段错误。我仍然不确定如何纠正它
- 我不确定为什么代码没有运行,它在编译时抛出错误,说它无法找到 PrintArray()
- 不确定出了什么问题(字符串输入)
- C++睡眠时间不确定
- 在C++中做一个场景问题,我不确定如何处理布尔和if循环
- 我收到阻止我运行程序的警告,但不确定如何解决
- 未解决的内容包括在Eclipse CDT中,其中路径名不确定
- 我只需要一些调试帮助(C++)。我遇到了多个不确定如何解决的问题
- 我不断收到错误"变量'b'未初始化正在使用,我不确定如何解决它
- 不断收到有关类型匹配的错误.不确定这意味着什么或如何解决它
- 也许快速错误但我不确定如何解决
- 错误LNK2019 -不确定如何修复(1未解决的外部)
- C++程序-不确定是否,我提供了正确的解决方案