-2

以下程序在计算选择后终止,没有进一步发生。所以我的问题是我在做什么错?请有人检查这个程序并通知我我的错误。这个程序是一个计算器,它要求用户选择计算方法,即加法,除法或乘法等,然后显示计算后的结果图片:http: //i43.tinypic.com/2hykpjp.png 恕我直言C 编程新手。

main()
{
  // declaration
  int add,sub,mul,div,selection;
  float a,b,c;
  // prompt user to select a method
  cout << "Calculator, which performs addition,subtraction,multiplication and division: add,sub,mul,div";
  cout << "Please enter your selection (for example: mul): ";
  cin >> selection;
  if (selection = add)
  {
             // prompt user to enter values
             cout << "Please enter first value: ";
             cin >> a;
             cout << "Please enter second value: ";
             cin >> b;
             // calculations
             c = a + b;
             // result
             cout << "Answer: " << c;
  }
  if (selection == sub)
  {
             // prompt user to enter values
             cout << "Please enter first value: ";
             cin >> a;
             cout << "Please enter second value: ";
             cin >> b;
             // calculations
             c = a - b;
             // result
             cout << "Answer: " << c;
  }
  if (selection == mul)
  {
             // prompt user to enter values
             cout << "Please enter first value: ";
             cin >> a;
             cout << "Please enter second value: ";
             cin >> b;
             // calculations
             c = a * b;
             // result
             cout << "Answer: " << c;
  }
  if (selection == div)
  {
             // prompt user to enter values
             cout << "Please enter first value: ";
             cin >> a;
             cout << "Please enter second value: ";
             cin >> b;
             // calculations
             c = a / b;
             // result
             cout << "Answer: " << c;
  }
}
4

3 回答 3

2

你还没有在这里初始化任何东西:

int add,sub,mul,div,selection;

您接受使用的价值,selectioncin >> selection;这些东西应该意味着什么:

if (selection = add)
if (selection == sub)
if (selection == mul)
if (selection == div)

具有存储类的变量auto未初始化为任何默认值。

于 2013-10-25T11:00:09.673 回答
1

以下行中没有初始化。

 int add,sub,mul,div,selection;

用唯一值初始化这些变量(比如 0 表示添加,1 表示子)。另外,相比之下

 selection = add

利用

selection == add

改为使用else if单独的 if。它将提高性能。在您的情况下,它将比较每个条件。它作为示例显示。

  if (selection = add)
     {
                // prompt user to enter values
                cout << "Please enter first value: ";
                cin >> a;
                cout << "Please enter second value: ";
                cin >> b;
                // calculations
                c = a + b;
                // result
                cout << "Answer: " << c;
     }
  else if (selection == sub)
     {
                // prompt user to enter values
                cout << "Please enter first value: ";
                cin >> a;
                cout << "Please enter second value: ";
                cin >> b;
                // calculations
                c = a - b;
                // result
                cout << "Answer: " << c;
     }

//Rest of your program.
于 2013-10-25T11:05:46.437 回答
1

根据您运行程序的示例,您尝试做的是输入字符串,例如add,然后比较它们。您在程序中实际所做的是selectionaddsub等是整数变量,只能用于存储整数。

您必须将selection声明为字符串变量,并将其值与字符串常量进行比较,如下所示:

string selection;

接着:

if (selection == "add")
于 2013-10-25T11:19:49.120 回答