Thread: problem using ofstream

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

    problem using ofstream

    hi just wondering if any body could help me i have made a list of objects but i did this manually and not through a loop then i sort the list and try to ouput to a file it works fine except the last 3 items dont contain the right info.
    sorrry it is messy and longer than it needs to be but i can sort that out after i fix the output. ive also put the file for input and the results in the next post

    if you could help id really appreciate it thankyou in advance
    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:
        int currentxp;
        int currentlvl;
        int nextlvl;
        int xpforlvl;
        int xpneeded;
        string name;
      
        
         
    };
    
     bool operator<(skill& s1, skill& s2)
        {
            return s1.xpneeded < s2.xpneeded;
        }  
        
    
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    int clvl =0;
    int cxp =0;
    string named;
    list<skill> skills;
    //create a file stream and use it to extract data from a text file
    //store the data into the relavant class objects
    
    ifstream fin("accounts.txt");
    
    return 0;
    }
    edited to make easy reaing the other posts
    Last edited by stien; 01-18-2007 at 11:58 AM.

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    31
    this is the input file:

    Code:
    Agility	65	450514
    Attack	78	1638569
    Construction	63	368665
    Cooking	74	1151417
    Crafting	67	548040
    Defence	75	1225165
    Farming	64	409597
    Firemaking	85	3369802
    Fishing	65	450846
    Fletching	85	3467009
    Herblore	77	1573878
    Hitpoints	78	1629904
    Hunting	63	368680
    Magic	78	1629296
    Mining	63	368848
    Prayer	72	899331
    Ranged	77	1489605
    Runecrafting	62	344416
    Slayer	65	449505
    Smithing	69	668,138
    Strength	74	1029484
    Theiving	65	449492
    Woodcutting	76	1353132

    and this is the output file:
    Code:
    Cooking 
    Runecrafting 62599
    Mining 80580
    Hunting 80748
    Construction 80763
    Farming 86657
    Fishing 97107
    Agility 97439
    Slayer 98448
    Crafting 120011
    Cooking 185026
    Prayer 196947
    Herblore 224930
    Defence 250416
    Ranged 309203
    Attack 347499
    Hitpoints 356164
    Magic 356772
    Fletching 505285
    Firemaking 602492
    Smithing 813777    // it starts to go wrong here
    ,138 813777
    ,138 813777
    ,138 813777

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Smithing 69 668,138
    That line is from the inpute file. The comma is probably throwing everything off.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    31
    thank you i cant belive i did that u guys are great thanks

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This code seems to cry for some loops? You are using a list, but you construct each element manually with a different name, and then push it back. No wonder things go wrong if you have that much code instead of a few lines.

    By the way, I can't see where you have defined agility, woodcutting etc (which you don't need at all - you could use a single skill temp;, assign desired values and push it onto the list, then repeat).

    Edit: OK, saw the definitions now. But anyway, most of the code you have here could be done by

    Code:
    list<skill> skills;
    skill temp;
    ifstream fin("somefile");
    while (fin >> temp.name >> temp.currentlvl >> temp.currentxp) {
        temp.nextlvl = temp.currentlvl + 1; 
    //??, should probably be a simple method void skill::next_level()
        temp.xpforlvl = xparray[temp.currentlvl];
        temp.xpneeded = temp.xpforlvl - temp.currentexp;
        skills.push_back(temp);
    }
    Last edited by anon; 01-18-2007 at 12:12 PM.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    31
    i know but my main problem was making it all work first

    because im quite new to c++ the code i used before did have those loops but it didnt work so now i can edit my old program and it should work

    the reason i declared them all then pushed em back is so i could single step through to make sure it all worked right

    the code was very long so i deleted off the post so to make it easier to read the rest of the posts

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    because im quite new to c++ the code i used before did have those loops but it didnt work so now i can edit my old program and it should work
    Alright. Now you have probably learnt that "Unrolling" loops is absolutely useless as a debugging strategy .

    In this case, since the data you read from the file didn't turn out right, checking if the file itself is OK should be the first thing to do. (You could also have it print all data after each reading operation to verify it was read correctly.)

    Good luck with the game!

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    31
    hehe ty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM