473,404 Members | 2,137 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,404 software developers and data experts.

Switch/Case Fallthrough BUG

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 2869
oler1s
671 Expert 512MB
Please give an example. Show the program input you used, a bit of sample output, etc.
Mar 6 '08 #2
gpraghuram
1,275 Expert 1GB
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
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 Expert 512MB
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
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
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
11
by: suzy | last post by:
in vb6 you can do this: select case value case 1, 2 'do something end case
6
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...
4
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 : ...
15
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
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:...
11
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...
7
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...
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
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.