Hello there,

im writing code for my asteroids game and am nearly finished but ive come across a problem when trying to load the file

the save game is written like this and works perfectly fine, it saves the things i want to a text file:

void WrapGame::save()
{
	cout<<"enter the filename (Max 20 characters)"<<endl;
	cin>>Filename;
	ofstream saveGame(Filename, ios::out);
	saveGame<<gameState<<"\n"<<score<<"\n"<<health<<"\n"<<lives;
	saveGame.close();
}

The load game function however throws me a red line under the first >> after savegame on the last line of the function is called

void WrapGame::load()
{
	cout<<"Enter the name of the file you wish to load:"<<endl;
	cin>>Filename;
	ifstream saveGame(Filename, ios::in);
        saveGame>>gameState >> score>>health>>lives;
}

where am i going wrong?


any help would be awesome

cheers

Benny

Recommended Answers

All 4 Replies

Depends on what "red line" means. We can't even hazzard a guess without more information. Like maybe an error message.

ok so i figured out why its giving me red lines, i was not declaring a variable to store the save game file stuff in.

i need to load up the 4 things i have saved how would i go about doing this?

the code im using looks like this

int var1;
	cout<<"Enter the name of the file you wish to load:"<<endl;
	cin>>Filename;
	ifstream saveGame;
	saveGame.open(Filename);
	savegame>>var1;
	cout<<" "<<var1<<endl;
	saveGame.close();

is that more on the right track? i was getting errors saying it hasnt been initialized so i gave it a value of 0. it doesnt seem to be outputting the gamestate however which should be 1

1) Give your variables useful names. var1 could be gallons of milk for all we know.
2) What does the input file actually look like?
3) Maybe whatever value it outputs is exactly what's in the file.
4) Since you have no error checking, maybe the file can't be found, file is locked, etc, when you tried to open it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.