Filtering and copying characters to another string

I want to copy every character that is not a symbol or punctuation into a new string variable using isalpha() and isdigit(). The compiler throws an error saying "string subscript out of range." Thanks in advance for your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string new_string;
	string edited_string;

	cout << "Please type a sentence or numbers...\n";
	getline(cin, new_string);

	int count = 0;
	for (int x = 0; x < new_string.length(); x++)
	{
		if (isalpha(new_string[x]) || isdigit(new_string[x]))
		{
			edited_string[count] = new_string[x]; // THIS is the line with problem
			count = count + 1;
		}
	}
return 0;
}
Last edited on
Deleted Post. See kemort's reply.
Last edited on
closed account (48T7M4Gy)
edited_string += new_string[x]; the only change required unless you want to explicitly initialize string edited_string = "";

forget about count.

The rerason it wasn't working is because the string is empty.
closed account (48T7M4Gy)
Sorry chico, I didn't mean to steal your thunder.
oh no at all. Your solution is much simpler. My solution consisted of "reinventing the wheel", while yours was "replace the only the lug-nut." Thanks for jumping in.
Topic archived. No new replies allowed.