Code Crash!!!

This is a discussion on Code Crash!!! within the C++ Programming forums, part of the General Programming Boards category; Hi, I've written a code which replaces characters in a string with their respective URLEncode (i.e a " " will ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Code Crash!!!

    Hi,
    I've written a code which replaces characters in a string with their respective URLEncode (i.e a " " will be replaced by "+" etc..).
    However the code crashes at execution.
    Please help!!
    Here's the code extract;
    Code:
    --------------------------------------------------------------------------------
    for(j=0;j<one.length();j++)
    {

    indxD = one.find_first_of ( " " , 0 );
    one.replace(indxD,1,"%2B",3);
    }
    ---------------------------------------------------------------------------------

    Thank You In Anticipation,
    John

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Why this was doing on the contest board is beyond me!

    MOVED!

  3. #3
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    uhhmm...maybe you should paste some more code?
    It's kinda hard to tell what goes wrong from what you've shown us...

    /btq

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    To have a stab at it;
    Code:
    #include <iostream>
    #include <string>
    
    
    int main(){
    
    	using std::cout;
    	using std::endl;
    	using std::string;
    
    	string one("Hello World This Is Encoded");//string to encode
    	const string rep("%2B");//replacement token		   
    	string::size_type indxD = 0;//return of find...	
    				
    	std::cout << "Unencoded " << one << std::endl;
    
    	while(1){
    		indxD = one.find_first_of (' ',0);//find a char....
    		if(indxD == string::npos)break;//if -1 then end (not found!)
    		else one.replace(indxD,1,rep.c_str(),rep.length());//replace with token
    	}
    
    	std::cout << "Encoded " << one << std::endl;
    	
    
    }

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I don't think there is something wrong with the loop, maybe it crashes from something else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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