473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Switch Statements will not recognize a char comparison

I wanted to use a switch statement to select from several choices and print the choice.

The problem I am having is that it does not reconize the case statements the way I would assume they should:

Intel based, xp pro, borland 4.5

Expand|Select|Wrap|Line Numbers
  1. switch (gasType)
  2. {
  3.      case (('r') || ('R')): printf("Type of gasoline: %s\n",RArray);
  4.      case (('u') || ('U')): printf("Type of gasoline: %s\n",UArray);
  5.      case (('s') || ('S')): printf("Type of gasoline: %s\n",SArray);
  6. }
  7.  
I get an error message saying that the two statements following the first case are duplicates.

I have also tried to separte them out into individual statements:
Expand|Select|Wrap|Line Numbers
  1. switch (gasType)
  2. {
  3.      case 'r' :  printf("Type of gasoline: %s\n",RArray);
  4.      case 'R' : printf("Type of gasoline: %s\n",RArray);
  5. }
This seemed to work... When it encountered the 'R' it work fine skipping 'r' and printing as you would expect. When it encounter the 'r' first it would print and then print a second time because it would see the 'R'. Very strange.

Any insight would be great...
Mar 17 '10 #1

✓ answered by donbock

The operand for the case statement has to be a single value, not an expression; so you were right to separate them. Your immediate problem is that you need to add break statements to prevent one case from falling through into the next.

You can get the same effect as the || operator in your first attempt via the following construct:
Expand|Select|Wrap|Line Numbers
  1.    case 'r':
  2.    case 'R':
  3.       printf("Type of gasoline: %s\n", RArray);
  4.       break;
  5.    case 'u':
  6.    case 'U':
  7.       printf("Type of gasoline: %s\n", UArray);
  8.       break;
  9.    ...
Another way to get the same effect is:
Expand|Select|Wrap|Line Numbers
  1. switch (toupper(gasType)) 
  2.    case 'R':
  3.       printf("Type of gasoline: %s\n", RArray);
  4.       break;
  5.    case 'U':
  6.       printf("Type of gasoline: %s\n", UArray);
  7.       break;
  8.    ...
However, in this particular example you're doing almost exactly the same thing in each case. You should be suspicious whenever you find yourself typing the same thing on multiple lines. This code can be simplified to:
Expand|Select|Wrap|Line Numbers
  1. const char *gasName;
  2. ...
  3. switch (toupper(gasType))
  4. {
  5.    case 'R':
  6.       gasName = RArray;
  7.       break;
  8.    case 'U':
  9.       gasName = UArray;
  10.       break;
  11.    ...
  12.    default:
  13.       gasName = NULL;
  14. }
  15. if (gasName != NULL)
  16.    printf("Type of gasoline: %s\n", gasName);

5 4231
donbock
2,426 Expert 2GB
The operand for the case statement has to be a single value, not an expression; so you were right to separate them. Your immediate problem is that you need to add break statements to prevent one case from falling through into the next.

You can get the same effect as the || operator in your first attempt via the following construct:
Expand|Select|Wrap|Line Numbers
  1.    case 'r':
  2.    case 'R':
  3.       printf("Type of gasoline: %s\n", RArray);
  4.       break;
  5.    case 'u':
  6.    case 'U':
  7.       printf("Type of gasoline: %s\n", UArray);
  8.       break;
  9.    ...
Another way to get the same effect is:
Expand|Select|Wrap|Line Numbers
  1. switch (toupper(gasType)) 
  2.    case 'R':
  3.       printf("Type of gasoline: %s\n", RArray);
  4.       break;
  5.    case 'U':
  6.       printf("Type of gasoline: %s\n", UArray);
  7.       break;
  8.    ...
However, in this particular example you're doing almost exactly the same thing in each case. You should be suspicious whenever you find yourself typing the same thing on multiple lines. This code can be simplified to:
Expand|Select|Wrap|Line Numbers
  1. const char *gasName;
  2. ...
  3. switch (toupper(gasType))
  4. {
  5.    case 'R':
  6.       gasName = RArray;
  7.       break;
  8.    case 'U':
  9.       gasName = UArray;
  10.       break;
  11.    ...
  12.    default:
  13.       gasName = NULL;
  14. }
  15. if (gasName != NULL)
  16.    printf("Type of gasoline: %s\n", gasName);
Mar 17 '10 #2
I figured that it had to be something straightforward. After reading your post I went looking further into my text book and 3 sections further it brings up the break; statement use with the switch. I will give this a try and that will help big time.

Thanks
Mar 17 '10 #3
Here is the last bit to get my assignment 100%...

I am using a separte switch statement to print the day of the week but I keep getting an error stating Undefined symbol 'weekDay' in function printReceipt

I set it up almost identical to the above examples but can not get it to compile...

Expand|Select|Wrap|Line Numbers
  1.  
  2. char *weekday;
  3. char *RArray;
  4. char *UArray;
  5. char *SArray;
  6.  
  7. switch ( toupper(gasType))
  8. {
  9.      case ('R'): printf("Type of gasoline: %s\n",RArray); break;
  10.      case ('U'): printf("Type of gasoline: %s\n",UArray); break;
  11.      case ('S'): printf("Type of gasoline: %s\n",SArray); break;
  12. }
  13.  
  14.  
  15. switch (dayNum)
  16. {
  17.      case 0: weekDay = "Sunday\0"; break;
  18.      case 1: weekDay = "Monday\0"; break;
  19.      case 2: weekDay = "Tuesday\0"; break;
  20.      case 3: weekDay = "Wednesday\0"; break;
  21.      case 4: weekDay = "Thursday\0"; break;
  22.      case 5: weekDay = "Friday\0"; break;
  23.      case 6: weekDay = "Saturday\0"; break;
  24. }
  25. printf("Today is : %s\n", weekDay);
  26.  
Any ideas?
Mar 17 '10 #4
Banfa
9,065 Expert Mod 8TB
Defined:
char *weekday;

Used
case 0: weekDay = "Sunday\0"; break;

C/C++ is case sensitive.
Mar 18 '10 #5
Sometimes it's the obvious that gets overlooked. That did the trick (as if you had any doubt...

Thank you for the quick response to all.

Very nice site and very nice people. I will be letting my classmates know.
Mar 18 '10 #6

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

Similar topics

26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
15
by: Mike and Jo | last post by:
I've been converting some code to C++. I'm trying to use the Switch function to compare a result. Is it possible to use switch to evaluate '>0', '<0', 0? Example switch (result) { case...
5
by: Neil Zanella | last post by:
Hello, Unlike in pre-C99 versions of C where variables can only be defined at the beginning of blocks, C99 allows variables to be defined in arbitrary places inside blocks. However, gcc 3.2.2...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
11
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at...
3
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...
4
by: Colin Basterfield | last post by:
Hi, Coming back to C# after a foray back into Delphi 7, and I wanted to use the switch keyword to do the following: switch (pwdChar) { case "a".."z": case "A".."Z": case "0".."9":
2
by: 7777777.chen | last post by:
Is it true that VC++ doesn't support switch on string data type? Did anyone know how to handle the following situation? (1) System::String* s_TC = treeview->Nodes->Item->Nodes->Item->Text; or...
10
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a...
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.