Sign in to follow this  
SonicD007

Save issues and questions

Recommended Posts

I'm trying to get my saving and loading completed for my text game, but I don't understand why it is having issues allowing the following to compile correctly:
void SaveFiles()
 {
     ofstream fout;
     fout.open(SaveFiles.txt);
     for(int i = 0; i < SavedFiles.size(); ++i)
       { fout << SavedFiles[i] << "\n"; }
       fout.close();
     //SavedFiles read all items stored in vector and write to file,  at startup read file.
 }

 void LoadFiles()
 {
     ifstream fin;
     fin.open(SaveFiles.txt);
     for(int i = 0; i < SavedFiles.size(); ++i)
        { fin >> SavedFiles[i].push_back(); >> "\n"; }
 }
fin.open(SaveFiles.c_str()); doesn't work either. These are the errors it gives out:
main.h: In function `void SaveFiles()':
main.h:645: error: request for member `txt' in `SaveFiles', which is of non-class type `void ()()'
main.h: In function `void LoadFiles()':
main.h:655: error: request for member `txt' in `SaveFiles', which is of non-class type `void ()()'
main.h:657: error: no matching function for call to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back()'

That's my main problem. My other is having my program wait for a user to press a button before moving on to the next part. I have Sleep() functions in place of this for the time being, but I would rather have the user move on when he/she feels like it. people say system("PAUSE") isn't a good thing to use, why I'm not sure, but that's the only way I know how to do that. I had another question but I can't remember what it was -_- so please help me with these two. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by SonicD007
I'm trying to get my saving and loading completed for my text game, but I don't understand why it is having issues allowing the following to compile correctly:

*** Source Snippet Removed ***


fin.open(SaveFiles.c_str()); doesn't work either. These are the errors it gives out:

*** Source Snippet Removed ***

That's my main problem. My other is having my program wait for a user to press a button before moving on to the next part. I have Sleep() functions in place of this for the time being, but I would rather have the user move on when he/she feels like it. people say system("PAUSE") isn't a good thing to use, why I'm not sure, but that's the only way I know how to do that. I had another question but I can't remember what it was -_- so please help me with these two. Thanks.


[edit]err... nevermind. I misread your post lol.
[edit2]yea, I was wrong again lol.

[Edited by - Programmer16 on September 7, 2006 1:08:23 PM]

Share this post


Link to post
Share on other sites
I tried c_str() and these are the errors it gives me:
(I have another two functions which load character data and they work, not sure why these function wont)

main.h: In function `void SaveFiles()':
main.h:645: error: request for member `c_str' in `SaveFiles', which is of non-class type `void ()()'
main.h: In function `void LoadFiles()':
main.h:655: error: request for member `c_str' in `SaveFiles', which is of non-class type `void ()()'
main.h:657: error: no matching function for call to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back()'

Share this post


Link to post
Share on other sites
Quote:
Original post by SonicD007
people say system("PAUSE") isn't a good thing to use, why I'm not sure,

Simian man has a good answer to that in another thread in For Beginners.

I don't think there's anything wrong with using "pause" in a practice setting, but a lot of people will say you should begin good coding habits in practice. You could try writing your own pause function.

EDIT:
I thought I'd also comment on your code. It looks like you're supplying SaveFiles.txt to fout.open() as if it were a variable, when fout.open() expects text. Surround it with quotes like "SaveFiles.txt" to make it a string literal.

Then, you later try to use the variable SavedFiles as a std::string-like variable, but I don't see it declared anywhere. Did you make it a global?

Let me know if I misread your code.

Share this post


Link to post
Share on other sites
try putting quotes around your filename in the open function. ie:

fout.open("SaveFile.txt");

A good way to pause is the following



//when wanting to pause
cout<<"press any key to continue...\n";
WaitForKeyPress();


//waitforkeypress function
void WaitForKeyPress();
{
while(!getch)
{}
}





That function does nothing until a key is pressed.

Share this post


Link to post
Share on other sites
Putting quotes around it worked, thanks. Yes the string is a global. Another program said I shouldn't use too many globals.

Now the pushback() for the load is giving me errors.
This I don't really get.

main.h:657: error: no matching function for call to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back()'

I don't have a lot of experience using files, so this is kinda confusing to understand.

The error occurs in the LoadFiles function.

Share this post


Link to post
Share on other sites
Quote:
Original post by SonicD007
Putting quotes around it worked, thanks. Yes the string is a global. Another program said I shouldn't use too many globals.
There are arguments to be made both ways, but my own guideline is to never use globals if there's another way. You can already see how global variables can be confusing for code maintenance.
Quote:

Now the pushback() for the load is giving me errors.
This I don't really get.

main.h:657: error: no matching function for call to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back()'

I don't have a lot of experience using files, so this is kinda confusing to understand.

The error occurs in the LoadFiles function.


What I don't understand is why you're using push_back() at all. The normal use is in a container like std::vector or std::stack. You would use it like:
std::stack<int> stackOfInts;
stackOfInts.push_back(9);
stackOfInts.push_back(70);
stackOfInts.push_back(616);
You should probably look up other sources to supplement your way of thinking about file I/O.

EDIT:
The MSDN articles on input streams and std::string are a bit dry but fairly helpful.

Share this post


Link to post
Share on other sites
I was using pushback to store all the SaveFile names into the vetor of my program so that when the person playing wants to load their character it would be there. I didn't have this before, and when a person saved, the net time it was run the program didn't know there were player files there so the player couldn't load them. Pushback is supposed to store things in a vector isn't it?

(Gonna take a look at the links now)

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