Reputation: 1
I am writing a hangman game. I am getting this error:Microsoft C++ exception: std::bad_alloc at memory location 0x0017F5E0. When I step through the program it stops here and the output window gives me this error: Microsoft C++ exception: std::bad_alloc at memory location 0x0017F5E0 and I see the message "error reading characters of string.
this is my code
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;
}
}
Here is all my code. I hope this helps so you do not have to guess. 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;
}
Upvotes: 0
Views: 1243
Reputation: 57749
I'm not going to debug your program, but here are some issues that I found:
Use std::vector
not arrays.
Arrays don't know their size, they can't expand on demand and messy when passing around.
Use your structures.
You create a userInfo
structure only to fill from the input.
Your Player
class has the same fields as userInfo
. You should create a userInfo
variable in Player
.
Pass the whole userInfo
structure to the Player
constructor.
You could use an initialization list to copy the userInfo
from the constructor parameter to the userInfo
variable in Player
.
Separate your concepts.
In your present design, the board is embedded into every Player
.
In main()
, at least one player must get the board words.
I suggest making a Board
class. Each Player
can give a guess to the Board
and it can return a response and redraw itself.
The Board
would be in charge of initializing its word list and choosing a guess.
Separate themes into separate translation units.
Put the Player into its own header and source file.
The main
function should be in a separate source file.
This would allow you to make changes in main
without recompiling the Player
.
The concept also helps support loose coupling, encapsulation, and data hiding.
Let the objects do the work.
The userInfo
class should have a method to obtain its member info from the outside world.
A Board
class should perform work related to the board.
A Player
class should perform work related to a player.
The main
function should glue everything together (such as coordinating players).
Upvotes: 2