【问题标题】:Asking the user to input one of the options and depending on the choice, the program shows the answer要求用户输入选项之一,并根据选择,程序显示答案
【发布时间】:2021-01-14 08:30:44
【问题描述】:

如何让用户输入两个数字,然后显示这些选项:

和, 平均的, 最大, 最低限度, 出口。

然后由于用户选择,程序显示答案,这个过程一直持续到用户输入数字5。

我的代码不能正常工作。代码如下:

#include <stdio.h>
#include <iostream>

using namespace std;
    
int main() {
  int x, y, choice;
  char sum, average, max, min;
  cout << "Enter two numbers: " << endl;
  cin >> x >> y;
  cout << "Choose one of these options: \n";
  cout << " sum - average - max - min - exit " << endl;
  choice == sum || average || max || min;
  cin >> choice;
       
  sum == x + y;
  max == x > y || y > x;

  if (choice == sum) {
    cout << "The sum is: " << x + y << endl;
  }

  if (choice == average) {
    cout << "The average is: " << x / y << endl;
  }
        
  if (choice == max&& x > y) {
    cout << "The maximum is: " << x << endl;
  } else 
      cout << " The maximum is: " << y << endl;
        
  if (choice == min&& x < y) {
    cout << "The minimun is: " << x << endl;
  } else 
    cout << " The minimun is: " << y << endl;

  while (true) {
    string choice;
    cin >> choice;
    if (choice == "5") {
      break;
    }
  }
  return 0;
}

【问题讨论】:

  • 我认为您将变量名与字符串值混淆了。您可能需要good book

标签: c++


【解决方案1】:

所以你想根据用户的选择来计算应该结束的计算器?如果是这样的话 您可以使用 While 循环包装函数,并根据用户输入将集合保持为布尔值。喜欢:

bool Terminate = false;
while(!Terminate)
{
    //Do Your Calculations Here


    //Ask for the User Input
    std::string UserChoice = "";
    std::cout << "Do You Want to Exit [ Y/N ]";
    std::cin << UserChoice;
    if(UserChoice == "Y" || USerChoice == "y")
    {
       std::cout << "Ending the Program";
       Terminate = true;
    }
}

如果您有任何疑问,请查看这里 https://docs.microsoft.com/en-us/cpp/get-started/tutorial-console-cpp?view=msvc-160

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2021-10-24
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    相关资源
    最近更新 更多