Making a Simple Program

I make a simple program using all that we have learnt up to now, plus something extra.

A Simple Program

Here’s the Problem:

Suppose that you have been given an assignment to do a lot of calculations at once. However, you do not have a calculator to do the job. Your boss tells you to simply make one in your PC, given your expertise in the subject. You now have the abominable job of making a calculator that you will only be using once.

Some Prerequisites

Okay, so here is how I think I will be writing the program for this:

• Make a starting menu, giving me all the options
• For each keybutton pressed, it should give me options on how to continue my calculations, or to rather clear it all to zero.

Functions, are, well, this:

Functions allow to structure programs in segments of code to perform individual tasks. -CPlusPlus

Said in a simple way, functions are operations that, if given the proper parameters, you can call at any point of the program to do a certain task. You can think of it as a shortcut to lengthy programs. Here’s why I think it helps:

So let’s just assume that this calculator that I will be doing will have just addition.

Then the code should look something like this, right?:

#include <iostream>
using namespace std;
int main()
{
/* You'll know what I'm doing in a short while. Oh, and BTW, this is a thing. You can declare similar 		variables together with a comma*/
{	cout<<"Specify the two numbers to add:"<<endl;
cin>>a>>b;
cout<<a+b;
cout<<"Do you want to exit?If yes, press 1. If not, press 0"<<endl;
}
return 0;
}


Right?

But we can reach a greater level of efficiency with functions. This was a simple program; we can cheat around and use while loops. I doubt we’d be able to do that with many others.

A function is written like this:

data_type function_name(data-type-of-parameter); //This is the declaration section of it
function_name(parameter)
{
//Some code
}


Therefore, we should be able to manipulate the program by making it more like this:

#include <iostream>
using namespace std;
void menu(int); //Declaring a function that won't exactly return anything, and takes an int for a parameter
int param;
int main()
{
param = 0;

do{
cout<<"Do you want to exit? If yes, press 1, otherwise, press 0";
cin>>param;
}while(param==0);
return 0;
}
{
int a,b;
if(param==0)
{	cout<<"Specify the two numbers to add:"<<endl;
cin>>a>>b;
cout<<a+b<<endl;
}
else if(param==1)
{
cout<<"Thank you for using our calculator"; //Obligatory useless message
}
else cout<<"Invalid request"<<endl;
}


Believe me when I say this has a difference on how good the program looks.

So, let’s get started on the whole program, shall we?

Here is the code:

#include <iostream>
using namespace std;
int param;
int main()
{

while(param!=0)
return 0;
}
{
float a,b;
if(param==1)
{	cout<<"Specify the two numbers to add:"<<endl;
cin>>a>>b;
cout<<a+b<<endl;
}
else if(param==0)
{
cout<<"Thank you for using our calculator";
}
else if(param==2)
{
cout<<"Specify the two numbers to subtract:";
cin>>a>>b;
cout<<a-b;
}
else if(param==3)
{
cout<<"Specify the two numbers to multiply:";
cin>>a>>b;
cout<<a*b;
}
else if(param==4)
{
cout<<"Specify the two numbers to divide:";
cin>>a>>b;
cout<<a/b;
}
else cout<<"Invalid request"<<endl;
}