这个程序有什么问题,它终止了?

What's wrong with this program, it terminates?

本文关键字:终止 问题 程序 什么      更新时间:2023-10-16

以下程序在计算选择后终止,并且不会再发生任何操作。所以我的问题是我做错了什么?请有人检查此程序并通知我我的错误。该程序是一个计算器,它要求用户选择计算方法,即加法,除法或乘法等,然后在计算后显示结果图: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;
  }
}

您尚未在此处初始化任何内容:

int add,sub,mul,div,selection;

你使用cin >> selection;selection的值,但这些东西应该意味着什么:

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

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

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

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

string selection;

然后:

if (selection == "add")

下一行没有初始化。

 int add,sub,mul,div,selection;

使用唯一值初始化这些变量(例如 0 表示添加,1 表示 sub)。另外,在比较中

 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.