Sign in to follow this  
ARC inc

Program Crashing after making file

Recommended Posts

Alright so seems my program keeps on crashing without any type of warning the problem seems to be inside the saving file part of the program
void Game::new_game()
{
   ofstream account("account.txt");
   if(account.is_open())
   {
      cout << "Please Give your player a name!\n";
      cin >> name;
      account << name;
      account.close();
   }
   else cout << "Unable to open";
}
This is where is seems to be getting it's issues it crashes after you input your player's name, any ideas why it would do this?

Share this post


Link to post
Share on other sites
it was very helpful, I didn't think about the debugger hahaha easy to over look I suppose. My debugger came up this

#0 0043D70C std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) () (??:??)
#1 0040145B Game::new_game() (this=0x2) (C:/Documents and Settings/Computer/Desktop/Game/Untitled Text Game//load_save.h:24)
#2 004015C0 main() (C:/Documents and Settings/Computer/Desktop/Game/Untitled Text Game/main.cpp:16)

I've never seen anything like this before an have never once came accross this type of error, any ideas?

Share this post


Link to post
Share on other sites
Quote:
#2 004015C0 main() (C:/Documents and Settings/Computer/Desktop/Game/Untitled Text Game/main.cpp:16)


So take a look at that line in that source file.

Most likely, you have something like this:


Game* game; // on a previous line
game->new_game(); // line 16


You cannot just do this. It's equivalent to saying "paint that house", while not actually pointing at a house (or doing anything else to indicate which house you mean).

There is no reason why you should want to do this, anyway. You do not require pointers to use instances of classes. Classes simply define data types, and you can use their instances just like you use primitives: i.e., by value.

Thus:


Game game; // on a previous line; actually creates a game
game.new_game(); // uses the game without any need to "point at" it


which is like saying "build a house and then paint it".




Moving to For Beginners.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this