Removing Punctuation and Spaces in an array

Hi. I am a C++ programming Newbie working on a homework (ahead of schedule, yes!!!) for my C++ intro class. I am using MS Visual C++ in Windows 7 for class but I use g++ in Ubuntu 12.04 at home.

My question is that I need to write a function that copies a string array to another string array while it removes the spaces and punctuation from a string array. I have the code written and compiled (MS Visual C++) but am getting an error code when running the program. After receiving input and outputting it back on the screen (see below), a dialog box opens which says "Debug Assertion Failed" and "Expression:(unsigned)(c + 1) <=256

I'm lost and sorry if this is a silly newbie mistake... I also have a screenshot of the dialog box if someone can tell me how to post it. Thanks in advance!!!


Enter some input: Hi Cplusplus forum
Before remove, contents of the array:
----------------------
Hi Cplusplus forum




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

const int Size = 50;

void InputString(char [], int);
void OutputString(char []);
void RemovePunct(char [], char []);

int main()
{
	char yourString[Size];
	char newString[Size];
	
	InputString(yourString, Size);
	cout << "Before remove, contents of the array:" << endl << "----------------------" << endl;
	OutputString(yourString);
	
	RemovePunct(yourString, newString);
	cout << "After removing punction characters, contents of the array:" << endl << "----------------------" << endl;
	OutputString(newString);

  return 0;
}


void InputString(char a[], int size)
{
	cout << "Enter some input: ";
	cin.getline(a, size);
	return;
}


void OutputString(char a[])
{
	cout << a << endl;
}

void RemovePunct(char old_String[], char new_String[])
{
	for (int i = 0; i != Size; i++)
	{
		if (isalnum(old_String[i]))//If current character is alpha-numeric
		{
			new_String[(i - number)] = old_String[i]; //Add the character to new_String[] at 'i - number'.
			int number = 0; 
			number = number + 1;
		}
	}
	return;
}
1
2
3
new_String[(i - number)] = old_String[i]; //Add the character to new_String[] at 'i - number'.
			int number = 0; 
			number = number + 1;

You're trying to use number before declaring it. Also note that if you declare number within the if statement, it will go out of scope and then be reinitialized to 0 each time the if statement is reached (i.e. every time the loop runs), so you may want to rethink where you put that.
Last edited on
The code shall not be compiled because name number was not declared before its using in function RemovePunct
Thanks guys... so I rewrote void RemovePunct() but still have the same error. Here is what I have so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void RemovePunct(char old_String[], char new_String[])
{
	int number = 0; 
	for (int i = 0; i != Size; i++)
	{
		if (isalnum(old_String[i]))//If current character is alpha-numeric
		{
			new_String[(i - number)] = old_String[i]; //Add the character to new_String[] at 'i - number'.
			
			number = number + 1;
		}
	}
	return;
}


So while number would certainly have been a problem, it is not the problem
1
2
3
4
5
6
7
8
9

i = 0  num = 0

iteration
1     i = 0    num = 0   
2     i = 1    num = 1
3     i = 2    num = 2
.................................Size

Did I evaluate that right?
Topic archived. No new replies allowed.