Results 1 to 5 of 5
  1. #1
    yodaliketaco's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    winsock.dll
    Posts
    645
    Reputation
    45
    Thanks
    514
    My Mood
    Tired

    Runtime error with space character removing function

    This function is causing the program to crash, but I can't spot my error.

    Code:
    string removeSpaces (string input)
    {
    	string output;
    	int n;
    	int o = 0;
    	for ( n = 0; n < (int)input.length(); n++ )
    	{
    		if (input[n] != ' ')
    		{
    			output[o] = input[n];
    			o++;
    		}
    	}
    	return output;
    }
    Any ideas for what is wrong with the function?

  2. #2
    .::SCHiM::.'s Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    733
    Reputation
    180
    Thanks
    880
    My Mood
    Twisted
    Quote Originally Posted by yodaliketaco View Post
    This function is causing the program to crash, but I can't spot my error.

    Code:
    string removeSpaces (string input)
    {
    	string output;
    	int n;
    	int o = 0;
    	for ( n = 0; n < (int)input.length(); n++ )
    	{
    		if (input[n] != ' ')
    		{
    			output[o] = input[n];
    			o++;
    		}
    	}
    	return output;
    }
    Any ideas for what is wrong with the function?
    There are a few things which could be causing this, first you can't use strings like this:
    Code:
    output[o] = input[n];
    Use the member string::at, like this:

    Code:
    if(foo.at(bar) == 'foobar'){
    
    foo.at(bar) = foobar.at(bar);
    
    }
    And why are you manually checking for spaces? The string class has a member (find) who searches and returns the position of a character or string inside the string.
    Code:
    if( foo.find(' ') != std::string::npos ){   // if it's found
    
    foo.at( foo.find(' ') ) = '_';    // replace a space with an underscore
    
    }

    I'm SCHiM

    Morals derive from the instinct to survive. Moral behavior is survival behavior above the individual level.

    Polymorphic engine
    Interprocess callback class
    SIN
    Infinite-precision arithmetic
    Hooking dynamic linkage
    (sloppy)Kernel mode Disassembler!!!

    Semi debugger




  3. #3
    mmbob's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    ja
    Posts
    653
    Reputation
    70
    Thanks
    1,157
    My Mood
    Bitchy
    Quote Originally Posted by .::SCHiM::. View Post
    There are a few things which could be causing this, first you can't use strings like this:
    Code:
    output[o] = input[n];
    Use the member string::at, like this:

    Code:
    if(foo.at(bar) == 'foobar'){
    
    foo.at(bar) = foobar.at(bar);
    
    }
    Using string[x] is the exact same as using string.at(x).

    Quote Originally Posted by .::SCHiM::. View Post
    And why are you manually checking for spaces? The string class has a member (find) who searches and returns the position of a character or string inside the string.
    Code:
    if( foo.find(' ') != std::string::npos ){   // if it's found
    
    foo.at( foo.find(' ') ) = '_';    // replace a space with an underscore
    
    }
    You can't easily remove characters from within the string, so he loops through the original string to copy the non-space characters over to the new string.

    @yodaliketaco
    The problem is probably that the output string has a length of 0, but you're trying to change characters past 0. A solution would be
    Code:
    if (input[n] != ' ')
    {
    	output.append(input[n]); // adds a character to the end of output
    }
    Last edited by mmbob; 07-11-2011 at 08:12 AM.

  4. The Following User Says Thank You to mmbob For This Useful Post:

    yodaliketaco (07-11-2011)

  5. #4
    yodaliketaco's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    winsock.dll
    Posts
    645
    Reputation
    45
    Thanks
    514
    My Mood
    Tired

    string::append removes the runtime error:

    Code:
    string removeSpaces (string input)
    {
    	string output;
    	int n;
    	int o = 0;
    	for ( n = 0; n < (int)input.length(); n++ )
    	{
    		if (input[n] != ' ')
    		{
    			output.append(input, o, 1);
    			o++;
    		}
    	}
    	return output;
    }

    But the primary function isn't fulfilling its purpose. I must have a logic error: (Yes I know stringstream solves most of this; I prefer to write my own functions, that way I'm better prepared when there isn't a class for what I want to do.)

    Here is the code:

    Code:
    //getNum returns integer value in base (base) of string input
    int getNum (string input, int base)
    {
    	input = removeSpaces(input);
    	int n;
    	int i = 0;
    	int total = 0;
    	int mult;
    	for ( n = 0; n < (int)input.length(); n++ )
    	{
    		if (numericChar(input[n], base) != NULL)
    		{
    			mult = numOfIntegers(input, base) - 1;
    			mult = mult - i;
    			mult = power((double)base, (double)mult);
    			mult = (mult * numericChar(input[n], base));
    			total = mult + total;
    			i++;
    		}
    	}
    	return total;
    }
    //power returns base^exponent as integer
    double power (double base, double exponent)
    {
    	if (exponent > (double)1) return base * (power(base, (exponent - 1)));
    	else if (exponent == (double)1) return base;
    	else if (exponent == (double)0) return (double)1;
    	else if (exponent < (double)0) return ( (double)1 / power(base, ((double)0 - exponent)));
    	return NULL;
    }
    //numOfIntegers returns number of integers in string input as integer
    int numOfIntegers (string input, int base)
    {
    	int n;
    	int total = 0;
    	for (n = 0; n < (int)input.length(); n++)
    	{
    		if (numericChar(input[n], base) != NULL) total++;
    	}
    	return total;
    }
    //removeSpaces returns string without spaces
    string removeSpaces (string input)
    {
    	string output;
    	int n;
    	int o = 0;
    	for ( n = 0; n < (int)input.length(); n++ )
    	{
    		if (input[n] != ' ')
    		{
    			output.append(input, o, 1);
    			o++;
    		}
    	}
    	return output;
    }
    //returns value of character up to base 16
    int numericChar (char input, int base)
    {
    	if (input == '0') return 0;
    	else if (input == '1') return 1;
    	if (base > 2)
    	{
    		if (input == '2') return 2;
    		if (base > 3)
    		{
    			if (input == '3') return 3;
    			if (base > 4)
    			{
    				if (input == '4') return 4;
    				if (base > 5)
    				{
    					if (input == '5') return 5;
    					if (base > 6)
    					{
    						if (input == '6') return 6;
    						if (base > 7)
    						{
    							if (input == '7') return 7;
    							if (base > 8)
    							{
    								if (input == '8') return 8;
    								if (base > 9)
    								{
    									if (input == '9') return 9;
    									if (base > 10)
    									{
    										if (input == 'A' || input == 'a') return 10;
    										if (base > 11)
    										{
    											if (input == 'B' || input == 'b') return 11;
    											if (base > 12)
    											{
    												if (input == 'C' || input == 'c') return 12;
    												if (base > 13)
    												{
    													if (input == 'D' || input == 'd') return 13;
    													if (base > 14)
    													{
    														if (input == 'E' || input == 'e') return 14;
    														if (base > 15)
    														{
    															if (input == 'F' || input == 'f') return 15;
    														}
    													}
    												}
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    	return NULL;
    }
    When spaces exist, it truncates the end of the string by a variable amount of characters. (Most of the time, either 1 or 2).
    The point of removing the spaces was that they were acting as delimiters, with the function only returning numeric characters before the first space.

  6. #5
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed
    wouldn't it be easier to do this with char* var; instead of a string type?

Similar Threads

  1. Help with some c++ runtime errors
    By hobosrock696 in forum C++/C Programming
    Replies: 8
    Last Post: 09-10-2010, 01:55 PM
  2. Runtime Error Kick, How?
    By lemonslap in forum WarRock - International Hacks
    Replies: 23
    Last Post: 07-09-2008, 05:11 PM
  3. Error With warrock
    By blazebomb55 in forum WarRock - International Hacks
    Replies: 1
    Last Post: 06-20-2008, 11:58 AM
  4. i get runtime error
    By daniel89gt in forum WarRock Korea Hacks
    Replies: 1
    Last Post: 02-13-2008, 07:20 PM
  5. Runtime Error
    By larstils in forum WarRock - International Hacks
    Replies: 2
    Last Post: 02-13-2008, 12:20 PM