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);
} 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
excellent. that worked. thanks.
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
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
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
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
"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?
>>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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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'
|
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':
|
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 ////////
//////////////////////////////////////////
|
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 );
|
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");
| |
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...
|
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;
|
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?
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |