Functions - Hipolito
Functions - Hipolito
I. INTRODUCTION
In this activity, the C++ Functions are utilized to create a simple program and its execution. The function
within a program can vary. In the precious activities, we only have one function which is the main function. We
can have additional functions to perform a code and reuse it as many times as we want. Using this feature, we can
create a program efficiently as it allows us to perform codes with different parameters and overloaded functions.
In the previous activity, the program was about the continuation of BM Helper Introduction which is the
assessment of the current knowledge and grades tracker of the user through the use of the C++ tools. In this
activity, switch tool and the additional functions were employed to create a simple program as a continuation of
the previous activities.
#include <iostream>
#include <string>
void sayPressKey(){
cout<< "Press 'G' + enter to continue."<< endl;
}
void sayInputNum(){
cout<< "\nInput a number:";
}
void sayInputAnotherNum(){
cout<< "\nInput another number:";
}
void sayInputOp(){
cout<< "\nInput an operator:";
}
int main() {
/*Opening for Operators. User should input which operator to choose*/
cout<<"------------------------------------------------------------------------------------------------------------------------"
<< endl;
cout<<" BASIC OPERATIONS " << endl;
cout<<"------------------------------------------------------------------------------------------------------------------------"
<< endl;
cout<<"\nIn this part, you need to input your two numbers and the operator you want to use. Let's
start!"<<endl;
string response;
sayPressKey();
cin>>response;
cout<<"\n------------------------------------------------------------------------------------------------------------------------"
<< endl;
int numOne,numTwo;
char op;
sayInputNum();
cin>> numOne;
sayInputAnotherNum();
cin>> numTwo;
sayInputOp();
cin>> op;
switch(op){
case '+':
cout<<numOne<<"+"<<numTwo<<"="<<numOne+numTwo;
break;
case '-':
cout<<numOne<<"-"<<numTwo<<"="<<numOne-numTwo;
break;
case '/':
cout<<numOne<<"/"<<numTwo<<"="<<numOne/numTwo;
break;
case '*':
cout<<numOne<<"*"<<numTwo<<"="<<numOne*numTwo;
break;
default:
cout<<"Invalid Operator";
break;
}
cout<<"\n------------------------------------------------------------------------------------------------------------------------"
<< endl;
cout<<"\n------------------------------------------------------------------------------------------------------------------------"
<< endl;
sayInputNum();
cin>> numOne;
sayInputAnotherNum();
cin>> numTwo;
sayInputOp();
cin>> op;
switch(op){
case '+':
cout<<numOne<<"+"<<numTwo<<"="<<numOne+numTwo;
break;
case '-':
cout<<numOne<<"-"<<numTwo<<"="<<numOne-numTwo;
break;
case '/':
cout<<numOne<<"/"<<numTwo<<"="<<numOne/numTwo;
break;
case '*':
cout<<numOne<<"*"<<numTwo<<"="<<numOne*numTwo;
break;
default:
cout<<"Invalid Operator";
break;
}
cout<<"\n------------------------------------------------------------------------------------------------------------------------"
<< endl;
cout<<"\n------------------------------------------------------------------------------------------------------------------------"
<< endl;
sayInputNum();
cin>> numOne;
sayInputAnotherNum();
cin>> numTwo;
sayInputOp();
cin>> op;
switch(op){
case '+':
cout<<numOne<<"+"<<numTwo<<"="<<numOne+numTwo;
break;
case '-':
cout<<numOne<<"-"<<numTwo<<"="<<numOne-numTwo;
break;
case '/':
cout<<numOne<<"/"<<numTwo<<"="<<numOne/numTwo;
break;
case '*':
cout<<numOne<<"*"<<numTwo<<"="<<numOne*numTwo;
break;
default:
cout<<"Invalid Operator";
break;
}
cout<<"\n------------------------------------------------------------------------------------------------------------------------"
<< endl;
cout<<"\n\nAnd we're done with your trial for the operators."<<endl;
sayPressKey();
cin>>response;
Implementation of the additional functions for coding makes developing a program smoother. It is such a
convenient tool as it creates a ‘shortcut’ for every code which I use frequently. While performing this activity,
ideas randomly occur in my mind. Fortunately, I was able to present those ideas using the additional functions and
some C++ tools. It took me some time to create a concept, but I gained knowledge while completing the activity.