So I tried to read that other thread regarding creating a calculator, and I couldn't see any longer it burnt my eyes so bad. Now I know I used some bad programming practices, but it was all just for simplicities sake on the kiddies that are just learning. If you see any problems with my code, or have any suggestions, I'm more than happy to take constructive criticism.
Anyways, here is the code, and it's commented out quite nicely so you shouldn't need me to explain every bit of code in little sections. If that is required I'd be more than happy to do it, just make a reply here and I'll update the original post.
Code:
// 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 wrote 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;
}
If you would like to modify the code and make it your own, then feel free to do that. Share what you have, and maybe others will learn from your own personal experiences you had!
And again, if you have something to say about the code, then please make it somewhat intelligible. I would be more than happy to change my code if it does not meet the standards of some of the veteran programmers out there (though I think this is about as easy as it gets, so I don't see a problem with it at all).
Enjoy!