Microsoft C异常:内存位置0x0017F5E0处的std :: bad_alloc

时间:2013-12-03 19:10:35

标签: c++

我正在写一个刽子手游戏。我收到此错误:Microsoft C ++异常:std :: bad_alloc在内存位置0x0017F5E0。当我单步执行程序时它停在这里,输出窗口给出了这个错误:Microsoft C ++异常:std :: bad_alloc在内存位置0x0017F5E0,我看到消息“读取字符串字符时出错。

这是我的代码

void Player::boardSetup()
{
    unsigned seed =time(0);
    srand(seed);
    word=rand()%100;
    Words1[word]=Word1;
    strcpy_s(Copy,Word1.c_str());

    size=strlen(Word1.c_str());
    for(index=0;index<size;index++)
    {
        Guess[index]='-';
        cout<<Guess[index]<<endl;
    }


}

这是我的所有代码。我希望这有帮助,所以你不必猜测。 班级球员

{   public:
    string fName;
     string lName;
     int DOB;
     string username;
     int SS4;
     string email;
     int won;
     int lost;
     const int static  WordSIZE=15;
     int const static totalWORDS=100;
     string static Letters[WordSIZE];
     string static  Words1[totalWORDS];
     char static   Copy[WordSIZE];
     char static Guess[WordSIZE];
     int index;
     int word;
     int size;
     int isComing;//I need function to initialize these.
     char letter;
     bool correct;//I need a function to initialize these.
     string Word1;
public:
    Player(string,string,int,string,int,string);
    void getWords();
    void boardSetup();
    void playGame();
    void deathBed(int);



};

Player::Player(string first,string last,int birth, string nicname,int SS,string mail)
{
 fName=first;
 lName=last;
 DOB=birth;
 username=nicname;
 SS4=SS;
 email=mail;
 isComing=0;
 correct=true;
}
    const int static  WordSIZE=15;
     int const static totalWORDS=100;
  string Player::  Words1[totalWORDS]; 
    char Player::   Copy[WordSIZE];
     char Player:: Guess[WordSIZE];
      string Player:: Letters[WordSIZE];
 void Player::getWords()
{ 





  ifstream WordBank;
  int index=0;
    WordBank.open("C:\\WordBank\\words.txt");


    if(WordBank)
    {
         while(WordBank>>Words1[index])
         {
           index++;
         }
     WordBank.close();
    }
    else
        {
          cout<<"There was an error."<<endl;
        }

}


    /*string *words2;
    words2=new  string[100];
  ifstream WordBank;
  int index;
    WordBank.open("C:\\WordBank\\words.txt");


    if(WordBank)
    {
       for(index=0;(WordBank>>words2[index]);index++)
        {



        }
    WordBank.close();
    }
    else
        {
          cout<<"There was an error."<<endl;
        }
    delete [] words2;
   words2=0;
}*/

void Player::boardSetup()
{
    unsigned seed =time(0);
    srand(seed);
    word=rand()%100;
    Words1[word]=Word1;
    strcpy_s(Copy,Word1.c_str());

    size=strlen(Word1.c_str());
    for(index=0;index<size;index++)
    {
        Guess[index]='-';
        cout<<Guess[index]<<endl;
    }


}






}

void Player::playGame()
{
    while(isComing!=7)
    {
        deathBed(isComing);
        cout<<Guess<<endl;
        cout<< "Please guess a letter."<<endl;// or press 0 to go to the main screen for help
        cin>>letter;
        letter=toupper(letter);

            for (index=0;index<size;index++)
        {
            if(Copy[index]==letter)
            {
               cout<<"Nice Job"<<endl; //add the ability to see the word
               Guess[index]=letter;
               cout<<Guess[index]<<endl;

            }
            else  if(strcmp(Word1.c_str(),Guess)==0)
              {
                  cout<<"You WIN!!!"<<endl;
                  return;
              }

            else if (correct=false)
            {
                cout<<"Please,Try again"<<endl;
                isComing++;
            }
        }

    }
     void deathBed(int isComing);

     cout<<"The word is"<<Words1[word]<<"."<<endl;

     //draw a big red noose. call a function for it.

}


struct userInfo
{
    string FName;
    string LName;
    int dob;
    string Username;
    int ss4;
    string Email;
};

userInfo getUserInfo();


int main()
{ 
  userInfo i; 
     i=getUserInfo();
 Player player1(i.FName,i.LName,i.dob,i.Username,i.ss4,i.Email);
 player1.getWords();
 player1.boardSetup();
 player1.playGame();

    return 0;
}

userInfo getUserInfo()
{
    userInfo info;

    cout<<"What is your first name?"<<endl;
    cin>> info.FName;
    cout<<"What is your last name?"<<endl;
    cin>>info.LName;
    cout<<"What is your date of birth"<<endl;
    cin>>info.dob;
    cout<<"Please enter a user name."<<endl;
    cin>>info.Username;
    cout<<"Please enter the last four digits of your Social Security number."<<endl;
    cin>>info.ss4;
    cout<<"Please enter your email address."<<endl;
    cin>>info.Email;

    return info;
}

1 个答案:

答案 0 :(得分:2)

我不会调试你的程序,但是我发现了一些问题:

使用std::vector不是数组。
阵列不知道它们的大小,它们无法按需扩展并且在四处传播时会变得混乱。

使用您的结构。
您只创建userInfo结构以填充输入 您的Player类与userInfo具有相同的字段。您应该在userInfo中创建Player变量 将整个userInfo结构传递给Player构造函数 您可以使用初始化列表userInfo从构造函数参数复制到userInfo中的Player变量。

分离您的概念。
在您目前的设计中,每个Player都会嵌入电路板 在main()中,至少有一名玩家必须得到董事会的话 我建议做一个Board课程。每个Player都可以对Board进行猜测,并且可以返回响应并自行重绘 Board将负责初始化其单词列表并选择猜测。

将主题分隔为单独的翻译单元。
将播放器放入自己的标头和源文件中 main函数应位于单独的源文件中 这样您就可以在main中进行更改,而无需重新编译Player 该概念还有助于支持松散耦合封装数据隐藏。

让对象完成工作。
userInfo类应该有一种从外界获取其成员信息的方法 Board班级应该执行与董事会相关的工作 Player课程应该执行与玩家相关的工作 main函数应该将所有内容粘合在一起(例如协调玩家)。

相关问题