-
[C++] Simple calculator
Got bored, made a simple calculator in C++
Not really much functionality right now, as its impractical to use instead of the Microsoft calculator.
Code:
#include <iostream>
#include <string.h>
using namespace std;
//Simple Calculator
//By Flareboy323
string Entry;
int m_num_1;
int m_num_2;
int a_num_1;
int a_num_2;
int d_num_1;
int d_num_2;
int s_num_1;
int s_num_2;
int main()
{
cout << "Basic C++ Calculator by Flareboy323.\n";
cout << "What would you like to do? Enter /help for a list of commands, or /exit to exit.\n\n";
cin >> Entry;
if (Entry == "/help")
{
cout << "Type -m for multiplication, -a for addition, -s for subtraction, and -d for division" << endl << endl;
cin >> Entry;
}
if (Entry == "-m")
{
cout << "Multiplication Selected. Enter the first number you would like to multiply.\n";
cin >> m_num_1;
cout << "Now enter the second number you would like to multiply.\n";
cin >> m_num_2;
cout << m_num_1 << " times " << m_num_2 << " equals " << m_num_1 * m_num_2 << endl;
cin >> Entry;
}
if (Entry == "-a")
{
cout << "Addition Selected. Enter the first number you would like to add.\n";
cin >> a_num_1;
cout << "Now enter the second number you would like to multiply.\n";
cin >> a_num_2;
cout << a_num_1 << " plus " << a_num_2 << " equals " << a_num_1 + a_num_2 << endl;
cin >> Entry;
}
if (Entry == "-d")
{
cout << "Division Selected. Enter the first number you would like to divide.\n";
cin >> d_num_1;
cout << "Now enter the second number you would like to divide.\n";
cin >> d_num_2;
cout << d_num_1 << " devided by " << d_num_2 << " equals " << d_num_1/d_num_2 << endl;
cin >> Entry;
}
if (Entry == "-s")
{
cout << "Subtraction Selected. Enter the first number you would like to subtract.\n";
cin >> s_num_1;
cout << "Now enter the second number you would like to divide.\n";
cin >> s_num_2;
cout << s_num_1 << " minus " << s_num_2 << " equals " << s_num_1/s_num_2 << endl;
cin >> Entry;
}
}
Also, bummed it down to make it as short as possible (Just for fun):
Code:
#include <iostream>
#include <string.h>
using namespace std;
string Entry;
int m1;
int m2;
int a1;
int a2;
int d1;
int d2;
int s1;
int s2;
int main()
{
cin >> Entry;
if (Entry == "/help")
{
cout << "-m = multiplication, -a = addition, -s = subtraction, -d = division" << endl << endl;
cin >> Entry;
}
if (Entry == "-m")
{
cout << "Enter first number.\n";
cin >> m1;
cout << "Enter second number.\n";
cin >> m2;
cout << m1 << " * " << m2 << " = " << m1 * m2 << endl;
cin >> Entry;
}
if (Entry == "-a")
{
cout << "Enter first number.\n";
cin >> a1;
cout << "Enter second number.\n";
cin >> a2;
cout << a1 << " + " << a2 << " = " << a1 + a2 << endl;
cin >> Entry;
}
if (Entry == "-d")
{
cout << "Enter first number.\n";
cin >> d1;
cout << "Enter second number.\n";
cin >> d2;
cout << d1 << " / " << d2 << " = " << d1/d2 << endl;
cin >> Entry;
}
if (Entry == "-s")
{
cout << "Enter first number.\n";
cin >> s1;
cout << "Enter second number.\n";
cin >> s2;
cout << s1 << " - " << s2 << " = " << s1/s2 << endl;
cin >> Entry;
}
}
Enjoy! :)
I'm going to be making a GUI version soon using Qt.