473,698 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Expressions in Switch Statements

10 New Member
In C++, my "switch" statement is okay when I ask it do evaluate ONE expression. (My number that I'm evaluating is one of ten single digits; I have ten cases for the ten digits.)

BUT, I have five separate digits to evaluate.

I can't believe that the only solution is to do:

Expand|Select|Wrap|Line Numbers
  1. switch (num1)
  2. {
  3. case 0:...
  4. case 9:
  5. }
  6.  
  7. switch(num2)
  8. {
  9. case 0:...
  10. case 9:
  11. }
etc.

Assuming that my question makes sense, how do I evaluate multiple expressions in a switch statement?
Mar 10 '07 #1
13 8340
horace1
1,510 Recognized Expert Top Contributor
not sure about your logic requirements?
are the statements associated with case 0:. case 1: etc the same in all the switch statements. Something like
Expand|Select|Wrap|Line Numbers
  1.     if( num1==1 && num2 ==1 && nium3==1 ....)    {.... }
  2.     else
  3.      if( num1==2 && num2 == 2 && nium3==2 ....)    {.... }
Mar 10 '07 #2
jsta43catrocks
10 New Member
I have a list of five different numbers (digits).

My switch statement does something different whether the digit == 1 or whether the digit == 2, etc.

I'm doing the switch statement five different times to five different digits.

It's not liking me trying this:

Expand|Select|Wrap|Line Numbers
  1. switch (digit1, digit2, digit3, digit4, digit5)
  2. case 1:...
  3. case9:
  4. }
but that's essentially what I'm trying to do.

Writing out the entire thing five separate times doesn't seem right.

Does this make sense? (the question, that is)
Mar 10 '07 #3
horace1
1,510 Recognized Expert Top Contributor
you store the five digits in an array and use a loop
Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<5;i++)
  2.   switch (digits[i])
  3.    { 
  4.     case 1:...
  5.     case9:
  6. }
Mar 10 '07 #4
jsta43catrocks
10 New Member
Sadly, we can't use arrays for this.

It makes sense, but this is for a project for school, and we haven't gotten that far in the text yet. (and the teacher has yet to talk about them.)

Are there any other options?




you store the five digits in an array and use a loop
Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<5;i++)
  2.   switch (digits[i])
  3.    { 
  4.     case 1:...
  5.     case9:
  6. }
Mar 10 '07 #5
Ganon11
3,652 Recognized Expert Specialist
Is there a way that you can use a loop to evaluate one digit at a time, or do you have to have them stored as you show (digit1, digit2, ..., digit5)?

I'm thinking something like this:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < 5; i++) {
  2.    cin >> digit;
  3.    //Switch statement to evaluate digit
  4. }
Mar 10 '07 #6
jsta43catrocks
10 New Member
Sadly, we can't use arrays for this.

It makes sense, but this is for a project for school, and we haven't gotten that far in the text yet. (and the teacher has yet to talk about them.)

Are there any other options?
I've tried converting it all to a large "if...if else" statement, but it's REALLY long because I've got 10 "else's" for each of the 5 digits, so it's 50+ lines long. (UGH!)
Mar 10 '07 #7
jsta43catrocks
10 New Member
Each digit is already named (digit1, digit2, etc.), because they're already being converted from being "chars" into "ints."

(They're being read from a file, but "12345" is read as an entire number, so I'm reading them each as characters first -- one at a time -- and then converting them to integers, so then I can work with them.)

In my effort to boil the question down to the simplest elements, some details get lost.

My "if...if else" statements WORK (see other post), but it looks so cumbersome and awkward. I just thought the switch statement would make it easier(?)




Is there a way that you can use a loop to evaluate one digit at a time, or do you have to have them stored as you show (digit1, digit2, ..., digit5)?

I'm thinking something like this:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < 5; i++) {
  2.    cin >> digit;
  3.    //Switch statement to evaluate digit
  4. }
Mar 10 '07 #8
horace1
1,510 Recognized Expert Top Contributor
I've tried converting it all to a large "if...if else" statement, but it's REALLY long because I've got 10 "else's" for each of the 5 digits, so it's 50+ lines long. (UGH!)
could you have the switch in a function and call it five times?
Mar 11 '07 #9
Ganon11
3,652 Recognized Expert Specialist
What exactly are you going to be doing depending on these digits? Depending on what it is, there might be a way to simplify this process.
Mar 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
13003
by: Anders Borum | last post by:
Hello! Consider the following enumeration used to combine several of the values. I am trying to figure out what the best solution is to check for the presence of a value using bitwise comparisons. I'm able to get if statements to work, but am unsure if switch statemens allow this. Maybe it's just me on a friday, but if you've got a better solution than if statements, please let me know.
1
1306
by: Steve | last post by:
Why does the Visual Studio editor fail to indent cases under switch statements and public/private/protected in class definitions? I've been working with Visual Studio since 4.x and it's always been this way. I thought I'd get used to it, but the failure to indent these lines does nothing but make unreadable and sloppy looking code. I always override this behavior when writing switch statements and defining classes, but I still run into this...
2
1922
by: ALi Shaikh | last post by:
Hey switch statements are not working it gives me errors saying "case" illegal use of word. heres the code //Chinese calender Project #include <iostream.h> int main() { int year; int myear; cout<<"Enter a year"; cin>>year;
6
4298
by: dmc333 | last post by:
I have a program where I ask a user to input their income and then it evaluates the tax using switch statements. The program ends where I enter the income and fails to show me the tax. Here's some of the code: case 1: if (income < 1000) tax = income*.3; printf("\nYou have this much: %lf in taxes", tax); break; Thanks
2
3481
by: Emre DÝNÇER | last post by:
is it possible to have a multiple criteria switch in C# switch(name , surname){ case "John","Smith" break; } thanks in advance
11
4962
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the statements in switch #2 enter a 3rd switch. I don't receive any compile errors except whenever I attempt to add default switches to switches 2 or 3.
4
2043
by: charmeda103 | last post by:
Im working on a program in C++ and i need to use switch statements. this is my first using switches. how to put on in my while loop. can you give me an example. heres what I have so far: #include <iostream> #include <iomanip> #include <string> #include <fstream> #include <cmath>
4
4737
by: formsrtuff | last post by:
Hello all, I'm trying to get multiple IF, THEN statements to work. Based on several examples, I believe it is coded properly, however, when I execute the code, nothing happens with the exception of one condtion. Here's the code and condition: If Exterior.Value = "Snow Hauling" And Me.statecmbo <> "" And Me.zipcmbo <> "" Then Me.Filter = " Like '" & Nz(statecmbo.Value, "") & "*' And Like '" & Nz(zipcmbo.Value, "") & "*'...
1
2071
by: Josh Ibrahim | last post by:
Hello all :D, I am currently learning Java. I have a question about switch statements, in this program I am trying to use a switch statement instead of an If/else sequence to display a letter grades numerical value; however I would also like to include/exclude the '+' and '-' characters. example: enter a letter grade with capital letters(example a = A or b+ = = B+): input: A output:the numeric value is: 4.0 // switch sequence starts to...
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.