简体   繁体   English

Project4.exe中0x00998876处的首次机会异常:0xC0000005:访问冲突写入位置0x00000000

[英]First-chance exception at 0x00998876 in Project4.exe: 0xC0000005: Access violation writing location 0x00000000

I am writing a hangman game. 我正在写一个子手游戏。 I cannot proceed with debugging because I do not understand this error: 我无法继续进行调试,因为我不了解此错误:

First-chance exception at 0x00998876 in Project4.exe: 0xC0000005: Access violation writing location 0x00000000. Project4.exe中0x00998876处的第一次机会异常:0xC0000005:访问冲突写入位置0x00000000。 Unhandled exception at 0x00998876 in Project4.exe: 0xC0000005: Access violation writing location 0x00000000. Project4.exe中0x00998876的未处理异常:0xC0000005:访问冲突写入位置0x00000000。

This is where the error is being generated: 这是生成错误的地方:

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

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

The Words array is declared as a member variable. Words数组被声明为成员变量。

Here is my code. 这是我的代码。 I am not yet sure how formatting words and I am trying to finish this program. 我还不确定如何格式化单词,我正在尝试完成此程序。

class Player
{   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;
}

您没有向我们展示您的测试用例 ,但是调试错误清楚地表明您正在通过空指针进行写操作(可能在Words1 )。

I debugged your code on Visual Studio 2012. You have a problem with your boardSetup() function that is preventing the game from being played. 我在Visual Studio 2012上调试了您的代码。boardSetup()函数存在问题,导致无法玩游戏。 Your assignment for Word1 is the wrong way. 您为Word1分配的方法是错误的。 I mean you should have 我的意思是你应该

Word1=Words1[word];

Instead of 代替

Words1[word]=Word1;

After this the program runs and you can play. 之后,程序将运行,您可以播放了。 However when you win it does not say so. 但是,当您获胜时,它不会这么说。 It just keeps asking for more letters. 它只是不断要求更多的信件。 I will leave that exercise up to you. 我将把这项工作留给您。 I did not see any crash, null pointer access or bad_alloc (although those were not expected since you are not using pointers). 我没有看到任何崩溃,空指针访问或bad_alloc(尽管由于您未使用指针而未预期)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Project0_opengl.exe 中的 0x00000000 处抛出异常:0xC0000005:访问冲突执行位置 0x00000000 - Exception thrown at 0x00000000 in Project0_opengl.exe: 0xC0000005: Access violation executing location 0x00000000 在0xC0000005中的0x6ececafa出现未处理的异常:访问冲突写入位置0x00000000 - Unhandled Exception as at 0x6ececafa in 0xC0000005 : Access violation writing location 0x00000000 在 CobwebDiagram.exe 中的 0x00000000 处引发异常:0xC0000005:访问冲突执行位置 0x00000000 - Exception thrown at 0x00000000 in CobwebDiagram.exe: 0xC0000005: Access violation executing location 0x00000000 CEDel.exe中0x03f7111c处的第一次机会异常:0xC0000005:访问冲突写入位置0x002e0364 - First-chance exception at 0x03f7111c in CEDel.exe: 0xC0000005: Access violation writing location 0x002e0364 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 0xC0000005:访问冲突执行位置0x00000000 - 0xC0000005: Access violation executing location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 MyString.exe中0x0FC9E559(ucrtbased.dll)的未处理异常:0xC0000005:访问冲突写入位置0x00000000 - Unhandled exception at 0x0FC9E559 (ucrtbased.dll) in MyString.exe: 0xC0000005: Access violation writing location 0x00000000 IXAudio2-0xC0000005:访问冲突写入位置0x00000000 - IXAudio2 - 0xC0000005: Access violation writing location 0x00000000 NVIDIA未处理的异常,位于0x002a2da2中 <work.exe> 0xC0000005:访问冲突读取位置0x00000000 - NVIDIA Unhandled exception at 0x002a2da2 in <work.exe>0xC0000005: Access violation reading location 0x00000000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM