c++程序是否可以在if/else语句中包含switch语句
Can a c++ program have an switch statement inside an if/else statement
我正试图在if/else语句中执行switch语句。它不起作用。我不确定这是否是因为我在switch语句中使用的保留词"break"使我退出了函数。请帮忙!!!
/* Date : March 3, 2015
Date Due : March 23, 2015
Program Name: Hospital Calculator
Description: This program will ask the user to enter several information
related to his/her room floor measurements. After that, it will generate
a bill that will tell the customer the price for the carpet job.
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cctype>
//Constants
// Room Rates
const float SINGLE_ROOM = 525.00,
DOUBLE_ROOM = 325.00,
WARD = 550.00,
//Phone Access Rates
SHARED_LINE = 2.95,
DEDICATED_LINE = 5.95,
//Television Rates
BASIC_CHANNELS = 2.95,
CABLE_CHANNELS = 5.95;
using namespace std;
//Prototypes
void getdata(string &, int &);
float get_room ( int, string &);
float get_phone(int days, string &);
int main()
{
string name,
room_type,
phone_type,
tv_type;
int days;
float room_charges,
phone_charges,
tv_charges;
//The ones below are subfuntions calls that devide each section
//Input section (call)
getdata(name,days);
//room_charges =
//room_charges = get_room(days,room_type);
phone_charges = get_phone(days, phone_type);
cout << phone_charges << endl;
cout << phone_type << endl;
/*tv_charges = get_tv(days, tv_type);
print(name, days, room_charges, phone_charges, tv_charges);
*/
cin.get();
cin.ignore();
return 0;
}
// Input Section
//This function takes the information needed from the user
void getdata(string & name, int & days)
{
string symbol;
symbol.assign (50, '*');
cout << symbol<< endl;
cout << right << setw(22) << "tCustomer's Namettt: ";
getline (cin,name);
cout << right << setw(22) << "tNumber of days in the hospitaltt: ";
cin >> days;
cout << symbol << endl;
cin.ignore();
}
float get_room ( int days, string & room_type)
{
string choice;
char ch;
float room_charges;
cout << "nntttRoom Usedn";
cout << "ttt________nn";
cout << fixed << setprecision (2);
cout << "t1- Single room-One bed t"<< SINGLE_ROOM <<"nn";
cout << "t2- Double room-Two bedst"<< DOUBLE_ROOM <<"nn";
cout << "t3- Ward t"<< WARD <<"nn";
cout << "t Enter Choice 1, 2, or 3 : ";
getline(cin, choice);
cin.ignore();
ch = toupper(choice[0]);
if (ch == '1' or ch == 'S')
{
room_charges = SINGLE_ROOM * days;
room_type = "in a Single Room";
}
else if (ch == '2' or ch == 'D')
{
room_charges = DOUBLE_ROOM * days;
room_type = "in a Double Room";
}
else if (ch == '3' or ch == 'W')
{
room_charges = WARD * days;
room_type = "in a Ward";
}
return room_charges;
}
float get_phone(int days, string & phone_type)
{
string prompt,
choice;
char pr, ch;
float phone_charges;
cout << "Would you like Phone Access (Y/N): ";
cin >> prompt;
pr = toupper(prompt[0]);
if (pr == 'Y')
{
cout << "nntttPhone Accessn";
cout << "ttt________nn";
cout << "t1- Shared t" << SHARED_LINE <<"nn";
cout << "t2- Dedicatedt"<< DEDICATED_LINE <<"nn";
cout << "t Enter Choice 1 or 2: ";
getline(cin, choice);
ch = toupper(choice[0]);
if (ch == '1' or ch == 'S')
{
phone_charges = (SHARED_LINE * days);
phone_type = "(Shared)";
else if (ch == '2' or ch == 'D')
case '2':
case 'D':
{
phone_charges = (DEDICATED_LINE * days);
phone_type = "(Cable)";
break;
}
}
}
else if (pr == 'N')
{
phone_charges = 0.00;
phone_type = "None";
}
cin.ignore();
return phone_charges;
}
这里不需要switch语句。但为了保持现有的功能,这里有一些修复方法。您缺少几个括号,以及switch( ch )
本身。
if (ch == '1' or ch == 'S')
{
phone_charges = (SHARED_LINE * days);
phone_type = "(Shared)";
} //<----- missing this
else if (ch == '2' or ch == 'D')
{
switch( ch ) //<----- missing this
{ //<------and this
case '2':
//currently doing nothing, falls through to case 'D':
//break; <-----add this for the time being?
case 'D':
phone_charges = (DEDICATED_LINE * days);
phone_type = "(Cable)";
break;
}/// <-----and this
}
您没有使用关键字"switch"。代码应该看起来像
else if(ch == '2' or ch == 'D'){
switch(ch){
case '2':
break;
case 'D':
phone_charges = (DEDICATED_LINE * days);
phone_type = "(Cable)";
break;
}
}
我不确定你是否打算把"2"的情况留空,但如果在每种情况下都不休息,就会导致失败。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 我似乎对if/else的基本语句有问题:/
- 如果 else 语句不断循环
- C++ If/Else 语句被跳过
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 如何将 if else 语句重写为 switch 语句
- 在 C++ 中的 if-else if- else 语句期间更改变量
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如果 else 语句使用大写和小写字母
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 多个'if'语句,不带'else'
- 嵌套的 else 语句 c++
- 为什么切换 if else 语句的顺序会导致错误?
- 如何使用 if else 语句
- if/else 语句输出由于加号或减号而未显示正确的消息
- if-else 语句仅按特定顺序工作,我不知道为什么
- "if else"被视为单个语句吗?
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误