Switch Statement

I have this problem in my program code. I'm trying to get a switch statement to work where a user has to input a digit or letter. I don't know how to put the cin statement as. Is there a way to put "or" in a cin statement?
You can always put an if statement before hand, but I think if you put case 1 and a case a in there it will still work, test that out, my visual studios is on the fritz.
So here is my code so far:

//This program lets the user input a letter number,or a single digit
//It determines the type of character entered and outputs the character and the type


#include <iostream>

using namespace std;

int main()
{
char digit;
char letter;



cout << "Enter a single digit or letter.";
cin >>
Last edited on
Remember to use code tags when posting codes out.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
char x, y;


cout << "Digits or Letters?(D\L)\n";
cin >> x;
if (X == "D")
{


}

else if (X == "L")

{


}

You could do something like that.
I don't know if this works but you could try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
char x;



cout << "Enter a single digit or letter.";
cin >> x;

case 'a':

case 1:

default:

Last edited on
Sorry i'm new how do you post code tags? It also has to display out what the letter or number was
such as if i typed in 5 it should say as an output "you have entered 5, a number". Should I use enum data type?
Doing it with just cin is the wrong approach, if you need more detailed analysis of an input value, getting the input as raw text and parsing it is the appropriate thing to do.

In this case the parsing is even trivial, it just involves getting the input as a char, and checking if it's a digit or a alpha character.

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

int main() {
char c;
std::cin >> c;
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
//it's an alpha character (letter)
//do switch
switch (c) {
case 'a':
...
}
} else if (c >= '0' && c <= '9') {
//it's a digit
//if necessary you can convert it into the actual numeric value like so:
//c -= '0';

//do switch
switch (c) {
case '0':
...
}
} else {
//error, it's a symbol of some sort
}
}
Last edited on

#include <iostream>

using namespace std;

int main()
{
char x;

cout << "enter single digit or letter.";
Last edited on
Oh man i feel stupid now.I didn't know you can say that A is greater than Z in C++. I thought that wouldn't work.
And how does one post code tags?
[code]
...
...
[/code]
stravant how does the case work in that situation? and if a symbols such as (* or ^)were included too would i type another else if?
"I didn't know you can say that A is greater than Z in C++."

Characters in C++ are a numeric type with a size of one byte. You can use any of the normal numeric operators on them, typically only +, - and comparisons are useful with actual character constants though.

If would be pointless to make a new else-if block for the symbols, as they can't be easily paired off into groups like the letters / numbers can. The reason that I used else-if blocks for the letters / numbers is because you wanted the distinction for those big blocks, and having that distinction in each branch of a switch statement would be repetitive. For the symbols though, there are no extra distinctions to be made, so handling each case in a third switch statement will work well.

Just put a switch statement in the else block if you want to handle any of the symbols, that's the fall-through for anything that's not a letter or number right now.
Last edited on
[code]


Is that right so far?
Last edited on
Yes aside from your ugly indentation style (There's no right style, but there certainly are wrong ones). If you want the same behavior for every letter though you'd be better off just doing the output outside of the switch.
And what is case 'a' and '0'? Where did that come from? Sorry for being a hassle. I just started with C++. I want my code to display back as an output what the letter, symbol, or number that was entered.
Last edited on
If all you want is to determine if the input is a alpha character, digit or symbol, then you don't need any switch statements at all, the else/ifs will suffice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main() {
	char c;
	std::cin >> c;
	if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
		std::cout << "Character `" << c << "` was entered.\n";
	} else if (c >= '0' && c <= '9') {
		std::cout << "Digit `" << c << "` was entered.\n";
	} else {
		std::cout << "Symbol `" << c << "` was entered.\n";
	}
	return 0;
}
Well I thought it was simpler to use switch because there are different cases.
Switches are useful when you're branching based on a simple integral quantity being equal to given constants.

If you need more complex cases, such as a value being in the range of all letters or digits, then a switch statement isn't the right thing to use.
So you mean number based? i guess that makes sense.
Topic archived. No new replies allowed.