how to do switchs correctly

how do you make the switch work with this program. please and thank you.

#include <iostream>
using namespace std;

int main()
{
char s[50];
int i;
int letters = 0;
int numbers = 0;
int others = 0;
int total = letters + numbers + others;
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)
{
switch (s[i])
{
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 'N':
case 'n':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++;
break;
}
case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':
case '9':

{
numbers++;
i++;
break;
}
{case '!':case '@':case '#':case '$':case '%':case '^':case '&':case '*':case '(':
case ')':

others++;
i++;
break;

default:


total= letters + numbers + others;

cout << "Your string has " << total << " total characters " << endl;
cout << letters << "letters" << endl;
cout << numbers << "numerical characters" << endl;
cout << others << "other characters" << endl;

break;
}
}
}
return 0;
}
Hi,

First up always use code tags:

http://www.cplusplus.com/articles/z13hAqkS/


That is not a good use of a switch IMO. Instead test if each char (with a while loop) is an alpha with std::isalpha

http://www.cplusplus.com/reference/cctype/isdigit/
http://www.cplusplus.com/reference/cctype/isalpha/
I'm sorry about the tags I'm really new to this but I will use them next time. I know its not logically to use a switch for this program but this is what my instructor wants. this program was a if else statement but he wanted us to convert it a switch when I compile and run it. It does not count anything in the string it just says zero for total of character's, letters, number, and other. How do I fix that. I have to use iostream and nothing else.
Sorry for the delay, have been watching the rugby.

I'm sorry about the tags I'm really new to this but I will use them next time.


It would be good if you could use them this time. Edit your post, and use the <> button on the format menu.

It looks like you have the last part cout's as part of the default case, but it's hard to tell without code tags and indentation. That's why we are always keen for posters to use them all the time. One can also compile / run the code when there are tags.

Try moving that code out of the switch
thanks for trying to help me I just have no clue what your talking about when it comes to tags I will just try to figure something out with this program thanks again sorry I'm not understanding the tags thing this is not my area of expertise.
I just have no clue what your talking about when it comes to tags


That's why I posted a link to the article, so you could read it :+)

The code tags are actually there before you make a post, you must have deleted them. And the hint about the <> button.

Try moving that code out of the switch


1
2
3
4
5
6
total= letters + numbers + others;

cout << "Your string has " << total << " total characters " << endl;
cout << letters << "letters" << endl;
cout << numbers << "numerical characters" << endl;
cout << others << "other characters" << endl;

thank you It work I actually had to move that part of the code not only from the switch but from the while to. thank you so so much those {} for the while and switch has to be before that part of the code. thank you thank you!!!!
Right - good you have a solution.

Had you used code tags from the start, I would have seen the problem straight away - which would have saved you 2 or 3 hours.
you do know that 'a' to 'z' and 'A' to 'Z' are continuous values in the ascii table?
Topic archived. No new replies allowed.