Help with charter count using switch statement

I am trying to count the number of uppercase, lowercase, numbers and other characters that a user inputs.

I have to do this using switch statements or I would being using If and Else If

So I know my while statement is messed up and my switch[1] may be hosed as well.

If anyone can give me any advice I would greatly appreciate it.

My current results classify the users input correctly its just not counting it correctly.

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
//  Spencer Ross     keyboard input charter count

#include <iostream>
using namespace std;
int main()
{

// declare and initialize input

char s[50];
int i;
int lowercase = 0;
int uppercase = 0;
int number = 0;
int other = 0;

    cout << "Enter a continuous string of characters with no blank spaces" << endl;
    cout << "(example: aBc1234!@#$%)" << endl << endl;
    cout << "Enter your string :";
    cin >> s;
    cout << endl;


//  i = 0;
// while (s[i] != 0)

do
{
// Switch statement case by case basis
            switch(s[1])
            {
                {

            case 'a':case 'b':case 'c':case 'd':case 'e':
            case 'f':case 'g':case 'h':case 'i':case 'j':
            case 'k':case 'l':case 'm':case 'n':case 'o':
            case 'p':case 'q':case 'r':case 's':case 't':
            case 'u':case 'v':case 'w':case 'x':case 'y':case 'z':
                   ++lowercase;
                    break;
                }
                {
            case 'A':case 'B':case 'C':case 'D':case 'E':
            case 'F':case 'G':case 'H':case 'I':case 'J':
            case 'K':case 'L':case 'M':case 'N':case 'O':
            case 'P':case 'Q':case 'R':case 'S':case 'T':
            case 'U':case 'V':case 'W':case 'X':case 'Y':case 'Z':
                    ++uppercase;
                    break;
                }
                {
            case '1':case '2':case '3':case '4':case '5':
            case '6':case '7':case '8':case '9':case '0':
                    ++number;
                    break;
                }
                {
                default:
                    ++other;
                    break;
                }
            }
        }while (!s);

//while (s[i] != 0)

// output results


    cout << "Your string has "<< uppercase+lowercase+number+other<<" total characters"<<endl;
    cout << uppercase+lowercase << " letters" << endl;
    cout << number << " numerical characters" << endl;
    cout <<  other << " other characters" << endl;
    cout << endl;


    return 0;
}
Last edited on
Hi,

the while loop terminates the first time it reaches line 63. Because you are checking whether s is nullptr or not. Line 65 looks better.
But you also check the second character all the time (line 30, s[1]).

Regards,
Mathes
Last edited on
So when I use line 65

 
while (s[i] != 0);


My prompt opens up but when I push enter to execute the program. I get an error message that says "PROG11.exe has stopping working" like it just crashes.


Also I haven't learned std::string or "auto". I do appreciate the response though.

I am not sure why while (s[i] != 0); makes my program crash.

Do I need to change

 
 switch(s[1])


to something else?
Last edited on
You do not increase i in the while loop. So line 65 will always check the i-th character. This results in an endless loop.

Also you did no initialise i in line 11. I am not sure whether it is automatically set to 0 or not in this context, but just to be sure you should set it to 0, like you do with the other variables.

Topic archived. No new replies allowed.