Ugh... somethings wrong - messing up list.

This is a discussion on Ugh... somethings wrong - messing up list. within the C++ Programming forums, part of the General Programming Boards category; Okay so i'm writing this program to keep track of snakes for breeders and large-scale collectors. The problem is that ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Exclamation Ugh... somethings wrong - messing up list.

    Okay so i'm writing this program to keep track of snakes for breeders and large-scale collectors.

    The problem is that for some reason it's flip-flopping the ID numbers and the Names.

    Here are the functions and code chunk which uses this function:

    This function loads the entire list into 2 vectors:
    Code:
    void loadSnakeList(vector<string> &ids, vector<string> &names)
    {
    	ifstream fin("snakes/main_list.txt");
    	vector<string> v;
    	string temp;
    	while(getline(fin, temp))
    	{
    		v.push_back(temp);
    	}
    	fin.close();
    	int size = v.size();
    	string tname, tnum;
    	bool flag = false;
    	for(int i=0; i<size; i++)
    	{
    		for(int j=0; j<v.at(i).size(); j++)
    		{
    			if(v.at(i).at(j) == '-')
    			{
    				flag = true;
    			}
    			else if(flag == false)
    			{
    				tname.push_back(v.at(i).at(j));
    			}
    			else if(flag == true)
    			{
    				tnum.push_back(v.at(i).at(j));
    			}
    		}
    		ids.push_back(tnum);
    		names.push_back(tname);
    		flag = false;
    		tnum.clear();
    		tname.clear();
    	}
    }
    This one appears to be working correctly.

    Here's the Function which adds to the list:
    Code:
    void addSnakeList(snake *save)
    {
    	ofstream fout("snakes/main_list.txt", ios::app);
    	fout<<(save->nicName)<<"-"<<(save->idNumber)<<endl;
    	fout.close();
    }
    Also appears to be working fine.

    Here's where i think the problem lies, cause everything works until you run this function and then it gets flipped:
    Code:
    void deleteSnakeFile(string file)
    {
    	string rem= "/snakes/"+file+".dea";
    	remove(rem.c_str());
    	//remove from list
    	vector<string> rnames;
    	vector<string> rums;
    	loadSnakeList(rums, rnames);
    	//Search
    	int pos;
    	for(int i=0; i < rums.size(); i++)
    	{
    		if(rums.at(i) == file)
    		{
    			pos = i;
    		}
    	}
    	//Rewrite file
    	ofstream fout2("snakes/main_list.txt");
    	for(int i=0; i<rums.size(); i++)
    	{
    		if(i != pos)
    		{
    			fout2<<rums.at(i)<<"-"<<rnames.at(i)<<"\n";
    		}
    	}
    	fout2.close();
    	//Done
    }
    This is where i believe the problem lies.

    Here's where the information is displayed:
    Code:
    ...
    clr();
    					setColor(BLUE, WHITE, h);
    					cout<<"                                                                                ";
    					cout<<"                                    ANIMALS                                     ";
    					cout<<"                                                                                ";
    					cout<<endl;
    					setFontColor(NORMAL, h);
    					//CONTENT HERE!!!!!!*****
    					vector<string> ids;
    					vector<string> names;
    					loadSnakeList(ids, names);
    					for(int i=0; i<ids.size(); i++)
    					{
    						setFontColor(WHITE, h);
    						cout<<"     "<<ids.at(i);
    						setFontColor(NORMAL, h);
    						cout<<"  ->  ";
    						setFontColor(GREEN, h);
    						cout<<names.at(i)<<endl<<endl;
    					}
    ...
    I dont think the problem's here either.



    Please help!

    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> fout2<<rums.at(i)<<"-"<<rnames.at(i)<<"\n";
    There you are writing the names out second, but earlier you were doing the names first. Isn't that a problem?

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    yes and i tried switching it but still things get flip flopped
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    void loadSnakeList(vector<string> &ids, vector<string> &names)
    {
        ifstream fin("snakes/main_list.txt");
        vector<string> v;
        string temp;
        while(getline(fin, temp))
        {
            v.push_back(temp);
        }
        fin.close();
        int size = v.size();
        string tname, tnum;
        bool flag = false;
        for(int i=0; i<size; i++)
        {
            for(int j=0; j<v.at(i).size(); j++)
            {
                if(v.at(i).at(j) == '-')
                {
                    flag = true;
                }
                else if(flag == false)
                {
                    tname.push_back(v.at(i).at(j));
                }
                else if(flag == true)
                {
                    tnum.push_back(v.at(i).at(j));
                }
            }
            ids.push_back(tnum);
            names.push_back(tname);
            flag = false;
            tnum.clear();
            tname.clear();
        }
    }
    You don't need a vector to store the strings from the file into. Simply read a string and process it instead. Also, use the string's find and substr member functions to find the '-' character and create the substrings which you'll store into the ids and names vectors. That whole function can be reduced to this:
    Code:
    void loadSnakeList(vector<string> &ids, vector<string> &names)
    {
        ifstream fin("snakes/main_list.txt");
        string temp;
    
        while(getline(fin, temp))
        {
            string::size_type index = temp.find('-');
            if( index != string::npos )
            {
                ids.push_back( temp.substr(0,index) );
                names.push_back( temp.substr(index+1) );
            }
        }
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thank you! I'll give that a try.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21