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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

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