Thread: y wont this work this way

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    y wont this work this way

    ok this isnt my code its my cuz that im tryin to understand and make simpler....

    i wanna know i have to do
    molecular check;
    and molecular temp;

    Code:
    unsigned storeData(ifstream & ifs,vector <molecular>& m) 
    { 
    	molecular check; //temporary to read in files
    	string name; 
    	string formula; 
    	unsigned max(0);
    	char ch;
    	
    	while(ifs>>name>> ch >> check.carbon >> ch >> check.hydrogen)  
    	{ 
    		molecular temp; 
    		unsigned c = check.carbon;
    		unsigned h = check.hydrogen;
    
    		if(!exists(m,c,h))//if this is a new formula 
    		{ 
    			temp.names.push_back(name); 
    			temp.carbon=c; 
    			temp.hydrogen=h; 
    			m.push_back(temp);//create this molecule and push it into the vector 
    				if(c>max) 
    					max=c; 
    		} 
    		
    		else//check if this name already exists and if it doesnt push it back 
    		{   //on the names vector 
    			for(size_t i=0;i<m.size();i++) 
    				if(m[i].carbon==c && m[i].hydrogen==h && !nameExists(m,name,c,h)) 
    				{ 
    					m[i].names.push_back(name); 
    				} 
    		} 
    	} 
    	
    	return max; 
    }

    if i put just molecular temp here
    Code:
    		molecular temp; 
    	string name; 
    	string formula; 
    	unsigned max(0);
    	char ch;
    	
    	while(ifs>>name>> ch >> temp.carbon >> ch >> temp.hydrogen)  
    	{
    and run the program it gives me wrong output...
    any1 know y i have to do molecular check and temp and not just do one temp outside the loop
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Try asking the question like an intelligent human being and not some "y r u supid"
    2) Why don't you ask the person who wrote it what the purpose and logic is?

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    b/c like i said im tryin to make it simpler and im changing something more complex that he did. ive already deleted and changed da code he created...
    and if u want to come off sarcastic and not help then dont post and move on
    Last edited by gamer4life687; 09-14-2005 at 08:53 PM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    But they can explain their logic to you and you can then find a better method if one exists. Remember though that shorter is not always simpler. Now of course you didn't give us the full function that you changed so there is no real way for us to make sure you didn't forget to change something else.

    And I'm happy being sarcastic right here, its comfy

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    i kno the purpose of the prog and everything and his program works perfectly fine... we both had 2 do the same prog but we did it in class and we did it differently.. so in this persons code they used atoi() 2 find convert the string which the school doesnt want and i changed it .... just my problem was y do i have 2 create to temporary
    molecular check;
    and mocleculary temp;

    instead of just doing

    Code:
    unsigned storeData(ifstream & ifs,vector <molecular>& m) 
    { 
    	molecular temp;
    	string name;
    	unsigned max(0);
    	char ch;
    	
    	while(ifs>>name>> ch >> temp.carbon >> ch >> temp.hydrogen)  
    	{ 
    		unsigned c = temp.carbon;
    		unsigned h = temp.hydrogen;
    
    		if(!exists(m,c,h))//if this is a new formula 
    		{ 
    			temp.names.push_back(name); 
    			temp.carbon=c; 
    			temp.hydrogen=h; 
    			m.push_back(temp);//create this molecule and push it into the vector 
    				if(c>max) 
    					max=c; 
    		} 
    		
    		else//check if this name already exists and if it doesnt push it back 
    		{   //on the names vector 
    			for(size_t i=0;i<m.size();i++) 
    				if(m[i].carbon==c && m[i].hydrogen==h && !nameExists(m,name,c,h)) 
    				{ 
    					m[i].names.push_back(name); 
    				} 
    		} 
    	} 
    	
    	return max; 
    }
    it seems wierd to me that because i declare temp outside the loop it suddenly gives me wrong output.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    4get it ... i realized i had to add this ....
    temp.names.clear();
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  7. #7
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by gamer4life687
    4get it ... i realized i had to add this ....
    temp.names.clear();
    Yeah, because of !exists(m,c,h), would consider the old values in temp, because moving it outside the while statement would prevent it from resetting every time, right? wrong? hmm.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    exactly right and thats the reason my cousin converted the string to an int im guessing. from the looks of it he couldnt figure out how to read each individual part and clear the names... but thx its working now
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM