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

Home Posts Topics Members FAQ

Switch/Case Fallthrough BUG

43 New Member
I have a switch/case statement using numerics for a menu, but when i enter an alphanumeric statment it goes into a loop printing my menu and the default statement from switch/case the default statement. The only way to end it is ctrl-c. the code is below. Can someone please explain this quirk to me?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int CHOICE;
  4. int LTENC[11];
  5.  
  6. //SNIPPED - cArrayModify(); / pLastTen();
  7.  
  8. void doNothing()
  9. {     
  10.     printf("\n");
  11.     cArrayModify();    
  12. }
  13.  
  14. void invalidChoice()
  15. {
  16.     printf(" Invalid Choice!! \n");    
  17.     cArrayModify();
  18. }
  19.  
  20. void menu()
  21. {
  22.     cArrayModify();
  23.     printf(" \n \n"); 
  24.     printf(" Menu: \n");     
  25.     printf(" 1 - Print Menu \n");
  26.     printf(" 2 - Do nothing \n");    
  27.     printf(" 3 - Exit \n \n"); 
  28.     pLastTen();
  29.  
  30. }
  31.  
  32. int programexit()
  33. {
  34.     printf("\n Exit Conditions met, program will now exit!\n");
  35.     return 0;
  36. }
  37.  
  38. void commandlp()
  39. {
  40.     while(1)
  41.     {
  42.         printf("-> ");
  43.         scanf("%d",&CHOICE);
  44.         switch(CHOICE)
  45.         {
  46.             case 1 :  menu(); 
  47.             break;
  48.  
  49.             case 2 : doNothing();
  50.             break;
  51.  
  52.             case 3 : programexit();
  53.             exit(0);
  54.  
  55.             default: invalidChoice();
  56.             break;
  57.  
  58.         }
  59.     }
  60.  
  61. }
  62.  
  63.  
  64. int main()
  65. {
  66.  
  67.     menu();
  68.     commandlp();
  69.  
  70. }
Mar 6 '08 #1
5 2888
oler1s
671 Recognized Expert Contributor
Please give an example. Show the program input you used, a bit of sample output, etc.
Mar 6 '08 #2
gpraghuram
1,275 Recognized Expert Top Contributor
You are only coming out of switch statement and not the while loop.
The break u have given is only for switch statement.
So you should also exit from while loop or change while to if

Raghuram
Mar 6 '08 #3
DaemonCoder
43 New Member
Expand|Select|Wrap|Line Numbers
  1.  Menu:
  2.  1 - Print Menu
  3.  2 - Do nothing
  4.  3 - Exit
  5.  
  6.  
  7.  Last ten commands: 0, 0, 0, 0, 1, 1, 1, 1, 5,-> s
  8.  
  9. ->  Invalid Choice!!
  10. ->  Invalid Choice!!
  11. ->  Invalid Choice!!
  12. ->  Invalid Choice!!
  13. ->  Invalid Choice!!   //  this loops to this with no further user input...
  14.  
  15. -> ^C
The reason i didnt break oout of the while loop is because this is a menu with an exit function. I dont want it to exit unless the user says to exit.
Mar 6 '08 #4
oler1s
671 Recognized Expert Contributor
Yeah, there's a bug in your code. And it's not the switch statement. I was going to type up a large post, but I realized that an article on scanf explains the issue.
Mar 6 '08 #5
DaemonCoder
43 New Member
So using scanf() is my problem... So now i have another issue...

Expand|Select|Wrap|Line Numbers
  1.  
  2. <SNIPPED>
  3. while(1)
  4.     {
  5.         printf("-> ");
  6.         sscanf("%*s",CHOICE_STRING);
  7.  
  8.         CHOICE = strcmp (CHOICE_STRING, commandListVariable);
  9. // if  commandListVariable == "exit" and CHOICE_STRING == "exit"
  10. // what does this return...???
  11.  
  12.  switch(CHOICE)
  13.         {
  14.  
  15. <snipped>
  16.  
  17.  
Do i have to create a seperate variable for each command or is there a way to use strcmp for a direct comparison to a literal so that i get the correct command executed in the switch statement??

The information that i found says "Returns an integral value indicating the relationship between the strings". How do i 'predict' what the valuse of the comparison will be?
Mar 7 '08 #6

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

Similar topics

12
3228
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
11
1282
by: suzy | last post by:
in vb6 you can do this: select case value case 1, 2 'do something end case
6
449
by: Mark Broadbent | last post by:
How on earth did Anders & co arrive at this awful switch/ case/ select statement? Not only does it lose the abilty to do an intelligent switch a-la VB eg. using >, <, list of values etc .... but it also lacks a "C# feel" e.g. I would have expected a brace pair to be required between each case label .... and the break to prevent fall thru is pointless? Am I misguided in thinking that Anders has tried to adhere to the C++ case
4
29234
by: genc ymeri | last post by:
Hi, I'm trying to do a very easy thing in SWITCH statment in C# but for some reasons I can't do it (I'm getting errors messages,probably I'm missing something) What I want to do is this : int n = aObject.FunctionOfIntergerType; switch (n)
15
3512
by: Benny Raymond | last post by:
I'm confused as to how fallthrough is limited in switch. For example the following works: string switch_test = "a"; switch (switch_test) { case "a": case "b": case "c": doSomething(a);
24
4376
by: Mark | last post by:
Hi - how can I get a switch statement to look for a range of values? I have: function payCalc(field, field2) switch(field.value) { case 0-50: field2.value="lower band"; case 51-99: field2.value="mid band"; case 100-9999: field2.value="high band";
11
3152
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test 1"); break;
7
3357
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
0
8683
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
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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...
1
8902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7740
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...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
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
4372
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...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.