检测某个按键

时间:2014-04-29 17:14:28

标签: c++

我的程序当前显示的文本要求您执行哪个操作(1,2,3,4),我希望程序输出某个消息,具体取决于按下哪个键(1,2,3,4)

{ 
    int option;
    cout << "" << endl;
    cout << "Choose an operation to perform." << endl;
    cout << "" << endl;
    cout << "Calculate Determinants of Matrices [Enter 1]" << endl;
    cout << "Calculate Sum of Matrices [Enter 2]" << endl;
    cout << "Calculate Difference between Matrices [Enter 3]" << endl;
    cout << "Calculate Product of Matrices [Enter 4]" << endl;

    cin >> option;

    if (option == 1);
    {
        cout << "Determinant of A = " << endl;
        cout << "Determinant of B = " << endl;
    }

    if (option == 2);
    {

        cout << "Sum = " << endl;
    }

    if (option == 3);
    {
        cout << "Difference = " << endl;
    }

    if (option == 4);
    {
        cout << "Product = " << endl;
    }


}

3 个答案:

答案 0 :(得分:2)

删除所有不必要的;

if (option == 1);
                ^

P.S。:@ThomasMatthews评论说,最好在这里使用switch

switch (option)
{
case 1:
    cout << "Determinant of A = " << endl;
    cout << "Determinant of B = " << endl;
    break;
case 2:
    ...
case 3:
    ...
case 4:
    ...
default:
    break;
}

答案 1 :(得分:1)

我建议您使用switch语句(我发现if-else-if梯子不可读。)

switch (option)
{
    case 1:
        cout << "Determinant of A = " << endl;
        cout << "Determinant of B = " << endl;
        break;

    case 2:
        cout << "Sum = " << endl;
        break;

    case 3:
        cout << "Difference = " << endl;
        break;

    case 4:
        cout << "Product = " << endl;
        break;

    default:
        cout << "Unknown selection." << endl;
        break;
}

更高级的技术是使用查找表或std::map以及函数指针或函数对象。

编辑1:功能对象的地图
这里的想法是根据从用户收到的号码查找要执行的功能。

我们拥有的工具是指向函数或函数对象(仿函数)和std::map的指针。

让我们定义Functor:

struct Operation
{
  virtual void operator() (void) = 0;  // Yes, a void parameter is my style.
};

我们已经定义了一个operator()的基类。这允许我们将类视为函数调用。 virtual ... = 0;表示这是一个接口函数,所有后代都必须实现它。

接下来,我们将定义一个后代类进行求和。其他选项将具有类似的结构。

struct Sum_Operation : public Operation
{
  void operator() (void)
  {
    cout << "Accessing Summing operation.\n";
  }
};

让我们向前宣布其他行动:

struct Determinant_Operation;
struct Difference_Operation;
struct Product_Operation;

定义并初始化地图:

typedef std::map<unsigned int, Operation*> Operation_Container;

//...
Operation_Container menu1;
//...
menu1[1] = new Determinant_Operation;
menu1[2] = new Sum_Operation;
menu1[3] = new Difference_Operation;
menu1[4] = new Product_Operation;

// Executing the map function:
Operation_Container::iterator iter;
iter = menu1.find(option);
if (iter != menu1.end())
{
    // Execute the function
    (iter->second)();
}

我更喜欢数组,因为可以在不更改查找功能的情况下更新或修改数组。

答案 2 :(得分:0)

尝试 else if 并删除条件语句

之后使用的所有;
 if (option == 1)
    {
        cout << "Determinant of A = " << endl;
        cout << "Determinant of B = " << endl;
    }

   else if (option == 2)
    {

        cout << "Sum = " << endl;
    }

   else if (option == 3)
    {
        cout << "Difference = " << endl;
    }

  else  if (option == 4)
    {
        cout << "Product = " << endl;
    }

或使用切换案例

switch (option)
{
case 1:
        cout << "Determinant of A = " << endl;
        cout << "Determinant of B = " << endl;
        break;
case 2: cout << "Sum = " << endl;
        break;
case 3: cout << "Difference = " << endl;
        break;
case 4: cout << "Product = " << endl;
        break;
default:
        break;
}