Thread: For loop question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    Red face For loop question

    Ok! I have this code as follows:

    Code:
    int iConParam = 0;
    
    for ( iConParam = 0; iConParam < vecContractParam.length() ; ++iConParam )
    {
    ...
    ...
    ..
    ..
    if (meet my condition)
    {
    ++iConParam
    }
    if (iConParam = vecContractParam.length()+1)
    {
    Generate my output;
    }
    }

    As notice, if I place ++iConParam in my for loop, it will not generate the output that I need. How am I suppose to increase the length of vecContractParam.length() in my for loop?

    The code above is a rough idea of what I am trying to do. Is there any better way to code it instead of using for loop?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this C++ code? If so, vecContractParam.length() is the length of the vector, and if iConParam is used to index into the vector, it should not ever be iConParam == length()+1, since that would be TWO steps beyond the valid range.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    So how am I suppose to do that?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    First, answer matsp's question: is this C++ code? If it is, what is vecContractParam?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Is a C code and vecContractParam is the records of my vector

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... in that case vecContractParam.length() is probably wrong. You should be writing vecContractParam.length instead since length is a member variable of your vector. You might want to indent your code better as well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    iConParam == length - 1

    And why do you initialize and define iConParam at nearly the same time? It won't matter, but you don't need it.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Quote Originally Posted by Brafil View Post
    iConParam == length() - 1

    Sorry what u meant is it as follows?

    Code:
    int iConParam = 0;
    
    for ( iConParam = 0; iConParam < vecContractParam.length() ; ++iConParam )
    {
    ...
    ...
    ..
    ..
    if (meet my condition)
    {
    iConParam == vecContractParam.length() - 1
    }
    if (iConParam = vecContractParam.length()+1)
    {
    Generate my output;
    }
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    JuzMe, please post the smallest and simplest compilable program that demonstrates your problem. Stop leaving out code with "...". I am reasonably certain that you should be having compile errors if this is really C, unless you have written your program such that it does the wrong thing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Sorry, I meant single '='. And yes, please post your code. It'd be much easier.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Here is my code

    Code:
    void bghCalculatedValues::getBillDisplayParam(
    bghVantiveDescription &newVantiveDesc,
    bghVecContractParameters &vecContractParam,
    bghContract& contract)   	
    {
    	#ifdef DEBUGADONIS
            cout << "Hera newVantiveDesc.PackageMemberID: " << newVantiveDesc.PackageMemberID<< endl ;
    	 cout << "Hera partnumber: " <<newVantiveDesc.PartNumber<< endl ;
    	 cout << "Hera ParamDetails: " << ParamDetails<< endl ;
    	 cout << "Hera ParamDetails.exists: " << ParamDetails.exists (newVantiveDesc.PartNumber )<< endl ;
    #endif
       jTime tmpBSDate = Invoice.Date(PERIODSTART);
       jTime tmpBEDate = Invoice.Date(PERIODEND);
       //herahoe not required
       if ( ParamDetails.exists (newVantiveDesc.PartNumber))//herahoe change newVantiveDesc.PackageMemberID to  newVantiveDesc.PartNumber
       {
    #ifdef DEBUGADONIS
            cout << "Adding the package member id" << newVantiveDesc.PackageMemberID<< endl ;
    cout << "Hera Adding the part number: " << newVantiveDesc.PartNumber<< endl ;
    #endif
            billparamvec &ParamRef =  ParamDetails(newVantiveDesc.PartNumber);//herahoe change newVantiveDesc.PackageMemberID to  newVantiveDesc.PartNumber
            for (unsigned int i=0; i<ParamRef.billParamVector.length() ; i++)//herahoe ParamRef.billParamVector.length() to vecContractParam.length()
            {
    #ifdef DEBUGADONIS
               cout << "Adding param vector :" << newVantiveDesc.PackageMemberID<< endl ;
               cout <<"LABEL "<<ParamRef.billParamVector[i].paramEntryLabel<<endl;
    		    cout <<"Hera at label vecContractParam.length() "<<vecContractParam.length()<<endl;
    			cout <<"Hera ParamRef.billParamVector.length(): "<<ParamRef.billParamVector.length()<<endl;
    #endif
    
    	    int iConParam = 0;
    	    bool displayonce = false;
    		bool enterhere = false;
    		int paramCheck = 0;
    	    char buff[100];
       //     paramCheck =  vecContractParam.length() + 5;
    	    for ( iConParam = 0; iConParam < vecContractParam.length() ; iConParam++ )
    	    {
    
    			#ifdef DEBUGADONIS
               cout << "hera iConParam :" << iConParam<< endl ;
    			cout << "hera2 vecContractParam.length() :" << vecContractParam.length()<< endl ;
    			#endif
    		if ( displayonce )
    		{
    		    break;
    		}
    			#ifdef DEBUGADONIS
               cout << "hera ParamRef.billParamVector[i].paramType :" << ParamRef.billParamVector[i].paramType<< endl ;
    			cout << "hera newVantiveDesc.PartNumber :" << newVantiveDesc.PartNumber<< endl ;
    			#endif
    				//jString::intValue(ParamRef.billParamVector[i].paramType)
    	//	if ( jString::intValue(ParamRef.billParamVector[i].paramType) == newVantiveDesc.PartNumber) //herahoe change ParamRef.billParamVector[i].paramType == vecContractParam[iConParam].paramType to vecContractParam[iConParam].paramType == newVantiveDesc.PartNumber
    		//{herahoe remove it
    #ifdef DEBUGADONIS
               cout << "hera param comes here " << endl ;
    			#endif
    		    if (( vecContractParam[iConParam].EndDate >= tmpBSDate ) )
    		    {
    							#ifdef DEBUGADONIS
               cout << "hera vecContractParam[iConParam].EndDate :" << vecContractParam[iConParam].EndDate<< endl ;
    			cout << "hera tmpBSDate :" << tmpBSDate<< endl ;
    			#endif
    /*Vengat for WR00939 */
                            if ( vecContractParam[iConParam].paramValue  == DEFNONEVALUE )
                            {
                                break;
                            }
    /*Vengat for WR00939 */
    
    			if ( ParamRef.billParamVector[i].DisplayFlag == 0 )
    			{
    			    displayonce = true;	
    			}
                   
    			
    			vecContractParam[iConParam].bParamDetail = &ParamRef.billParamVector[i];
    						#ifdef DEBUGADONIS
               cout << "hera param start date :" << (const char *) vecContractParam[iConParam].StartDate.format("%Y%m%d")<< endl ;
    			cout << "hera param end date :" << (const char *) vecContractParam[iConParam].EndDate.format("%Y%m%d")<< endl ;
    			cout << "hera param type :" << (const char *) vecContractParam[iConParam].paramType << " Label: "<<ParamRef.billParamVector[i].paramEntryLabel <<" Partnumber: "<< newVantiveDesc.PartNumber<< endl ;
    			#endif
    			/*herahoe start add (const char *)*/sprintf ( buff, "%05d%010d%s%s", ParamRef.billParamVector[i].paramBillSequence , (const char *) vecContractParam[iConParam].paramType , (const char *) vecContractParam[iConParam].StartDate.format("%Y%m%d"),  (const char *) vecContractParam[iConParam].EndDate.format("%Y%m%d") );
    			//herahoe add
    			//partnumber
                const char *paramTypeP = strrchr((const char *) vecContractParam[iConParam].paramType, '+'); //get partnumber value from last + sign onwards
                jString paramTypeP2 = paramTypeP;
    			jStringV newParamTypeP = paramTypeP2.split('+');
    			//partnumber end
    			//label
               jString newParamTypeL = (const char *)vecContractParam[iConParam].paramType;
               jStringV ParamTypeL = newParamTypeL.split(paramTypeP);
    	 #ifdef DEBUGADONIS
    						 cout << "Hera not in paramTypeP: "<<paramTypeP<< endl;
    					  cout << "Hera not in newParamTypeP: "<<newParamTypeP[1]<< endl;//partnumber
    					  cout << "Hera not in ParamTypeL: "<<ParamTypeL[0]<< endl;//Label
                          cout << "Hera contract.vecContractParam.exists(jString(buff)): "<<contract.vecContractParam.exists(jString(buff))<< endl;//Label
    #endif
    
    			if ( !contract.vecContractParam.exists(jString(buff)) && (ParamRef.billParamVector[i].paramEntryLabel == ParamTypeL[0] && newVantiveDesc.PartNumber == newParamTypeP[1]) )
    			{
    			  contract.vecContractParam.add(jString(buff), vecContractParam[iConParam]);	
    
    			}
    				else if(!contract.vecContractParam.exists(jString(buff)) && !(ParamRef.billParamVector[i].paramEntryLabel == ParamTypeL[0] && newVantiveDesc.PartNumber == newParamTypeP[1]))
    					{
    
    				  if (iConParam > 0 ) 
    					{
    						++iConParam;
    						enterhere = true;
    						 #ifdef DEBUGADONIS
    						 cout << "iConParam: "<<iConParam<< endl;
    						#endif
    					}
    					
    					}
    					else
    					{
    						break;	
    					}	
    		    }
    	 	
    	    }
            }
        }
        return;
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JuzMe
    Here is my code
    You need to stop programming and revise your instructional material right now. That is C++, not C. If you do not even know what language you are learning...

    Anyway, moved to C++ programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

Tags for this Thread