Runtime error with space character removing function

Posts 1–5 of 5 · Page 1 of 1
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?
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

}
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
}

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.
wouldn't it be easier to do this with char* var; instead of a string type?
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us