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 -
switch (gasType)
-
{
-
case (('r') || ('R')): printf("Type of gasoline: %s\n",RArray);
-
case (('u') || ('U')): printf("Type of gasoline: %s\n",UArray);
-
case (('s') || ('S')): printf("Type of gasoline: %s\n",SArray);
-
}
-
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: -
switch (gasType)
-
{
-
case 'r' : printf("Type of gasoline: %s\n",RArray);
-
case 'R' : printf("Type of gasoline: %s\n",RArray);
-
}
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...
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: - case 'r':
-
case 'R':
-
printf("Type of gasoline: %s\n", RArray);
-
break;
-
case 'u':
-
case 'U':
-
printf("Type of gasoline: %s\n", UArray);
-
break;
-
...
Another way to get the same effect is: - switch (toupper(gasType))
-
{
-
case 'R':
-
printf("Type of gasoline: %s\n", RArray);
-
break;
-
case 'U':
-
printf("Type of gasoline: %s\n", UArray);
-
break;
-
...
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: - const char *gasName;
-
...
-
switch (toupper(gasType))
-
{
-
case 'R':
-
gasName = RArray;
-
break;
-
case 'U':
-
gasName = UArray;
-
break;
-
...
-
default:
-
gasName = NULL;
-
}
-
if (gasName != NULL)
-
printf("Type of gasoline: %s\n", gasName);
5 4231
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: - case 'r':
-
case 'R':
-
printf("Type of gasoline: %s\n", RArray);
-
break;
-
case 'u':
-
case 'U':
-
printf("Type of gasoline: %s\n", UArray);
-
break;
-
...
Another way to get the same effect is: - switch (toupper(gasType))
-
{
-
case 'R':
-
printf("Type of gasoline: %s\n", RArray);
-
break;
-
case 'U':
-
printf("Type of gasoline: %s\n", UArray);
-
break;
-
...
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: - const char *gasName;
-
...
-
switch (toupper(gasType))
-
{
-
case 'R':
-
gasName = RArray;
-
break;
-
case 'U':
-
gasName = UArray;
-
break;
-
...
-
default:
-
gasName = NULL;
-
}
-
if (gasName != NULL)
-
printf("Type of gasoline: %s\n", gasName);
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
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... -
-
char *weekday;
-
char *RArray;
-
char *UArray;
-
char *SArray;
-
-
switch ( toupper(gasType))
-
{
-
case ('R'): printf("Type of gasoline: %s\n",RArray); break;
-
case ('U'): printf("Type of gasoline: %s\n",UArray); break;
-
case ('S'): printf("Type of gasoline: %s\n",SArray); break;
-
}
-
-
-
switch (dayNum)
-
{
-
case 0: weekDay = "Sunday\0"; break;
-
case 1: weekDay = "Monday\0"; break;
-
case 2: weekDay = "Tuesday\0"; break;
-
case 3: weekDay = "Wednesday\0"; break;
-
case 4: weekDay = "Thursday\0"; break;
-
case 5: weekDay = "Friday\0"; break;
-
case 6: weekDay = "Saturday\0"; break;
-
}
-
printf("Today is : %s\n", weekDay);
-
Any ideas?
Banfa 9,065
Expert Mod 8TB
Defined:
char *weekday;
Used
case 0: weekDay = "Sunday\0"; break;
C/C++ is case sensitive.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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":
|
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...
|
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...
|
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;
}
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |