1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include "includes.h"
bool load( weapons w[] )
{
// ifstream defaults to std::ios::in, but I use it there anyway
std::ifstream iFile( "weapons.txt", std::ios::in );
// if the file is NOT open
if( ! iFile.is_open() )
{
std::cout << "Unable to open file...\n";
// return, file error
return false;
}
std::string input;
std::stringstream ss;
int index = 0;
// while it's NOT the end of the file
while( ! iFile.eof() )
{
std::getline( iFile, input, ' ' );
//
// check to see if the line has a comment
//
if( input.substr( 0, 1 ) == "/" )
{
// if the current line has a "/" at the start
// get the whole line and restart the loop
std::getline( iFile, input, '\n' );
continue;
}
// set the item name
w[ index ].item = input;
// get description
std::getline( iFile, input, ' ' );
w[ index ].description = input;
// remove the underscores from the description
for( unsigned int i = 0; i < w[ index ].description.size(); ++i )
{
// if un underscore is found, at the same element in the string,
// set it to a space ' '
if( w[ index ].description[ i ] == '_' )
w[ index ].description[ i ] = ' ';
}
// get rarity
std::getline( iFile, input, ' ' );
w[ index ].rarity = input;
// get minimum damage
std::getline( iFile, input, ' ' );
// clear, add input to ss, stream to int
ss.clear();
ss << input;
ss >> w[ index ].MIN_DMG;
// get max damage
std::getline( iFile, input, ' ' );
// clear, add input to ss, stream to int
ss.clear();
ss << input;
ss >> w[ index ].MAX_DMG;
// get coin type
std::getline( iFile, input, ' ' );
w[ index ].coinType = input;
// get coin amount. Last variable, line end( '\n' )
std::getline( iFile, input, '\n' );
ss.clear();
ss << input;
ss >> w[ index ].coinAmount;
// move to the next index
++index;
}
// exit after the file has finished being read
return true;
}
|