473,386 Members | 1,873 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,386 software developers and data experts.

switch case failure

The following is an extract from my program, which I seperated out and
compiled as its own program.

The problem is that as soon as a case tests true, all the cases below
it execute as well. So if the user inputs, 4, the statements from
cases 4-9 will execute.

(I replaced function calls with cout for testing.)

I am using Win2k, Open Watcom IDE v 1.3.

This has tested as happening in several places in the program.

(p.s. - it compiles as is, it also compliles fine with all the
commented out libraries - same error either way)


//#include <stdio.h>
#include <stdlib.h>
#include <iostream.h> // instead of using namespace std
//#include <string.hpp>
//#include <fstream.h>
//#include <istream>
//#include "Fcopy.h"
//#include <ctime>
//#include <math.h>
int main ()
{
int menu_select_int =-1;

do
{
system("CLS");
cout <<"**********************MENU********************* **\n";
cout << " Create Or Edit Input File..........1\n";
cout << " Calc. no. of stages................2\n";
cout << " Data plot..........................3\n";
cout << " RR vs. No. Of stages...............4\n";
cout << " McCabe Thiele plot.................5\n";
cout << " Printout the input file............6\n";
cout << " Printout of the tray compositions..7\n";
cout << " Printout of the vle data...........8\n";
cout << " Quit...............................9\n";
cout << "************************************************* \n\n";
cout << "MENU: ";
cin >> menu_select_int;

switch (menu_select_int)
{

case 1:
cout << "\ngoing to input data function\n";
cout << "case 1";
cin.ignore();

case 2:
cout << "\ngoing to CNOS function\n";
cout << "case 2";
cin.ignore();

case 3:
cout << "\ngoing to DP function\n";
cout << "case 3";
cin.ignore();

case 4:
cout << "\ngoing to RR vs function\n";
cout << "case 4";
cin.ignore();

case 5:
cout << "\ngoing to McTh function\n";
cout << "case 5";
cin.ignore();

case 6:
cout << "\ngoing to PIF function\n";
cout << "case 6";
cin.ignore();

case 7:
cout << "\ngoing to printout function\n";
cout << "case 7";
cin.ignore();

case 8:
cout << "\ngoing to PO VLE function\n";
cout << "case 8";
cin.ignore();

case 9:
cout << "\ngoing to AYS - end function\n";
cout << "case 9";
cin.ignore();
} //end switch case
} while (menu_select_int != 9);

cin.ignore();
return(0);

}

Oct 6 '05 #1
15 7288

YitzHandel wrote:
The following is an extract from my program, which I seperated out and
compiled as its own program.

The problem is that as soon as a case tests true, all the cases below
it execute as well. So if the user inputs, 4, the statements from
cases 4-9 will execute.

(I replaced function calls with cout for testing.)

I am using Win2k, Open Watcom IDE v 1.3.

This has tested as happening in several places in the program.

(p.s. - it compiles as is, it also compliles fine with all the
commented out libraries - same error either way)


//#include <stdio.h>
#include <stdlib.h>
#include <iostream.h> // instead of using namespace std
//#include <string.hpp>
//#include <fstream.h>
//#include <istream>
//#include "Fcopy.h"
//#include <ctime>
//#include <math.h>
int main ()
{
int menu_select_int =-1;

do
{
system("CLS");
cout <<"**********************MENU********************* **\n";
cout << " Create Or Edit Input File..........1\n";
cout << " Calc. no. of stages................2\n";
cout << " Data plot..........................3\n";
cout << " RR vs. No. Of stages...............4\n";
cout << " McCabe Thiele plot.................5\n";
cout << " Printout the input file............6\n";
cout << " Printout of the tray compositions..7\n";
cout << " Printout of the vle data...........8\n";
cout << " Quit...............................9\n";
cout << "************************************************* \n\n";
cout << "MENU: ";
cin >> menu_select_int;

switch (menu_select_int)
{

case 1:
cout << "\ngoing to input data function\n";
cout << "case 1";
cin.ignore();


Add a "break" statement after every case you don't want
to fall through to the next case...

-David

Oct 6 '05 #2
excellent. that worked. thanks.

Oct 6 '05 #3

YitzHandel wrote:
The following is an extract from my program, which I seperated out and
compiled as its own program.

The problem is that as soon as a case tests true, all the cases below
it execute as well. So if the user inputs, 4, the statements from
cases 4-9 will execute.

[snip]

You need to insert break statements after each case "body". By default
(unfortunately), switch statements fall through. Here's an example:

switch( some_int )
{
case 1:
cout << "Case 1" << endl;
break;
case 2:
cout << "Case 2" << endl;
break;
default:
cout << "Default case" << endl;
}

Cheers! --M

Oct 6 '05 #4
Ian
YitzHandel wrote:
The following is an extract from my program, which I seperated out and
compiled as its own program.

The problem is that as soon as a case tests true, all the cases below
it execute as well. So if the user inputs, 4, the statements from
cases 4-9 will execute.

Your forgot the "break;" after each case!

Ian
Oct 6 '05 #5
mlimber wrote:

You need to insert break statements after each case "body". By default
(unfortunately), switch statements fall through. Here's an example:


It's not "unfortunate". It would be a real pain if it didn't work that
way. That's the only way C (and so C++) can do multiple cases resolving
to the same action. If fall-through weren't there, you couldn't have
this:
switch (n)
{
case 1:
case 2:
case 3:
// do thing A
break;
case 4:
// do thing B
break;
default:
// do thing C
}

Brian
Oct 7 '05 #6
Whether it is unfortunate or not is a matter of opinion. It probably does
allow some programming techniques which are prone to bugs or aren't as
structured. On the other hand a lot of C programmers take advantage of it
for things like this excerpt from a parser:

switch(c) {

case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
accum = 10 * accum + (c - '0');
break;

case 'S' : // Store value

// other alpha command cases

Or the situation where two cases are identical except for a line or two at
the beginning so one can fall through into the other:

case FIRST:
// some initial code
// NOTE: Falls through

case SECOND:
/// code shared between cases

In which case the wise programmer will ALWAYS place the note there pointing
out the lack of a break statement--otherwise somebody will get bit.

Wilton
Oct 8 '05 #7

"Default User" <de***********@yahoo.com> wrote in message
news:3q************@individual.net...
mlimber wrote:

You need to insert break statements after each case "body". By default
(unfortunately), switch statements fall through. Here's an example:


It's not "unfortunate". It would be a real pain if it didn't work that
way. That's the only way C (and so C++) can do multiple cases resolving
to the same action. If fall-through weren't there, you couldn't have
this:
switch (n)
{
case 1:
case 2:
case 3:
// do thing A
break;
case 4:
// do thing B
break;
default:
// do thing C
}

Brian


um, or you could just have an "unbreak" statement that is less error prone
and still do the exact same thing?
Oct 8 '05 #8
>>You need to insert break statements after each case "body". By default
(unfortunately), switch statements fall through. Here's an example:


It's not "unfortunate". It would be a real pain if it didn't work that
way. That's the only way C (and so C++) can do multiple cases resolving
to the same action. If fall-through weren't there, you couldn't have
this:

switch (n)
{
case 1:
case 2:
case 3:
// do thing A
break;
case 4:
// do thing B
break;
default:
// do thing C
}


Talking about switch statements in general:
I would have liked the possibility to write 1..5 meaning 1,2,3,4 or 5 like:

switch (n)
{
case 1..3:
// do thing A
break;
case 4:
// do thing B
break;
default:
// do thing C
}

Josce
Oct 9 '05 #9

On Fri, 7 Oct 2005, Default User wrote:
It's not "unfortunate". It would be a real pain if it didn't work that
way. That's the only way C (and so C++) can do multiple cases resolving
to the same action. If fall-through weren't there, you couldn't have
this:


One could argue that a better language design would be to allow for syntax
such as

switch (n)
{
case 1..3:
// do thing A

case 8 or 9:
// do some other thing

default:
// etc
}

Peter

Oct 9 '05 #10
Peter C. Chapin et al.:
....
One could argue that a better language design would be to allow for syntax
such as

switch (n)
{
case 1..3:
// do thing A

case 8 or 9:
// do some other thing

default:
// etc
}


There have also been proposals for "select all":

select all (n) {
case 1..3 or 5 or 9;
// do A
case 2 or 5..7 or 0;
// do B
case 1 or 6 or 9;
// do C
}

where n=1 would do first A then C, n=2 would do A,B, n=6 would do B,C, etc.
"switch" would be "select first" instead.

This would permit an enhanced version of what Wilton liked - some cases
could skip not just early, but instead any stage. It's almost an
implementation of state transition tables.

BTW, why is a multiway selection less structured than its special case, the
two-way selection? Real life problems are hardly ever structured!
--
Steve


Oct 9 '05 #11
On Sat, 8 Oct 2005 06:35:43 -0400, "Peter C. Chapin"
<pc*****@sover.net> wrote:

On Fri, 7 Oct 2005, Default User wrote:
It's not "unfortunate". It would be a real pain if it didn't work that
way. That's the only way C (and so C++) can do multiple cases resolving
to the same action. If fall-through weren't there, you couldn't have
this:


One could argue that a better language design would be to allow for syntax
such as

switch (n)
{
case 1..3:
// do thing A

case 8 or 9:
// do some other thing

default:
// etc
}

Peter


Doesn't GCC support case ranges eg (1..3) as an extension already?

JimS
Oct 9 '05 #12
Josce wrote:


Talking about switch statements in general:
I would have liked the possibility to write 1..5 meaning 1,2,3,4 or 5 like:

switch (n)
{
case 1..3:
// do thing A
break;
case 4:
// do thing B
break;
default:
// do thing C
}

I would tend to agree. My guess would be that they didn't want to change
the same construct too much from the original C version. But that's just a
guess, you'd have to ask the committee.


Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded header.
Oct 10 '05 #13
Default User escreveu:
Josce wrote:


Talking about switch statements in general:
I would have liked the possibility to write 1..5 meaning 1,2,3,4 or 5
like:

switch (n)
{
case 1..3:
// do thing A
break;
case 4:
// do thing B
break;
default:
// do thing C
}


I would tend to agree. My guess would be that they didn't want to change
the same construct too much from the original C version. But that's just
a guess, you'd have to ask the committee.

My guess is that for this minimum comfort we get a can of worms in the
parser design. . .
Oct 12 '05 #14
>>> switch (n)
{
case 1..3:
// do thing A
break;
case 4:
// do thing B
break;
default:
// do thing C
}

I would tend to agree. My guess would be that they didn't want to change
the same construct too much from the original C version. But that's just
a guess, you'd have to ask the committee.

My guess is that for this minimum comfort we get a can of worms in the
parser design. . .


It is a "minimum comfort" if the range is 1..3, but not if it is 1005..11004
(you'd not like to list them as individual cases,would you). Yes, I know you
can do it with an "if" instead, but if/elseif/elseif/elseif... is far less
clear to the person validating the code.

Personally, I'd like to see the "select first/select all" I mentioned before
(with the ranges).
--
Steve
Oct 12 '05 #15
On Tue, 11 Oct 2005 15:21:58 -0400, "Steve Fábián"
<ES******@BellAtlantic.net> wrote:
switch (n)
{
case 1..3:
// do thing A
break;
case 4:
// do thing B
break;
default:
// do thing C
}
I would tend to agree. My guess would be that they didn't want to change
the same construct too much from the original C version. But that's just
a guess, you'd have to ask the committee.

My guess is that for this minimum comfort we get a can of worms in the
parser design. . .


It is a "minimum comfort" if the range is 1..3, but not if it is 1005..11004
(you'd not like to list them as individual cases,would you). Yes, I know you
can do it with an "if" instead, but if/elseif/elseif/elseif... is far less
clear to the person validating the code.

Personally, I'd like to see the "select first/select all" I mentioned before
(with the ranges).


The problem is that you can't use 1..3 or 1...3 because then the way C
parsers work it will look like 1. which is a floating point number.
In GCC's case-range extension it has to look like 1 ... 3

You can always use a combination of switch/if/else, or if it all gets
a bit too hairy, use a jump table.

JimS
Oct 12 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Johannes B. Ullrich | last post by:
--=-WKgoK98ejo9BZyGYc3N/ Content-Type: text/plain Content-Transfer-Encoding: quoted-printable I am having problems with MySQL 4.0.12 on RedHat Advanced Server 2.1 using a dual Xeon with 8...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
19
by: Christopher Benson-Manica | last post by:
On a note related to my question about logical XOR, I'm trying to think of a non-obfuscated way to condense the following code block (which, as usual, I did not write): switch( int_val_1 ) {...
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
6
by: jao | last post by:
My company has a product in beta which uses Postgres 7.4.3. We expect to have a code freeze for our 1.0 product in March 2005. I'd really like to use Postgres 8.x in our 1.0 product. We're...
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...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...
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
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...

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.