我不断收到错误"变量'b'未初始化正在使用,我不确定如何解决它

I keep getting the error "The variable 'b' is being used without being initialized, and I'm not sure how to fix it

本文关键字:不确定 解决 何解决 初始化 错误 变量      更新时间:2023-10-16
//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的目的是做什么,这只会导致您无法输出最后一个孔。