开关盒故障 [英] switch case failure
问题描述
以下是我的程序的摘录,我将其分离出来并将
编译为自己的程序。
问题在于一个案例测试为真,所有案例都在下面
它也会执行。因此,如果用户输入4,将执行来自
情况4-9的语句。
(我用cout替换函数调用进行测试。)
我正在使用Win2k,Open Watcom IDE v 1.3。
这已经在程序的几个地方进行了测试。
(ps - 它按原样编译,它也可以与所有
注释掉的库完好无损 - 无论哪种方式都出现相同的错误)
// #include< stdio.h>
#include< stdlib.h>
#include< iostream.h> //而不是使用命名空间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<< "创建或编辑输入文件.......... 1 \ n" ;;
cout<< "计算器。没有。阶段................ 2 \ n" ;;
cout<< "数据图.......................... 3 \ n" ;;
cout<< " RR与阶段的数量............... 4 \ n" ;;
cout<< " McCabe Thiele情节................. 5 \ n" ;;
cout<< "打印输入文件............ 6 \ n" ;;
cout<< "托盘组合物的打印输出.7 \ n" ;;
cout<< " vle数据的打印输出........... 8 \ n" ;;
cout<< "退出............................... 9 \ n" ;;
cout<< ; " ********** * \ n \ n" ;;
cout<< MENU:" ;;
cin>> menu_select_int;
开关(menu_select_int)
{
案例1:
cout<< \\\
going to input data function \\\
;
cout<< 案例1;
cin.ignore();
案例2:
cout<< \ ingo to CNOS function\\\
;
cout<< 案例2;
cin.ignore();
案例3:
cout<< \ ingo to DP function\\\
;
cout<< 案例3;
cin.ignore();
案例4:
cout<< \\ tonging to RR vs function\\\
;
cout<< 案例4;
cin.ignore();
案例5:
cout<< \\\
going to McTh function\\\
;
cout<< 案例5;
cin.ignore();
案例6:
cout<< \ ninging to PIF function\\\
;
cout<< 案例6;
cin.ignore();
案例7:
cout<< \\\
going to printout function\\\
;
cout<< 案例7;
cin.ignore();
案例8:
cout<< \ ingo to PO VLE function\\\
;
cout<< 案例8;
cin.ignore();
案例9:
cout<< \\\
going to AYS - end function\\\
;
cout<< case 9;
cin.ignore();
} //结束开关盒
} while(menu_select_int!= 9) ;
cin.ignore();
返回(0);
}
YitzHandel写道:以下是我程序的摘录,我是分离出来并编译为自己的程序。
问题是,只要案例测试成立,下面的所有案例都会执行。因此,如果用户输入4,将执行来自案例4-9的语句。
(我用cout替换函数调用进行测试。)
我我正在使用Win2k,Open Watcom IDE v 1.3。
这已经在程序的几个地方进行了测试。
(ps - 它按原样编译,它也很好地编译所有
注释掉的库 - 无论哪种方式都出现错误)
//#include< stdio.h>
#include< stdlib.h>
#include< iostream.h> //而不是使用命名空间std
// #include< string.hpp>
// #include< fstream.h>
// #include< istream>
// #include" Fcopy.h"
// #include< ctime>
// #include< math.h>
int main()
{menu_select_int = -1;
做
{
系统(CLS);
cout<<" ; ********************** MENU ********************* ** \ n ;;
cout<< "创建或编辑输入文件.......... 1 \ n" ;;
cout<< "计算器。没有。阶段................ 2 \ n" ;;
cout<< "数据图.......................... 3 \ n" ;;
cout<< " RR与阶段数相对............... 4 \ n" ;;
cout<< " McCabe Thiele情节................. 5 \ n" ;;
cout<< "打印输入文件............ 6 \ n" ;;
cout<< "托盘组合物的打印输出.7 \ n" ;;
cout<< " vle数据的打印输出........... 8 \ n" ;;
cout<< "退出............................... 9 \ n" ;;
cout<< " ********** * \ n \ n" ;;
cout<< MENU:" ;;
cin>> menu_select_int;
开关(menu_select_int)
{/>
案例1:
cout<< 输入数据函数\ n;&#;> cout<< case 1;
cin.ignore();
添加break在每个案例之后你都不想要
来说明下一个案例...
-David
优异。有效。谢谢。
YitzHandel写道:以下是我程序的摘录,我把它分开并编译成自己的程序。
问题是,一旦案例测试成立,下面的所有案例都会执行。因此,如果用户输入4,将执行来自案例4-9的陈述。
[snip]
你需要在每个案例body之后插入break语句。默认情况下
(不幸的是),switch语句会失效。这是一个例子:
开关(some_int)
{
案例1:
cout<< 案例1 <<结束;
休息;
案例2:
cout<< 案例2 <<结束;
休息;
默认值:
cout<< 默认情况 <<结束;
}
干杯! --M
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);
}
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
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
这篇关于开关盒故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!