0

How to ask the user to input two numbers, and after that show these options:

sum,
average,
maximum,
minimum,
exit.

Then due to the user choice, the program shows the answer, and this process continues until the user input number 5.

My code does not work properly. Here is the code:

#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;
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
Rahmat
  • 1
  • 1
  • I think you're confusing variable names with string values. You might want a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jan 14 '21 at 08:35

1 Answers1

0

So you want to calculator which should end based on the user choice? If So You can wrap the Function with a While loop and Keep a set a bool based on the user input. Like :

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;
    }
}

If you have any doubts check this out https://learn.microsoft.com/en-us/cpp/get-started/tutorial-console-cpp?view=msvc-160

MMS27
  • 26
  • 2