Answer to Question #27910 in C++ for GULNAZ

Question #27910
How to make calculator in c++?
1
Expert's answer
2013-04-09T08:30:55-0400
// These are called headers. These things are wrote to provide the many different functions available to
// you such as std::cout, std::endl, std::string, and many other different ones. You can probably go to
// google and find the different commands written for you in these libraries if you'd like, but these are
// the only required ones to make this very simple calculator.
#include <iostream>
#include <string>

using namespace std;

int main()
{
// Doubles will make an integer value, but increase the number of bytes available. This will allow
// the client to use bigger numbers, if they wish of course. These also can become decimals when there
// is a remainder.
double number1;
double number2;
double total;

// Integers are values that are either negative or positive. You can use signed or unsigned before
// declaring the integer to state if they are allowed to be a negative number or not, but we won't
// get into that right now.
int current_operation;
int keep_running;

// Booleans are very simple. True or False? We will create this one boolean for the simple sake of
// teaching loops and the such, and just to make our program exitable in general.
bool programRunning = true;

// If you do not declare if you are checking if the statement is true or false, and you are not using
// the negation operator, then it will automatically assume the following statement is true. Below is
// an example of what this comment actually means...
//
// while(programRunning) returns true
// while(programRunning == true) returns true
// while(programRunning != false) returns true
//
// while(!programRunning) returns false
// while(programRunning == false) returns false
// while(programRunning != true) returns false
//
// Hopefully this clarified what I was trying to say :-).
while(programRunning)
{
cout << "Welcome to my very first program!" << endl;
cout << "First, we need to declare two variables and preform an mathematical operation to determine what the total is!" << endl;
cout << endl;
cout << "First value: ";
cin >> number1; // NOTE: When using cin >> to recieve input, it will automatically create a new line for you.
cout << "Second value: ";
cin >> number2;
cout << endl;
cout << "Great! Now all we have to do is declare which mathematical operation you would like to use!" << endl;
cout << endl;
cout << "Type 1 if you would like to add the two numbers." << endl;
cout << "Type 2 if you would like to subtract the two numbers." << endl;
cout << "Type 3 if you would like to multiply the two numbers." << endl;
cout << "Type 4 if you would like to divide the two numbers." << endl;
cout << endl;
cout << "Input: ";

cin >> current_operation;

cout << endl; // NOTE: This is used to make it look all pretty! Don't think it's random :-P.

if(current_operation == 1)
{
total = number1 + number2;

cout << "Your total is " << total << "!" << endl;
cout << endl;
cout << "Type 1 if you would like to find another answer." << endl;
cout << "Type 2 to exit the program." << endl;
cout << endl;
cout << "Input: ";

cin >> keep_running;

if(keep_running == 1)
{
system("CLS");
}

if(keep_running == 2)
{
programRunning = false;
}
}

if(current_operation == 2)
{
total = number1 - number2;

cout << "Your total is " << total << "!" << endl;
cout << endl;
cout << "Type 1 if you would like to find another answer." << endl;
cout << "Type 2 to exit the program." << endl;
cout << endl;
cout << "Input: ";

cin >> keep_running;

if(keep_running == 1)
{
system("CLS");
}

if(keep_running == 2)
{
programRunning = false;
}
}

if(current_operation == 3)
{
total = number1 * number2;

cout << "Your total is " << total << "!" << endl;
cout << endl;
cout << "Type 1 if you would like to find another answer." << endl;
cout << "Type 2 to exit the program." << endl;
cout << endl;
cout << "Input: ";

cin >> keep_running;

if(keep_running == 1)
{
system("CLS");
}

if(keep_running == 2)
{
programRunning = false;
}
}

if(current_operation == 4)
{
total = number1 / number2;

cout << "Your total is " << total << "!" << endl;
cout << endl;
cout << "Type 1 if you would like to find another answer." << endl;
cout << "Type 2 to exit the program." << endl;
cout << endl;
cout << "Input: ";

cin >> keep_running;

if(keep_running == 1)
{
system("CLS");
}

if(keep_running == 2)
{
programRunning = false;
}
}
}

// This is the very end of the program. If the program did what it was supposed to do, and checked
// to see if everything exited okay, and there were no errors, then it will return the value of 1.
return 1;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS