Write a C++ program that makes use of a switch statement but without the use of
ANY if statements. The program reads in a character (not including a white space)
from the user and then display accordingly:
"The input is a digit", if the character is between ‘0’ and ‘9’
"The input is an upper case letter” if the character is between ‘A’ and ‘Z”
"The input is a lower case letter" if the character is between ‘a’ and ‘z’
"The input is a special symbol” if otherwise
So far I have searched around everywhere on the net, youtube included to find a solution. All I know how to do is what I have so far:
#include <iostream>
using namespace std;
int main (void)
{
int number;
cout << "Please input something: ";
cin >> number;
switch (number)
{
case 0:
cout << "The input is a digit" << endl;
break;
case 1:
cout << "The input is a digt" << endl;
break;
case 2:
cout << "The input is a digt" << endl;
break;
case 3:
cout << "The input is a digt" << endl;
break;
case 4:
cout << "The input is a digt" << endl;
break;
case 5:
cout << "The input is a digt" << endl;
break;
case 6:
cout << "The input is a digt" << endl;
break;
case 7:
cout << "The input is a digt" << endl;
break;
case 8:
cout << "The input is a digt" << endl;
break;
case 9:
cout << "The input is a digt" << endl;
break;
default:
cout << "The input is wrong" << endl;
break;
}
system ("pause");
return (0);
}
That piece of code is just for the first part of the question, from 0 = 9. But I have no clue how to add characters aswell? How can I add characters and letters in one switch statement? Or is there another way to answer that question?
Please and Thankyou to anyone who can give me some help and/or advice =)
Bestakes: Hey, it is an assignment and I didn't want you to answer it for me, but tyvm anyways! =) I just had no clue how to add characters as well as integers into the program. I know about the ASCII table and realized needed to use from 97-122 for lowercase, 65 - 90 for uppercase, and 48- 57 (or something) for 0-9. That first part you put down char c[2];
cin>>c;
i = atoi( c );
I had no clue about, that is really what I needed to see. So that is what allows me to place in the numbers and get the proper input and output for what the user types in as a character. Ty again =)
Gotta love the pretentiousness of some posters on here, the guy is asking for help and they get all high and mighty...
It's kind of an unspolen rule on programming forums that you are not supposed to provide a full solution if the question is obviously an assignment. I think it applies here as well.
Anyways, it looks like you're supposed to use the ASCII table. All numbers and letters are next to eachother, so you only need to check in which "range" the input character falls. It's much more efficient than a huge switch.
Gotta love the pretentiousness of some posters on here, the guy is asking for help and they get all high and mighty...
It's kind of an unspolen rule on programming forums that you are not supposed to provide a full solution if the question is obviously an assignment. I think it applies here as well.
I don't think that's what he was referring to, I think it was nonsense like this:
First off, your shit is fucked so I'll just say this...
OP, take solace in the fact that only an amateur would make such a comment to a beginner. Anyone who was experienced and competent wouldn't be such a dick. That said... he did actually offer help after saying that.
checkout
cplusplus.com/reference/clibrary/cctype/
there is function to determine if a char is number or whatnot etc.
you don't have to invent the wheel again.
Thanks everyone for your help, really much appreciated.
Xion: Thanks for that, lol it makes the program look so much simpler with the fall through. I was so lost that I was trying stuff like case '0' ... '9':
I didn't realise I had to format it like that. Thx again =)
Ok, here is my final code:
#include <iostream>
using namespace std;
int main ()
{
char x;
cout << "Please input a number (between 0-9), symbol, lowercase letter or uppercase letter: ";
cin >> x;
cout << endl;
switch (x) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
cout << "The input is a digit." << endl;
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 'X':
case 'Y':
case 'Z':
cout << "The input is an uppercase letter." << endl;
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':
cout << "The input is a lowercase letter." << endl;
break;
default:
cout << "The input is a special symbol." << endl;
break;
}
system ("pause");
return (0);
}
There is still one problem with is =(. Even if I put in the number 10, 234, etc... It outputs "The input is a digit". I think I know why it does this, it does it because the characters from 0 - 9 make up every number. So if I put in 10046 for example, each individual character in that number is within 0-9. So how do I stop the program from reading it this way lol?
'x' is of the type char, so it can only hold a single character. Input as many characters as you like; only the first will be read into 'x' (istream& operator>> (istream& is, char& ch ); knows how to properly read into a char).
If you want to read in a string, not simply a single character, then 'x' should be a 'std::string'. However, your requirements state to only read a single character, so I think your solution is fine ("fine" in terms of the assignment, but you would never write "real" code like this).
On a side note, 'using namespace <whatever>;` is typically bad form because it imports *everything* in the namespace <whatever> into the global namespace, which can cause collisions (i.e., maintenance headaches). It's ok for small, one-off programs, but never put it in a header file, or an implementation file that will require maintenance over an extended period of time.
'x' is of the type char, so it can only hold a single character. Input as many characters as you like; only the first will be read into 'x' (istream& operator>> (istream& is, char& ch ); knows how to properly read into a char).
If you want to read in a string, not simply a single character, then 'x' should be a 'std::string'. However, your requirements state to only read a single character, so I think your solution is fine ("fine" in terms of the assignment, but you would never write "real" code like this).
On a side note, 'using namespace <whatever>;` is typically bad form because it imports *everything* in the namespace <whatever> into the global namespace, which can cause collisions (i.e., maintenance headaches). It's ok for small, one-off programs, but never put it in a header file, or an implementation file that will require maintenance over an extended period of time.
switch(char)
{
case '0'..'9' : cout << "The input is an number." << endl;
break;
case 'a'..'z' : cout << "The input is an lowercase letter." << endl;
break;
case 'A'..'Z' : cout << "The input is an uppercase letter." << endl;
break;
}
If i remember correctly that is how.
That is not valid C++. C++ does not supports ranges in switch statements (or, for that matter, range literals at all). Otherwise it would be impossible to implement the switch as a jump table, i.e., provide O(1) complexity in time. Each case must correspond to one and only one integral constant. That looks more like Ruby or Python (or some other higher level language)
switch(char)
{
case '0'..'9' : cout << "The input is an number." << endl;
break;
case 'a'..'z' : cout << "The input is an lowercase letter." << endl;
break;
case 'A'..'Z' : cout << "The input is an uppercase letter." << endl;
break;
}
If i remember correctly that is how.
That is not valid C++. C++ does not supports ranges in switch statements (or, for that matter, range literals at all). Otherwise it would be impossible to implement the switch as a jump table, i.e., provide O(1) complexity in time. Each case must correspond to one and only one integral constant. That looks more like Ruby or Python (or some other higher level language)
Don't remember what language it was( could be specific compiler ? ) ..
And if you think a switch is a O(1) operation... you might get a surprise...
No C++ compiler. You're right though; the language doesn't require that a switch statement be an O(1) operation (it doesn't say anything about how it must be implemented, only how it must behave), but it can be made to be an O(1) operation, and often is. case blocks are simply labels, easy to optimize in the general case.
I did make a mistake however; even though that code is not valid C++, the values are still constant, and it could be trivially translated to a jump table. For whatever reason I was thinking of variables in a case expression (like Ruby allows), which would require multiple runtime checks.
It doesn't really matter though; the language does not allow for range literals, and switch cases must be constant, integral expressions.
Write a C++ program that makes use of a switch statement but without the use of
ANY if statements. The program reads in a character (not including a white space)
from the user and then display accordingly:
"The input is a digit", if the character is between ‘0’ and ‘9’
"The input is an upper case letter” if the character is between ‘A’ and ‘Z”
"The input is a lower case letter" if the character is between ‘a’ and ‘z’
"The input is a special symbol” if otherwise
So far I have searched around everywhere on the net, youtube included to find a solution. All I know how to do is what I have so far:
#include <iostream>
using namespace std;
int main (void)
{
int number;
cout << "Please input something: ";
cin >> number;
switch (number)
{
case 0:
cout << "The input is a digit" << endl;
break;
case 1:
cout << "The input is a digt" << endl;
break;
case 2:
cout << "The input is a digt" << endl;
break;
case 3:
cout << "The input is a digt" << endl;
break;
case 4:
cout << "The input is a digt" << endl;
break;
case 5:
cout << "The input is a digt" << endl;
break;
case 6:
cout << "The input is a digt" << endl;
break;
case 7:
cout << "The input is a digt" << endl;
break;
case 8:
cout << "The input is a digt" << endl;
break;
case 9:
cout << "The input is a digt" << endl;
break;
default:
cout << "The input is wrong" << endl;
break;
}
system ("pause");
return (0);
}
That piece of code is just for the first part of the question, from 0 = 9. But I have no clue how to add characters aswell? How can I add characters and letters in one switch statement? Or is there another way to answer that question?
Please and Thankyou to anyone who can give me some help and/or advice =)
0-9 is one case, not 9.
A-Z is one
a-z is one
char c[2];
cin>>c;
i = atoi( c );
I had no clue about, that is really what I needed to see. So that is what allows me to place in the numbers and get the proper input and output for what the user types in as a character. Ty again =)
char c [2]
i = atoi ( c )
do please? How they work?
I never learnt that stuff, and they expect me to make this program. I don't understand how I can just come up with that lol.
It's kind of an unspolen rule on programming forums that you are not supposed to provide a full solution if the question is obviously an assignment. I think it applies here as well.
Anyways, it looks like you're supposed to use the ASCII table. All numbers and letters are next to eachother, so you only need to check in which "range" the input character falls. It's much more efficient than a huge switch.
I don't think that's what he was referring to, I think it was nonsense like this:
OP, take solace in the fact that only an amateur would make such a comment to a beginner. Anyone who was experienced and competent wouldn't be such a dick. That said... he did actually offer help after saying that.
cplusplus.com/reference/clibrary/cctype/
there is function to determine if a char is number or whatnot etc.
you don't have to invent the wheel again.
Good advice, but often times for class assignments, you do actually have to reinvent the wheel.
Xion: Thanks for that, lol it makes the program look so much simpler with the fall through. I was so lost that I was trying stuff like case '0' ... '9':
I didn't realise I had to format it like that. Thx again =)
#include <iostream>
using namespace std;
int main ()
{
char x;
cout << "Please input a number (between 0-9), symbol, lowercase letter or uppercase letter: ";
cin >> x;
cout << endl;
switch (x) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
cout << "The input is a digit." << endl;
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 'X':
case 'Y':
case 'Z':
cout << "The input is an uppercase letter." << endl;
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':
cout << "The input is a lowercase letter." << endl;
break;
default:
cout << "The input is a special symbol." << endl;
break;
}
system ("pause");
return (0);
}
There is still one problem with is =(. Even if I put in the number 10, 234, etc... It outputs "The input is a digit". I think I know why it does this, it does it because the characters from 0 - 9 make up every number. So if I put in 10046 for example, each individual character in that number is within 0-9. So how do I stop the program from reading it this way lol?
If you want to read in a string, not simply a single character, then 'x' should be a 'std::string'. However, your requirements state to only read a single character, so I think your solution is fine ("fine" in terms of the assignment, but you would never write "real" code like this).
On a side note, 'using namespace <whatever>;` is typically bad form because it imports *everything* in the namespace <whatever> into the global namespace, which can cause collisions (i.e., maintenance headaches). It's ok for small, one-off programs, but never put it in a header file, or an implementation file that will require maintenance over an extended period of time.
Oh I see, tyvm for the response.
That is not valid C++. C++ does not supports ranges in switch statements (or, for that matter, range literals at all). Otherwise it would be impossible to implement the switch as a jump table, i.e., provide O(1) complexity in time. Each case must correspond to one and only one integral constant. That looks more like Ruby or Python (or some other higher level language)
Look Ma, no if's!
And this is why I dropped out of college
No C++ compiler. You're right though; the language doesn't require that a switch statement be an O(1) operation (it doesn't say anything about how it must be implemented, only how it must behave), but it can be made to be an O(1) operation, and often is. case blocks are simply labels, easy to optimize in the general case.
I did make a mistake however; even though that code is not valid C++, the values are still constant, and it could be trivially translated to a jump table. For whatever reason I was thinking of variables in a case expression (like Ruby allows), which would require multiple runtime checks.
It doesn't really matter though; the language does not allow for range literals, and switch cases must be constant, integral expressions.