简体   繁体   English

无法创建switch语句

[英]Trouble creating switch statement

I am very new to C++, and I am having trouble completing an assignment. 我是C ++的新手,我在完成作业时遇到了麻烦。 I need to switch this if statement to a switch statement, but I am totally lost on how to do it since the number must be an integer. 我需要将这个if语句切换到switch语句,但由于数字必须是整数,我完全迷失了。 Could someone point me in the right direction? 有人能指出我正确的方向吗?

See below: 见下文:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  //declare named constants and variables
  const double RATE1 = .02;
  const double RATE2 = .05;
  double sales       = 0.0;
  double commission  = 0.0;

  //get input item
  cout << "Enter the sales amount: "; 
  cin >> sales; 

  //calculate commission
  if (sales <= 15000.0)
{
    commission = sales * RATE1;
else
    commission = sales * RATE2;

 }
//end if

//display commission
  cout << fixed << setprecision(2);
  cout << "Commission: $" << commission << endl;

system("pause");
return 0;
}   //end of main function

This is not feasibly possible to do. 这是不可行的。 switch requires integral equality, your code checks for lesser than on a double number. switch需要完全相等,你的代码检查的次数小于双数。

The unfeasible option would be to convert your double to integer with lost precision and than list all 15000 options as case labels. 不可行的选项是将double转换为精度丢失的整数,并将所有15000个选项列为case标签。 Not feasible at all. 根本不可行。

If you use gcc then you can use case ranges: Case Ranges , but this works IMHO for integral data types, I am not sure about doubles. 如果你使用gcc然后你可以使用案例范围: 案例范围 ,但这适用于整数数据类型恕我直言,我不确定双打。

But don't forget, this is only the extension of the compiler. 但不要忘记,这只是编译器的扩展。 I also heard something similar was introduced with C++11, but I am not sure there. 我也听说类似的东西是用C ++ 11引入的,但我不确定。

You can technically do it in this horrible way: 从技术上讲,你可以用这种可怕的方式做到这一点:

//calculate commission
switch (sales <= 15000.0)
{
case true: 
    commission = sales * RATE1;
    break;
default:
    commission = sales * RATE2;
}

But never, ever do this in real code. 但是,永远, 永远做在实际的代码。

As SergeyA pointed out in his answer this is feasible as it is a huge range of cases. 正如SergeyA他的回答中指出的那样,这是可行的,因为它是一个范围很广的案例。

If it absolutely needs to have a switch statement then we can cheat and have a function that returns true or false and then switch on the result of that function 如果它绝对需要一个switch语句,那么我们可以作弊并有一个返回true或false的函数然后打开该函数的结果

bool is_less(double amount)
{
    return amount < 15000.0;
}

//...

switch (is_less(sales))
{
    case true: commission = sales * RATE1; break;
    case false: commission = sales * RATE2; break;
}

You could do something like this, similar to NathanOliver's answer without a new function. 你可以这样做,类似于NathanOliver的答案而没有新的功能。

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  //declare named constants and variables
  const double RATE1 = .02;
  const double RATE2 = .05;
  double sales       = 0.0;
  double commission  = 0.0;

  //get input item
  cout << "Enter the sales amount: "; 
  cin >> sales; 

  //calculate commission

  bool lessthan = sales <= 15000.0;

  switch (lessthan) 
  {
      case true: 
          commission = sales * RATE1;
          break;

      case false: 
        commission = sales * RATE2;
        break;

      default: 
        break;        
  }

//display commission
  cout << fixed << setprecision(2);
  cout << "Commission: $" << commission << endl;

  system("pause");
  return 0;
}   //end of main function`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM