0% found this document useful (0 votes)
42 views5 pages

Functions - Hipolito

The document describes a C++ activity that teaches functions. It includes an introduction to functions and their uses. The program allows the user to input two numbers and an operator to perform basic math operations. Multiple functions are defined and used to prompt the user for input and display outputs. The program runs the math operations three times before concluding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views5 pages

Functions - Hipolito

The document describes a C++ activity that teaches functions. It includes an introduction to functions and their uses. The program allows the user to input two numbers and an operator to perform basic math operations. Multiple functions are defined and used to prompt the user for input and display outputs. The program runs the math operations three times before concluding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CMPE - COMPUTER FUNDAMENTALS AND PROGRAMMING

Shiela Rose Shantalle L. Hipolito BSIE 1 - 4


Activity 3 - Functions Ms. Abby Kate Orique

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.

II. PROGRAM PRINT SCREEN


III. SOURCE CODE

#include <iostream>
#include <string>

using namespace std;

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 sqrt(int num){


return num*num;
}

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\nYou have still have 2 chances to try the operators!"<<endl;


sayPressKey();
cin>>response;

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\nYou have still have 1 chance to try the operators!"<<endl;


sayPressKey();
cin>>response;

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;

cout<<"\n\nLoading to main menu..."<< endl;


return 0;
}
IV. ACTIVITY TAKEAWAYS

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.

You might also like