C/C++ How to Use C++ Switch Statements: Examples and Tips for Beginners

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Switch
AI Thread Summary
The discussion focuses on writing a switch statement in C++ to evaluate a character variable, `origLetter`. The requirements specify that if `origLetter` is 'a' or 'A', the program should print "Alpha"; if it is 'b' or 'B', it should print "Beta"; and for any other character, it should print "Unknown". Participants clarify that the switch statement is the appropriate structure to use, emphasizing the need for fall-through behavior for the cases of 'a' and 'A'. They also highlight the importance of correctly spelling "default" in the switch statement. The conversation includes guidance on how to implement the print statements for the specified cases, with a suggestion to replace placeholder text with the actual print commands. Overall, the thread serves as a tutorial on using switch statements for character evaluation in C++.
ineedhelpnow
Messages
649
Reaction score
0
Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If 'b' or 'B', print "Beta". For any other character, print "Unknown". Use fall-through as appropriate. End with newline.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   char origLetter = '?';

   origLetter = 'a';
   <STUDENT CODE>

   return 0;
}

Just do the <STUDENT CODE> part.
i don't get it. do i do case or if?
 
Technology news on Phys.org
ineedhelpnow said:
Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If 'b' or 'B', print "Beta". For any other character, print "Unknown". Use fall-through as appropriate. End with newline.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   char origLetter = '?';

   origLetter = 'a';
   <STUDENT CODE>

   return 0;
}

Just do the <STUDENT CODE> part.
i don't get it. do i do case or if?

Do "switch".

That is:
Code:
switch (<expression>)
{
    case <constant>: // Fall through
    case <constant>: <statements>; break;
    case <constant>: <statements>; break;
    default: <statements>; 
}

Oh. And don't spell it like [m]defualt[/m]. It will take you ages to figure that out.
Especially since it is completely legal, syntactically, as a label. :eek:
 
could you be a little more specific? i don't really understand how to do it since i have like lowercase and uppercase alpha and beta
 
ineedhelpnow said:
could you be a little more specific? i don't really understand how to do it since i have like lowercase and uppercase alpha and beta

So it's like:
Code:
    switch (origLetter)
    {
        case 'a': // Fall through
        case 'A': <handle 'A' or 'a' statements>; break;
        ...
    }
 
ok so could you explain the <handle 'a' or 'A'...> part. I am not familiar with it
 
ineedhelpnow said:
could you explain the <handle 'a' or 'A'...> part. I am not familiar with it
"<handle 'a' or 'A'...>" means that you should replace it with the code according to your instructions, namely: If 'a' or 'A', print "Alpha". That is, you should replace "<handle 'a' or 'A'...>" in ILS's code with the line that prints the string "Alpha".

For more about the [m]switch[/m] statement in C and C++, see here and here or, better, read your textbook or lecture notes.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
22
Views
3K
Replies
25
Views
2K
Replies
40
Views
3K
Replies
5
Views
3K
Replies
4
Views
14K
Replies
6
Views
1K
Back
Top