Thread: need a little help to finish

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    31

    need a little help to finish

    hi i just need a little help because my program returns an invalid error and i need help to make it valid.

    this code is mostly my own but with bits and pieces from else where as you can probly tell by my code i'm a begginer
    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
    i get the invalid when it couts
    Code:
    cout << s.name << " "<< *s.xpneeded<< endl;
    any ideas? thankyou in advance

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    cout << s.name << " "<< s.xpneeded<< endl;
    There you go.
    Last edited by CodeMonkey; 01-14-2007 at 11:38 PM. Reason: I was wrong.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    -- Also, it doesn't seem that you need to be using a pointer to an fstream object. Just use the object. You forget to delete it, anyway.
    -- You have included <string>, so go ahead and use them everywhere you now use char*.
    --Avoid 'using namespace std;'. Specify more specific frequently-used things instead: 'using std::cout;' , 'using std::endl'
    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();
    std::string does all of the template magic on basic_string<> for you. Use it unless a teacher tells you not to.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    31
    its times like this i feel the urge to smash myself in the head

    thankyou very much it worked a treat lol

    maybe i spoke to soon it now compiles but doesnt generate what i expected

    what i need it to do is puedocoded
    Code:
    read input from txt file
    process this and construct a few objects
    output data from the objects in a sorted order
    but it just shows gibberish
    Last edited by stien; 01-14-2007 at 11:39 PM.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    31
    ahhh u found my secrets lol

    by the way im self teaching form a book designed for dummies...
    well i guess im just plain stupid

    im sorry but i dont know how to use pointers or not use them rather.

    Code:
    Runecrafting,	62	340027
    Mining,	        63	368848
    Hunting,	63	368680
    Construction,	63	368665
    Smithing,	68	629248
    Farming,	64	409597
    Fishing,	65	450846
    Agility,	65	450514
    Slayer,		65      449505
    Theiving,	65	449492
    Magic,	        77	1581253
    Herblore,	77	1573878
    Crafting,	67	548036
    Prayer,	        72	899331
    this is the txt file i was using perhaps it might help u help me?
    so can i just get rid of the * of the ifstream?

    i get scared of using <string> becasue string dosnt go bold, dont know why its included must of had a moment of madness
    Last edited by stien; 01-15-2007 at 12:40 AM.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    std::string is not a language feature, that is why it is not bold. However, it is standard, optimal, and rather useful.
    As for the fstream: no, don't just remove the *
    Code:
    ifstream pFileStream("Accounts.txt");  //might end up changing name, but we'll keep it for now
        if (!pFileStream)     //if failbit, eofbit or badbit is set (that is, if opening the file failed, or worse)
        {
            cout << "Can't open Accounts.txt" << endl;
            return 0;
        }
    
    //. . . . . . . Later . . . . . . . 
    
    pFileStream.getline(buffer,256);  //use . instead of ->
    Uh oh. Look at this.
    Code:
     // associate an istrstream object with the input
        // character string
        istringstream inp(pString);
    Streams do not associate. Buffers do. What the code above is doing is calling std::string(pString) and using that as the initial value for sstream's internal string. Your argument is not modified one bit. Your code needs an overhaul. Either look into <cstrstream>, streambufs, or use std::string!
    Code:
    bool parseString(const std::string & pString, std::string & pName, int& clvl, int& cxp)
    {
        // associate an istrstream object with the input
        // character string
        std::istringstream inp(pString);
    
        // read up to the comma separator
        std::getline(inp, pName, ',');   //overloaded getline() for streams and strings
           //By the way, looking at you input file, you could simply get rid of the ',' after every name and use >> all the time.
        
        // now the account number and balance
        inp >> clvl >> cxp;
    
        // return the error status of the stream
        return inp;  //I think streams have operator bool()
    }
    Hopefully that's the right idea (somebody tell us if I'm wrong). Happy coding.

    *edit* You COULD use pubsetbuf(char*,streamsize) to use streams as you have been, but I would highly encourage std::string.
    Last edited by CodeMonkey; 01-15-2007 at 01:49 AM. Reason: addition
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    31
    hey thank you for the advice im gonna have a look into the three string things youve told me about and yes my code does need recoding
    this is just the first prototype hopfully i can make it into a proper program soon.
    im sure if i read up on std::string <cstrstream> and streambuffs i will be able to make this happen.

    if i could ask your advice on how i should write this program.

    what i need it to do is;
    Code:
    get and store data from a file (should i use a class)
    process the data and sort it (should i use a list of class)
    output to a new file  ( the output will use different members than the input if i use classes) 
    (ive tried the above and the output is messy 
    but im guessing im using the wrong tools or have used them wrong)
    i will be able to make a mockup version of whatever you suggest.

    edit[this code only uses a class and variables i made and was originally 2 different programs from a book i cut and pasted it together and edited a few pieces if u want i can post these programs so you can get the jist of what i needed?]

    sorry to keep editing but i think my main problem is the list<skill> i really have no idea why i needed
    Code:
    bool operator<(skill& s1, skill& s2)
        {
            return s1.xpneeded < s2.xpneeded;
        }
    or how this makes this work or not work how ever you see it
    Code:
         skills.push_back(*new skill(name,lvl,xp));
           skills.sort();
    llist<skill>::iterator iter = skills.begin();
    
    
    skill& s = *iter;
    cout << s.name << " "<< s.xp<< endl;
    iter++;
    Last edited by stien; 01-15-2007 at 11:53 AM.

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    skills.push_back(*new skill(name,lvl,xp));
    skills.sort();
    list<skill>::iterator iter = skills.begin();
    
    skill& s = *iter;
    cout << s.name << " "<< s.xp<< endl;
    iter++;
    Alright. Let's take a look.
    Code:
    skills.push_back(*new skill(name,lvl,xp));
    This creates a new entry in skills. 'new skill()' dynamically allocates space for a skill and constructs a skill in that space. You pass the value at the address returned by 'new' into push_back, whcih makes a copy of that and puts it in the container. So where's the problem? You never cleanup the memory. Just pass a constructor instead.
    Code:
    skills.push_back(skill(name,lvl,xp)); //This will create a temporary object for push_back() to copy, and then destroy the temp after the function call
    ......
    Code:
    skills.sort();
    Your comparison operator compares xpneeded -- that's "how this makes this work or not work." sort() selects an ideal sorting algorithm for your container, and then applies the algorithm using the < (less than) operator. So, you either have to use the language-default <, overload your own (as you did), or use a different version of sort that takes a comparison function as an argument.
    .......
    Code:
    list<skill>::iterator iter = skills.begin();   //self explanatory
    do {                                       //I think you want a loop here
    skill& s = *iter;                           //refer to entry directly through reference (if that's your style)
    cout << s.name << " "<< s.xp<< endl;      //keep in mind you could use the iterator's operator -> and do away with the reference altogether.
    }while(++iter != skills.end());            //while the next place is not beyond the last value
    As for your design, keep trying new ideas, remembering that your concept is pretty simple, and so should the code. Post code that is problematic or that you don't understand and tell us why.
    Last edited by CodeMonkey; 01-15-2007 at 02:16 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    31
    ok ill take that into account and i will start a whole new program from scratch instead of using the code from my book
    im not sure how i would cleanup memory at this time but ill look into doing so thank you again for you timr and help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I never Finish my programs :(
    By epidemic in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2007, 12:35 PM
  2. force finish
    By PUI in forum C Programming
    Replies: 1
    Last Post: 10-07-2006, 09:42 AM
  3. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  4. Finish my DATABASE using C
    By joydeep1 in forum C Programming
    Replies: 4
    Last Post: 07-06-2006, 12:47 AM
  5. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM