Jump to content
  • Advertisement
Sign in to follow this  
ARC inc

Program Crashing after making file

This topic is 3029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
You know that "this" is a pointer? The value is 0x2. This is very close to null (0x0). Sometimes the compiler will offset pointers a little, usually when accessing nested members or arrays.

Share this post


Link to post
Share on other sites
The problem is in the call site. You are calling "new_game" on an null pointer (or references). Somewhere, you are going to have to allocate an instance of Game (either on the stack or via new).

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!