To answer your questions directly, you'd probably want to do something like this:
// in do_load():
int idx;
while ( Profile1 >> idx ) { // read: for all integers the file (more specifically: until an non-integer is encountered)
switch ( idx ) {
case 0: havePickaxe = true; break;
case 1: haveSword = true; break;
case 2: havePotion = true; break;
// .
// .
// .
default:
cout << "Unknown item index: " << idx << endl;
}
}
// in do_save():
if ( havePickaxe )
file << 1 << endl;
if ( haveSword )
file << 2 << endl;
if ( havePotion )
file << 3 << endl;
// .
// .
// .
Unfortunately this entails a lot of work when you add a new item. The code is also very cumbersome. What happens if you accidentally mistook haveAwesomeItem as 4 instead of 7 in the save code?
Here are a few suggestions:
Familiarize and experiment with
arrays.
Next, do the same with
enumerations.
Then decide how you want to store your items. It seems that you're accessing your items via "havePickaxe" or "haveSword".
Why not represent this with an array, such as "bool haveItem[10]"? Then "havePickaxe" becomes "haveItem[0]" and "haveSword" becomes "haveItem[1]".
Can a player have more than one of a specific item type? If so, then it becomes "int items[10]", which tracks how many of each item type the player has. Why not use an enumeration instead of magic numbers to keep track of which slot in the array an item type belongs? (e.g.: haveItem[item_pickaxe] instead of haveItem[0]?)
From here, the design becomes simple:
// enumeration to keep track of the different item types
enum item_type {
item_pickaxe, // == 0
item_sword, // == 1
item_potion, // == 2
/* ... */ // == n
item_type_count // n + 1
};
// the actual inventory of the player
// an array of integers which keep track of the amount
// of each item_type the player has
int items[item_type_count] = {}; // <-- the curly brackets initialize the entire array with 0
bool do_save() {
// open the file
ofstream fs( "profile.txt" );
if ( !fs )
return false; // failed to open the file
// write how many of each item the player has in sequence, space-separated to the file
for ( int idx = 0; idx < item_type_count; ++idx )
fs << items[idx] << ' ';
return true;
} // (file is automatically closed)
bool do_load() {
// open the file
ifstream fs( "profile.txt" );
if ( !fs )
return false; // failed to open the file
// attempt to read a sequence of space separated integers,
// the number of items of each item type the player had
for ( int idx = 0; idx < item_type_count; ++idx ) {
fs >> items[idx]; // attempt to read an integer
if ( !fs )
return false; // read failed
}
return true;
} // (file automatically closed)
void test_function() {
items[item_pickaxe]++; // read: the player acquires one pickaxe
if ( items[item_potion] > 0 ) { // read: if the player has potions
items[item_potion]--; // read: consume a potion
player_hp += 13;
}
}
Also, it seems you want support for different profiles. You can use
stringstreams to build
strings to configure which file to load/save from:
bool do_load( int profile_number ) {
ostringstream ss;
ss << "profile" << profile_number << ".txt";
string filename = ss.str();
ifstream fs( filename.c_str() );
if ( !fs ) {
cout << "Unable to open file \"" << filename << "\" for reading." << endl;
return false;
}
// ...
return true;
}