Question about switch statement

Hello! I am new to c++ programing. I made the following code and the compiler replied that "error: switch quantity not an integer". Can anyone please help to identify the solution to this problem? Thank you!

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
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
const int arraySize=100;
string number[arraySize];
int a,b,c,d,e,f,g,h,i;

int main()
{   string a;
    cout << "please enter a sentence";
    getline(cin, a, '\n');
    string number [arraySize]= a;

    for (int n=0; n<=100; ++n)
    {   int a,b,c,d,e,f,g,h,i;
        switch (number[n])
        {
        case 'A':
            ++a;
            break;
        case 'B':
            ++a;
            break;
        case 'C':
            ++a;
            break;
        case 'D':
            ++b;
            break;
        case 'E':
            ++b;
            break;
        case 'F':
            ++b;
            break;
        case 'G':
            ++c;
            break;
        case 'H':
            ++c;
            break;
        case 'I':
            ++c;
            break;
        case 'J':
            ++d;
            break;
        case 'K':
            ++d;
            break;
        case 'L':
            ++d;
            break;
        case 'M':
            ++e;
            break;
        case 'N':
            ++f;
            break;
        case 'O':
            ++f;
            break;
        case 'P':
            ++g;
            break;
        case 'R':
            ++g;
            break;
        case 'S':
            ++g;
            break;
        case 'T':
            ++h;
            break;
        case 'U':
            ++h;
            break;
        case 'V':
            ++h;
            break;
        case 'W':
            ++i;
            break;
        case 'X':
            ++i;
            break;
        case 'Y':
            ++i;
            break;

        }
    }

}



I have another code..
this time I try to identify the first three digits of a number but it doesn't work as well xC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{   int x;
    cout << "please enter a number";
    cin >> x;
    long long int f= (pow(4,x)+pow(x,3)+pow(x,2)+x-pow(2,x))/(pow(3,x));
    for (int n=0; n< f; ++n)
    {
        cout << f.at(n);
        if (n=3)
            break;
    }
}
Last edited on
Few observations:
1. Try to NEVER use global variables (the ones outside a function). Very bad habit.
2. You redefine variables inside your main function, see line 8 and line 17
3. What is a? Is it an int? or is it a string? Make up your mind. Use different names.
4. On line 14, you define number as an array of strings. Is that your intention? I don't get what you want to do with that line
5. If number is an array of strings, number[n] is a string. You cannot compare a string to a char. Char is an integer type, string is not. However, if number is a string, not an array, number[n] is a char, and then the comparison is OK
6. If number is a string, it may be limited in size to any number, say 23. If you try to access number[23] in your for loop, you will get an error. Your n should go up to the length of the string. For the above example for (int n=0; n<23; n++). Note < instead of <=
OK.... I have no idea what you are trying to achieve it seems odd. But just for kicks here it is working doing whatever it does...

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
92
#include <iostream>
#include <string>

int main(){
	std::string text;
	int a, b, c, d, e, f, g, h, i;
	a = b = c = d = e = f = g = h = i = 0;
	std::cout << "please enter a sentence";
    getline(std::cin, text);

    for (unsigned int n = 0; n < text.length(); ++n)
    {   
        switch (text.at(n))
        {
        case 'A':
            ++a;
            break;
        case 'B':
            ++a;
            break;
        case 'C':
            ++a;
            break;
        case 'D':
            ++b;
            break;
        case 'E':
            ++b;
            break;
        case 'F':
            ++b;
            break;
        case 'G':
            ++c;
            break;
        case 'H':
            ++c;
            break;
        case 'I':
            ++c;
            break;
        case 'J':
            ++d;
            break;
        case 'K':
            ++d;
            break;
        case 'L':
            ++d;
            break;
        case 'M':
            ++e;
            break;
        case 'N':
            ++f;
            break;
        case 'O':
            ++f;
            break;
        case 'P':
            ++g;
            break;
        case 'R':
            ++g;
            break;
        case 'S':
            ++g;
            break;
        case 'T':
            ++h;
            break;
        case 'U':
            ++h;
            break;
        case 'V':
            ++h;
            break;
        case 'W':
            ++i;
            break;
        case 'X':
            ++i;
            break;
        case 'Y':
            ++i;
            break;

        }
    }

	return 0;
}
Last edited on
Opps xD I didn't explain it clearly..
I was trying to separate the sentence the user entered to single English letters and count how many times each letter appears, so I used array cause I wasn't sure how many letters user are going to input, then I tried to separate them with switch statement..
If you were going to do it that way, wouldn't you need 26 variables to keep track of the count for each letter. It seems you use the same variable for 3 different letters.

You'd also probably want to initialize the variables to 0 when you declare them. That way if there are no letters for that variable you don't get a weird output.
Topic archived. No new replies allowed.