Trying to sort bits and pieces of a user-inputted string into separate arrays

The purpose of this program is to get a user to input a random string of characters and then sort the input into three different arrays: one array for numbers, one array for letters, and one array for special characters. I'm using a switch statement embedded in a for loop in order to do this. I've verified that the switch statement works properly. The problem is saving the characters to their respective arrays. The program "works" properly, but the data I obtain is incorrect. I appreciate any help or advice you might have. Here is the code:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
using namespace std;

int main()
{
	const int NUMBERS = 100;
	string userInput;
	int i, numChars;
	int numbers[NUMBERS];
	int letters[NUMBERS];
	int spec[NUMBERS];
	
	cout << "Enter a random string of characters." << endl;
	getline(cin, userInput);
	
	numChars = int(userInput.length());

	for (i = 0; i < numChars; i++)
	{
		switch(userInput.at(i))
		{
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			numbers[i] = userInput.at(i);
			break;
		case 'a':
		case 'A':
		case 'b':
		case 'B':
		case 'c':
		case 'C':
		case 'd':
		case 'D':
		case 'e':
		case 'E':
		case 'f':
		case 'F':
		case 'g':
		case 'G':
		case 'h':
		case 'H':
		case 'i':
		case 'I':
		case 'j':
		case 'J':
		case 'k':
		case 'K':
		case 'l':
		case 'L':
		case 'm':
		case 'M':
		case 'o':
		case 'O':
		case 'p':
		case 'P':
		case 'q':
		case 'Q':
		case 'r':
		case 'R':
		case 's':
		case 'S':
		case 't':
		case 'T':
		case 'u':
		case 'U':
		case 'v':
		case 'V':
		case 'w':
		case 'W':
		case 'x':
		case 'X':
		case 'y':
		case 'Y':
		case 'z':
		case 'Z':
			letters[i] = userInput.at(i);
			break;
		default:
			spec[i] = userInput.at(i);
			break;
		}
	}
Look out for this: http://www.cplusplus.com/reference/clibrary/cctype/ (esp. isdigit()/isalpha())

Your problem is that you need a counter for each type, e.g. nr_count. Then it'd look like this:
1
2
3
4
int nr_count = 0;
...
numbers[nr_count] = userInput.at(i);
++nr_count;


do this for the other types accordingly
Yep, that definitely worked. I should have realized I needed a counter for that. I've never used the cctype commands before, so I'll play around with them and get to know them better. I really appreciate the help.
Topic archived. No new replies allowed.