Question about 'saving'

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1205 Views - Last Post: 05 June 2010 - 11:30 AM Rate Topic: -----

#1 Cyrusthebeast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 11-October 09

Question about 'saving'

Posted 04 June 2010 - 02:12 PM

I'm trying to create a very simple text-based game. I realized that this would suck if it was meant to be longer than like 5 minutes if there was no way to save and come back to it. I looked up how to write to files and read from them and I found this very nice tutorial that explained a lot:

http://www.cpp-home....archives/67.htm

The line that stuck out to me the most for what I will be trying to do was:

Quote

Using File >> str, will put just “Hi!” to the str array. You should have noticed, that it actually reads until it meets a white space.


Would there be some way I could use that into some form of saving? I was thinking that in the text file, it could be like:

Quote

name class gender strength


Just like that, so it'd be like this (just a simple draft that I typed up for the purpose of this post):

cout << "What is your name? \n";
cin >> nName;
file << nName;
cout << "Are you a (1)male or (2)female? \n";
cin >> nGender;
switch (nGender) {
case 1: {
file << " Male";
}
case 2: {
file <, " Female";



So that would make the text file look like this:

Quote

Cyrus Male


And then when it was time to load from the file, the code to do that would go something like:

file << nGender;



Because that would apparently go up until the whitespace. Unfortunately, this is where I get stuck. What would I do to get the gender and other variables after that loaded up?

This post has been edited by Cyrusthebeast: 04 June 2010 - 02:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about 'saving'

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Question about 'saving'

Posted 04 June 2010 - 02:14 PM

As long as you read and write consistently, it doesn't matter what scheme you use. A single line with white space delimiters may run into problems depending on your data set, so usually people use symbols that won't be found in the data, commas, semicolons, etc...
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Question about 'saving'

Posted 04 June 2010 - 02:16 PM

Quote

What would I do to the gender and other variables after that?
Read to them using << operator as well. What stops you from doing this?
Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Question about 'saving'

Posted 04 June 2010 - 03:39 PM

Might make it a bit easier to retrieve the data if each variable was saved on a new line as well.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Question about 'saving'

Posted 04 June 2010 - 03:43 PM

View PostKilorn, on 04 June 2010 - 03:39 PM, said:

Might make it a bit easier to retrieve the data if each variable was saved on a new line as well.



Why? It would require telling the program how many pieces of data to retrieve and store before moving to the next "object". Not necessarily better then other serialization techniques.
Was This Post Helpful? 0
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Question about 'saving'

Posted 04 June 2010 - 07:06 PM

Quote

So that would make the text file look like this:

Quote
Cyrus Male


No, it wouldn't.

it would be "CyrusMale" unless you insert the whitespace yourself, they're talking about using other delimiters that won't be found in the input, other than inserting your own whitespace.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1047
  • View blog
  • Posts: 4,452
  • Joined: 09-June 09

Re: Question about 'saving'

Posted 04 June 2010 - 07:10 PM

when you write data to a file you want to give the data some type of consistent delimiter : a space (' '), comma (','), a tab ('\t') ect....

This post has been edited by ImaSexy: 04 June 2010 - 07:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Question about 'saving'

Posted 04 June 2010 - 07:16 PM

I would probably use the null character '\0' to begin with.
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1047
  • View blog
  • Posts: 4,452
  • Joined: 09-June 09

Re: Question about 'saving'

Posted 04 June 2010 - 07:21 PM

taylor, a null character is not a empty space. How would you parse at NULL characters?
Was This Post Helpful? 0
  • +
  • -

#10 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Question about 'saving'

Posted 04 June 2010 - 07:28 PM

Oh right, he specified he wanted to use a certain method of getting input from the file.
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Question about 'saving'

Posted 04 June 2010 - 08:10 PM

Quote

How would you parse at NULL characters?
Parse NUL characters like you parse any other delimiter. It is perfectly valid to use NUL to delimit. The problem is that it's not human readable. If you're willing to ditch human editability of your files, by all means, use NUL.
Was This Post Helpful? 1
  • +
  • -

#12 Cyrusthebeast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 11-October 09

Re: Question about 'saving'

Posted 04 June 2010 - 08:52 PM

View PostKYA, on 04 June 2010 - 01:14 PM, said:

As long as you read and write consistently, it doesn't matter what scheme you use. A single line with white space delimiters may run into problems depending on your data set, so usually people use symbols that won't be found in the data, commas, semicolons, etc...

+

View PostOler1s, on 04 June 2010 - 01:16 PM, said:

Quote

What would I do to the gender and other variables after that?
Read to them using << operator as well. What stops you from doing this?


Ya know, I'm sorry for the stupid question, I never really realized that XD I just sort of figured that it started at the beginning of the file no matter what.

View PostKilorn, on 04 June 2010 - 02:39 PM, said:

Might make it a bit easier to retrieve the data if each variable was saved on a new line as well.


I'm doing that now for an easier-to-read file.

View Posttaylorc8, on 04 June 2010 - 06:06 PM, said:

Quote

So that would make the text file look like this:

Quote
Cyrus Male


No, it wouldn't.

it would be "CyrusMale" unless you insert the whitespace yourself, they're talking about using other delimiters that won't be found in the input, other than inserting your own whitespace.


Not to be a dick, but if you actually read the code, you'd see that I DID put the whitespace myself.

View PostImaSexy, on 04 June 2010 - 06:10 PM, said:

when you write data to a file you want to give the data some type of consistent delimiter : a space (' '), comma (','), a tab ('\t') ect....


I like whitespace the best :D

----------------------------

Also, stupidiness from the first question aside (again, I'm really sorry for that waste of you guys' time XD), here's another problem I'm having with saving.

Here's my code:

(main.cpp)
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>

int nTest();						//Forward declaration!
void vSave(int Name, int Gender, int Order, int Strength, int Health, int Mana, int Defense, int Chapter, int Level);
void vLoad(int Name, int Gender, int Order, int Strength, int Health, int Mana, int Defense, int Chapter, int Level);

using namespace std;

int main () {
	srand(time(NULL));				//seeds the time ONCE (ty Oler1s)
	int nCounter=0;					//starts the counter for 'for' loops
	int nStartR=1;
	int nInput;
	int nName, nGender, nOrder, nStr, nHealth, nMana, nDefense, nChapter, nLevel;
	nName=-1;
	nGender=-1;
	nOrder=-1;
	nStr=-1;
	nHealth=-1;
	nMana=-1;
	nDefense=-1;
	nChapter=-1;
	nLevel=-1;
	
	while (nStartR) {
		cout << "Hello! What would you like to do? " << endl;
		cout << "1. Load Game" << endl;
		cout << "2. New Game (will erase any saved data)" << endl;
		cin >> nInput;
		switch(nInput) {
		case 1: {
			vLoad(nName, nGender, nOrder, nStr, nHealth, nMana, nDefense, nChapter, nLevel);
			break;
				}
		case 2: {
			cout << "Starting a new game..." << endl;
			cout << "Hello! Welcome to the game! " << endl;
			cout << "What is your name? \n";
			cin >> nName; 
			cout << nName; //Debugging purposes
			vSave(nName, nGender, nOrder, nStr, nHealth, nMana, nDefense, nChapter, nLevel);
			nStartR=0;
				}
			break;
		default: {
			cout << "Unknown option. Restarting..." << endl;
				 }
		}
	}

		
		

	system("PAUSE");
	return 0;
}


(functions.cpp)
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>

using namespace std;

int nTest() {								//Random number out of 10
        cout << rand() % 10 + 1 << endl;

        return 0;
}

void vSave(int Name, int Gender, int Order, int Strength, int Health, int Mana, int Defense, int Chapter, int Level) {
	ofstream saveFile ("save.txt", ios::in | ios::out, ios::trunc);
	saveFile 
		<< Name << "\n" 
		<< Gender << "\n" 
		<< Order << "\n" 
		<< Strength << "\n" 
		<< Health << "\n" 
		<< Mana << "\n" 
		<< Defense << "\n"
		<< Chapter << "\n"
		<< Level << "\n";
	saveFile.close();
	}

void vLoad(int Name, int Gender, int Order, int Strength, int Health, int Mana, int Defense, int Chapter, int Level) {
	fstream loadFile ("save.txt", ios::in | ios::out);
	loadFile >> Name;
	loadFile >> Gender;
	loadFile >> Order;
	loadFile >> Strength;
	loadFile >> Health;
	loadFile >> Mana;
	loadFile >> Defense;
	loadFile >> Chapter;
	loadFile >> Level;
	cout << Name;			//test to see if it's working. 
	loadFile.close();
	}


So as you can see, it's the start for a game, I have most of the variable's set up. Unfortunately, when I run the program and press the #2 to start a new game, the case statement starts and asks me for my name. If I put a number or a symbol, it will print that to the name and work as it should (print it to the text file), unfortunately, most names aren't symbols, and if I put a name in (for example: Cyrus), it will keep the nName variable as it's original -1 value (for debugging purposes).

I looked up cin and found this very informational post right here:
http://www.daniweb.c...orial71858.html

So I tried to replace the int type of nName with a string, and the compiler complained and wouldn't compile. This is stupid but I tried it anyways, but I replaced the ints (ONLY for the variables like character, level, etc, not for the functions like int main, int nTest, etc) with strings.

Unfortunately, take a look:

1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(9): error C2065: 'string' : undeclared identifier
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(9): error C2146: syntax error : missing ')' before identifier 'Name'
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(9): error C2182: 'vSave' : illegal use of type 'void'
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(9): error C2059: syntax error : ')'
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(10): error C2065: 'string' : undeclared identifier
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(10): error C2146: syntax error : missing ')' before identifier 'Name'
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(10): error C2182: 'vLoad' : illegal use of type 'void'
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(10): error C2059: syntax error : ')'
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(15): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(37): error C2064: term does not evaluate to a function taking 9 arguments
1>c:\users\cyrus\documents\visual studio 2010\projects\game\main.cpp(45): error C2064: term does not evaluate to a function taking 9 arguments


So I tried to replace just nName with string sName and it's telling me that I cannot convert string to int, which is weird because I even changed the functions to deal with string name rather than int name, so it shouldn't be converting to an int. What can I do to fix this problem?


--------------

Also, I'm sorry for asking so many questions, I know it probably seems like I'm not doing any work at all, but I am, it's just that some concepts either take a long time or I just can't grasp without some other help, like for example, I always needed a teacher to explain math concepts to me in different ways, the books we had just couldn't explain it to me and I needed a different way XD
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Question about 'saving'

Posted 04 June 2010 - 09:10 PM

View PostCyrusthebeast, on 05 June 2010 - 12:52 PM, said:

So I tried to replace the int type of nName with a string, and the compiler complained and wouldn't compile.


Show us the code with nName as a string and the compile errors that code causes.

View PostCyrusthebeast, on 05 June 2010 - 12:52 PM, said:

Also, I'm sorry for asking so many questions,

It's fine.
So long as you are reading the answers and learning, and you are, then all is good.
Was This Post Helpful? 0
  • +
  • -

#14 Cyrusthebeast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 11-October 09

Re: Question about 'saving'

Posted 04 June 2010 - 09:20 PM

View Postjanotte, on 04 June 2010 - 08:10 PM, said:

View PostCyrusthebeast, on 05 June 2010 - 12:52 PM, said:

So I tried to replace the int type of nName with a string, and the compiler complained and wouldn't compile.


Show us the code with nName as a string and the compile errors that code causes.

View PostCyrusthebeast, on 05 June 2010 - 12:52 PM, said:

Also, I'm sorry for asking so many questions,

It's fine.
So long as you are reading the answers and learning, and you are, then all is good.


#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>

int nTest();                                            //Forward declaration!
void vSave(int Name, int Gender, int Order, int Strength, int Health, int Mana, int Defense, int Chapter, int Level);
void vLoad(int Name, int Gender, int Order, int Strength, int Health, int Mana, int Defense, int Chapter, int Level);

using namespace std;

int main () {
        srand(time(NULL));                              //seeds the time ONCE (ty Oler1s)
        int nCounter=0;                                 //starts the counter for 'for' loops
        int nStartR=1;
        int nInput;
        int nGender, nOrder, nStr, nHealth, nMana, nDefense, nChapter, nLevel;
        nGender=-1;
        nOrder=-1;
        nStr=-1;
        nHealth=-1;
        nMana=-1;
        nDefense=-1;
        nChapter=-1;
        nLevel=-1;
		string sName="-1";
        
        while (nStartR) {
                cout << "Hello! What would you like to do? " << endl;
                cout << "1. Load Game" << endl;
                cout << "2. New Game (will erase any saved data)" << endl;
                cin >> nInput;
                switch(nInput) {
                case 1: {
                        vLoad(sName, nGender, nOrder, nStr, nHealth, nMana, nDefense, nChapter, nLevel);
                        break;
                                }
                case 2: {
                        cout << "Starting a new game..." << endl;
                        cout << "Hello! Welcome to the game! " << endl;
                        cout << "What is your name? \n";
                        getline(cin,sName);
                        cout << sName; //Debugging purposes
                        vSave(sName, nGender, nOrder, nStr, nHealth, nMana, nDefense, nChapter, nLevel);
                        nStartR=0;
                                }
                        break;
                default: {
                        cout << "Unknown option. Restarting..." << endl;
                                 }
                }
        }

                
                

        system("PAUSE");
        return 0;
}


It actually won't let me compile with string sName=-1; (I'm not quite sure what I did to let it compile in the first one, I need to pay attention more) so I put it in quotes, but even if it wasn't in quotes and I just put the starting value as 0 it still won't work. If I hover my mouse over sName in case 1 it says:

Quote

no suitable conversion function from std::string to int exists


But as I said before, I'm not trying to convert it.
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Question about 'saving'

Posted 04 June 2010 - 09:28 PM

View PostCyrusthebeast, on 05 June 2010 - 01:20 PM, said:

Quote

no suitable conversion function from std::string to int exists


But as I said before, I'm not trying to convert it.


Yes you are.
Look at your prototype for this function:
void vLoad(int Name, int Gender, int Order, int Strength, int Health, int Mana, int Defense, int Chapter, int Level);


What type is expected for the first argument?
What type are you passing in?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2