c++ 程序是不是可以在 if/else 语句中包含 switch 语句[关闭]

Posted

技术标签:

【中文标题】c++ 程序是不是可以在 if/else 语句中包含 switch 语句[关闭]【英文标题】:Can a c++ program have an switch statement inside an if/else statement [closed]c++ 程序是否可以在 if/else 语句中包含 switch 语句[关闭] 【发布时间】:2015-06-09 02:10:13 【问题描述】:

我正在尝试在 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 Name\t\t\t: ";
    getline (cin,name);
    cout << right << setw(22) << "\tNumber of days in the hospital\t\t: ";
    cin >> days;
    cout << symbol << endl; 
    cin.ignore();
  

  float get_room ( int days, string & room_type)  
  
      string choice;
      char   ch;
      float  room_charges;
      cout << "\n\n\t\t\tRoom Used\n";
      cout <<     "\t\t\t________\n\n";
      cout << fixed << setprecision (2);
      cout << "\t1- Single room-One bed \t"<< SINGLE_ROOM <<"\n\n";
      cout << "\t2- Double room-Two beds\t"<< DOUBLE_ROOM <<"\n\n";
      cout << "\t3- Ward                \t"<< WARD <<"\n\n";
      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 << "\n\n\t\t\tPhone Access\n";
          cout <<     "\t\t\t________\n\n";
          cout << "\t1- Shared   \t" << SHARED_LINE <<"\n\n";
          cout << "\t2- Dedicated\t"<< DEDICATED_LINE <<"\n\n";
          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 语句,只有少数case 语句。它们需要在switch 中使用。 嗯,if/else 中没有switchif 中有一些 switch case 项目,我认为这是有充分理由的给你一个编译错误。 switch 语句通常以关键字 switch (...)... 开头。具体来说,switch (ch) 在您的 if (ch == '2' || ch == 'D') 之后。如果你正确地执行了switch,你可以将它放在if/else 块内。 【参考方案1】:

这里不需要 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
       

【讨论】:

【参考方案2】:

您尚未使用关键字“开关”。代码应该是这样的

else if(ch == '2' or ch == 'D')
    switch(ch)
    case '2':
        break;
    case 'D':
         phone_charges = (DEDICATED_LINE * days);
         phone_type    = "(Cable)";
         break;
    

我不确定您是否打算将“2”的情况留空,但在每种情况下都没有中断会导致失败。

【讨论】:

这会让它编译,但我怀疑代码“应该”看起来像那样! 当我说“应该”时,我指的是“switch-case”结构。

以上是关于c++ 程序是不是可以在 if/else 语句中包含 switch 语句[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

C++判断语句

3分钟搞定 C++ if else 语句 05

关于java里的IF..ELSE IF..ELSE语句,一定要加ELSE吗?

if/else 语句不起作用 C++ [关闭]

在mysql中用if…else语句判断当天是不是为星期天?

为啥JAVA源码中if else大都省略else,怎样的条件能够省去else?