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? -
#include<stdio.h>
-
#include<stdlib.h>
-
int CHOICE;
-
int LTENC[11];
-
-
//SNIPPED - cArrayModify(); / pLastTen();
-
-
void doNothing()
-
{
-
printf("\n");
-
cArrayModify();
-
}
-
-
void invalidChoice()
-
{
-
printf(" Invalid Choice!! \n");
-
cArrayModify();
-
}
-
-
void menu()
-
{
-
cArrayModify();
-
printf(" \n \n");
-
printf(" Menu: \n");
-
printf(" 1 - Print Menu \n");
-
printf(" 2 - Do nothing \n");
-
printf(" 3 - Exit \n \n");
-
pLastTen();
-
-
}
-
-
int programexit()
-
{
-
printf("\n Exit Conditions met, program will now exit!\n");
-
return 0;
-
}
-
-
void commandlp()
-
{
-
while(1)
-
{
-
printf("-> ");
-
scanf("%d",&CHOICE);
-
switch(CHOICE)
-
{
-
case 1 : menu();
-
break;
-
-
case 2 : doNothing();
-
break;
-
-
case 3 : programexit();
-
exit(0);
-
-
default: invalidChoice();
-
break;
-
-
}
-
}
-
-
}
-
-
-
int main()
-
{
-
-
menu();
-
commandlp();
-
-
}
5 2869
Please give an example. Show the program input you used, a bit of sample output, etc.
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
-
Menu:
-
1 - Print Menu
-
2 - Do nothing
-
3 - Exit
-
-
-
Last ten commands: 0, 0, 0, 0, 1, 1, 1, 1, 5,-> s
-
-
-> Invalid Choice!!
-
-> Invalid Choice!!
-
-> Invalid Choice!!
-
-> Invalid Choice!!
-
-> Invalid Choice!! // this loops to this with no further user input...
-
-
-> ^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.
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.
So using scanf() is my problem... So now i have another issue... -
-
<SNIPPED>
-
while(1)
-
{
-
printf("-> ");
-
sscanf("%*s",CHOICE_STRING);
-
-
CHOICE = strcmp (CHOICE_STRING, commandListVariable);
-
// if commandListVariable == "exit" and CHOICE_STRING == "exit"
-
// what does this return...???
-
-
switch(CHOICE)
-
{
-
-
<snipped>
-
-
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: junky_fellow |
last post by:
Which is better using a switch statement or the if-then equivalent
of switch ?
|
by: suzy |
last post by:
in vb6 you can do this:
select case value
case 1, 2
'do something
end case
|
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...
|
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 :
...
|
by: Juan |
last post by:
Thanks,
Juan.
|
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);
|
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:...
|
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...
|
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...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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: 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...
|
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: 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...
| |