Answers for "c++ calculator script"

C++
0

c++ calculator script

#include <iostream>

using namespace std;

int main()
{
cout << "Welcome to the Calculator, write :cancel: to cancel the calculator\n";
 string cancel;

 while(cancel != "cancel")
 {
     int number1 = 0;
     int number2 = 0;
     string op;

     cout << "please enter your first number!\n";

     cin >> number1;

     cout << "please enter your second number!\n";

     cin >> number2; 

     cout << "please enter a operator!\n";
     cout << "( *, / , + , - )";

     cin >> op;
    
    if(op == "*")
    {
        number1 = number1 * number2;
        cout << number1; 
        number1 = 0;
        number2 = 0;
    }

    if(op == "/")
    {
        number1 = number1 / number2;
        cout << number1;
        number1 = 0;
        number2 = 0;
    }

    if(op == "+")
    {
        number1 = number1 + number2; 
        cout << number1;
        number1 = 0;
        number2 = 0;
    }

    if(op == "-")
    {
        number1 = number1 - number2;
        cout << number1; 
        number1 = 0; 
        number2 = 0;
    }
    
    cout << "Write cancel to cancel\n";
    cout << "write continue to resume with the calculator!\n";
    
    cin >> cancel;

 }
  
  
}
//powered by my coding server: https://discord.gg/GUgnYHJV
Posted by: Guest on March-01-2021
0

how to build a calculator using c++

#include <iostream>

using namespace std;

int main()
{
int num1;
int num2;
char op;
  
  cout << "Enter a number:" << endl;
  cin >> num1; //takes input
  
  cout << "Enter another number:" << endl; //prints some prompt
  cin >> num2;
  
  cout << "Enter a operator:" << endl; //prints some prompt
  cin >> op;
  
  if(op == '+')
  {
  cout << "Result = " << num1 + num2 << endl;
  }else if(op == '-'){
  cout << "Result = " << num1 - num2 << endl;
  }else if(op == '*'){
  cout << "Result = " << num1 * num2 << endl;
  }else if(op == '/'){
  cout << "Result = " << num1 / num2 << endl;
  }
  
  
}
Posted by: Guest on July-24-2020

Browse Popular Code Answers by Language