473,606 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_in t)
{

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_in t != 9);

cin.ignore();
return(0);

}

Oct 6 '05 #1
15 7312

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_in t)
{

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 "unfortunat e". 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 "unfortunat e". 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
(unfortunatel y), switch statements fall through. Here's an example:


It's not "unfortunat e". 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 "unfortunat e". 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

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

Similar topics

0
3126
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 GByte of RAM. I have a database collecting logs. Each day, a new table is created. In order to allow for queries across more than one day, I use 'MERGE'
35
8325
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 for '5'. So in his switch statement, he omits a case for '5':
10
9567
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 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch //////// //////////////////////////////////////////
19
15180
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 ) { case 0: switch( int_val_2 ) { case enum_val_1: return( num1 == num2 ); case enum_val_2: return( num1 <= num2 ); case enum_val_3: return( num1 >= num2 );
7
4052
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 beautiful! void sw(int s) { switch (s) while (0) { case 0: printf("zero\n");
6
3051
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 especially looking forward to the background writer and tablespaces. Is the 8.0 release date known? Suppose 8.0 is released in December or January. Is it a sane thing to put 8.0 in a shipping product within the first two months of its release? I...
11
3147
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;
110
10533
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 case scenario for MyISAM backend? Also is it possible to not to lose data but get them corrupted?
7
3354
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
7981
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
8462
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8320
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5994
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
3952
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...
0
4011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2458
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
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1315
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.