Public Group

Program Crashing after making file

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

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())
{
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 on other sites
I suspect examining the value of "this" in a debugger might be enlightening.

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 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 on other sites
Ok so how do I fix it? I didn't think my pointer was written incorrectly but I suppose it is

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 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 linegame->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 gamegame.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.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 12
• 10
• 11
• 13
• Forum Statistics

• Total Topics
634096
• Total Posts
3015479
×