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.
//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
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.
#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':
...
}
} elseif (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
}
}
"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.
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.
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";
} elseif (c >= '0' && c <= '9') {
std::cout << "Digit `" << c << "` was entered.\n";
} else {
std::cout << "Symbol `" << c << "` was entered.\n";
}
return 0;
}