Thursday, 14 March 2013

C++ calculator program


#include <iostream>

using namespace std;

int main() {

double num1; //This is the variable for the first number the user will enter

char op; //This is the variable which will determine the operator user wants

double num2; //This is the variable for the second number the user will enter

int pick; //This is for the user's choice to quit or restart

double answer; //This is the variable for the answer

cout << "Welcome to the calculator program" << endl;

cout << "Please enter your first number:" << endl;

cin >> num1;

cout << "Please enter the operator you wish to use, choices: +,-,/,*:" << endl;

cin >> op;

cout << "Please enter your second number:" << endl;

cin >> num2;

//Now we have all the variables lets formulate an answer

if(op == '+'){ //So if the user picks '+' we will perform addition

answer = num1 + num2;

cout << num1 << " + " << num2 << " = " << answer << endl; //Here answer is given in the following form

}

if(op == '-'){ //So if the user picks '-' we must subtract

answer = num1 - num2;

cout << num1 << " - " << num2 << " = " << answer << endl; //Here the answer is given for subtraction

}

if(op == '*'){ //So if the user picks '*' we must multiply

answer = num1 * num2;

cout << num1 << " * " << num2 << " = " << answer << endl; //Answer for multiplication

}

if(op == '/'){ //So if the user picks '/' we must divide

answer = num1 / num2;

cout << num1 << " / " << num2 << " = " << answer << endl; //Answer for division

}

// Now our program is finished, we will ask the user to quit or do another calculation

cout << "Press '1' to restart the program, or '0' to quit:";

cin >> pick;

if(pick == 1)

{

main(); //We call the main method, and the program restarts

}

if(pick == 0)

{

return 0; //End

}



return 0;

}

No comments:

Post a Comment