In: Computer Science
Design a simple calculator program using C++ which is able to:
1. ADD two decimal numbers
2. MULTIPLY two decimal numbers.
The following features must be incorporated in your
program.
1. Must have an interface for the user to be able to either select
the ADD option or MULTIPLY option or
to EXIT the program.
NOTE: If the user makes a wrong selection, a display must be shown
to inform the user and the user
must be given a choice to select again.
2. All user inputs must be stored in a SINGLE DIMENSION ARRAY
before the results are computed.
3. ALL results must be displayed to TWO decimal places.
4. An option must be available to CONTINUE any one calculation
repeatedly without having to exit the
program.
C. Your program coding may incorporate any of the C program
statements as per requirement but the
following MUST included in your program.
1. system(“cls”)
2. switch break
3. do....while4. void functions
5. if
6. macros
CODE:
#include <iostream>
#include<stdlib.h>
#include<cmath>
#define add(a,b)(a+b)
#define multiply(a, b)(a*b)
using namespace std;
void printResult(double numbers[],double a,char c){
//fucntion to print the result
cout<<numbers[0]<<" "<<c<<"
"<<numbers[1]<<" =
"<<round(a*100.0)/100.0<<endl;
}
int main()
{
//do while loop
do{
system("cls");
double *numbers = new double[2],result;
int choice;
char option;
//asking the user to enter the operation choice
cout<<"\nEnter\n1. ADD two decimal number\n2. MULTIPLY two
decimal numbers\n3. Exit:"<<endl;
cin>>choice;
//asking the user to enter two numbers
cout<<"Enter a number: "<<endl;
cin>>numbers[0];
cout<<"Enter a number: "<<endl;
cin>>numbers[1];
//switch case
switch(choice){
case 1:
//if choice is 1
//the numbers will add
result = add(numbers[0],numbers[1]);
printResult(numbers,result,'+');
break;
case 2:
//case 2
//numbers will be multiplied
result = add(numbers[0],numbers[1]);
//printing the result
printResult(numbers, result, '*');
break;
case 3:
//exiting the code
exit(1);
break;
default:
//default case
cout<<"Invalid Input!"<<endl;
break;
}
//asking the user to continue or not
cout<<"Do you want to continue? (y/n) for
yes/no"<<endl;
cin>>option;
//if user enters n the program ends
if(option == 'n')
break;
}while(1);
return 0;
}
__________________________________________
CODE IMAGES:
_________________________________________
OUTPUT:
_________________________________________
Feel free to ask any questions in the comments section
Thank YOu!