Code:
// StringStream - read and parse the contents of a file
#include <fstream>
#include <sstream>
#include <iostream>
#include <list>
#include <string>
using namespace std;
int xparray[]=
{
83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833,2107,2411, 2746, 3115, 3523,
3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363,
14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224,
41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721 ,101333,
111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247886,
273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032,
668051, 737627, 814445, 899257, 992895, 1096278, 1210421, 1336443,
1475581, 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373,
3258594, 3597792, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253,
7195629, 7944614 , 8771558, 9684577 ,10692629 ,11805606 ,13034431};
class skill
{
public:
skill(char* pName,int lvl, int xp)
{
name = pName;
int clvl = lvl;
int cxp = xp;
int nlvl = lvl+1;
int xpforlvl = xparray[lvl];
int xpneeded = xpforlvl - xp;
}
char* name;
int clvl;
int cxp;
int nlvl;
int xpforlvl;
int xpneeded;
};
// parseAccountInfo - read a passed buffer as if it were
// an actual file - read the following
// format:
// name, account balance
// return true if all worked well
bool parseString(char* pString, char* pName, int arraySize,
int& clvl, int& cxp)
{
// associate an istrstream object with the input
// character string
istringstream inp(pString);
// read up to the comma separator
inp.getline(pName, arraySize, ',');
// now the account number
inp >> clvl;
// and the balance
inp >> cxp;
// return the error status
return !inp.fail();
}
list<skill>skills;
bool operator<(skill& s1, skill& s2)
{
return s1.xpneeded < s2.xpneeded;
}
int main(int nNumberofArgs, char* pszArgs[])
{
// get a file stream
ifstream* pFileStream = new ifstream("Accounts.txt");
if (!pFileStream->good())
{
cout << "Can't open Accounts.txt" << endl;
return 0;
}
// read a line out of file, parse it and display results
for(;;)
{
// add a divider
cout << "=============================" << endl;
// read a buffer
char buffer[256];
pFileStream->getline(buffer, 256);
if (pFileStream->fail())
{
break;
}
// parse the individual fields
char name[80];
int clvl;
int cxp;
bool result = parseString(buffer, name, 80,
clvl, cxp);
// output the result
cout << buffer << "\n";
if (result == false)
{
cout << "Error parsing string\n";
continue;
}
skills.push_back(*new skill(name,clvl,cxp));
//cout << "name = " << name << ","
// << "current lvl = " << clvl << ", "
// << "current xp = " << cxp << endl;
// put the fields back together in a different
// order (inserting the 'ends' makes sure the
// buffer is null terminated
//ostringstream out;
// out << name << ", "
// << clvl << " "
// << cxp << ends;
// output the result - istringstream also works with
// the string class but I have have been staying with
// character arrays until the discussion of the templates
//string oString = out.str();
// cout << oString << "\n" << endl;
}
skills.sort();
list<skill>::iterator iter = skills.begin();
while (iter != skills.end())
{
skill& s = *iter;
cout << s.name << " "<< *s.xpneeded<< endl;
iter++;
}
system("PAUSE");
return 0;
}
as you can see it dosent do what it says on the tin but ill edit those out later