In: Computer Science
Write a C++ program which consists of several functions besides the main() function.
I've got the below one. Can someone help me improve it absolutely as the requirement? I am not good at user loop and menu function. Can you help me add that? And please help me put main function to the top. When I want to put it to the top, the project can't run. I don't know what's the wrong with that.
// ===================
#include <iostream>
using namespace std;
// ===================
//
==========================================================================
void SumProductDifference(int x, int y, int& sum, int&
product, int& diff){
sum = x + y;
product = x * y;
diff = x - y;
} // Function SumProductDifference ()
// ====================================
// =========================
int power(int a, int b) {
int r = 1;
for (int i = 1; i <= b; i++)
r = r * a;
return r;
} // Function power ()
// ======================
// =====================================
void ProcessCommand(int& x, int& y) {
cout << "Enter two numbers :";
cin >> x >> y;
} // Function ProcessCommand()
// ==============================
// ============
int main() {
int x, y, sum, product, diff;
ProcessCommand(x, y);
SumProductDifference(x, y, sum, product, diff);
cout << "Sum = " << sum << "\nProduct= " <<
product << "\nDifference =" << diff;
cout << "\nPower of " << x << "to " << y
<< " is :" << power(x, y);
return 0;
} // Function main()
// =====================
Hi,
Please find the answer below:
----------------------------------------------------
To bring the main function to the top, we need to add the
function prototypes used in the program
at the top.
//Function prototypes
void SumProductDifference(int , int , int& , int& ,
int& );
int power(int , int );
void ProcessCommand(int& , int& );
If we dont add the prototypes , we will get errors in the project.
error: 'ProcessCommand' was not declared in this scope|
Fixed C++ Program
// ===================
#include <iostream>
using namespace std;
// ===================
//Function prototypes
void SumProductDifference(int, int, int&, int&, int&
);
int power(int, int );
void ProcessCommand(int&, int& );
// Function main()
// =====================
int main()
{
int x, y, sum, product, diff;
int selection;
do
{
cout<<"\n\n ------
Program Menu ------"<<endl;
cout<<"1:
SumProductDifference" << endl;
cout<<"2: Power"
<< endl;
cout<<"3: Quit"
<< endl;
cout<<"Enter your
selection: ";
cin>>selection;
if(selection == 1)
{
ProcessCommand(x, y);
SumProductDifference(x, y, sum, product, diff);
cout << "Sum = " << sum << "\nProduct= " <<
product << "\nDifference =" << diff;
}
else if(selection
==2)
{
ProcessCommand(x, y);
cout << "\nPower of " << x << "to " << y
<< " is :" << power(x, y);
}
else
{
//do nothing - Quit option
}
}
while((selection >= 1) && (selection
<= 2));
return 0;
}
// Function SumProductDifference ()
//
==========================================================================
void SumProductDifference(int x, int y, int& sum, int&
product, int& diff)
{
sum = x + y;
product = x * y;
diff = x - y;
}
// Function power ()
// =========================
int power(int a, int b)
{
int r = 1;
for (int i = 1; i <= b; i++)
r = r * a;
return r;
}
// Function ProcessCommand()
// =====================================
void ProcessCommand(int& x, int& y)
{
cout << "Enter two numbers :";
cin >> x >> y;
}
Sample Output:
------ Program Menu ------
1: SumProductDifference
2: Power
3: Quit
Enter your selection: 1
Enter two numbers :2 3
Sum = 5
Product= 6
Difference =-1
------ Program Menu ------
1: SumProductDifference
2: Power
3: Quit
Enter your selection: 2
Enter two numbers :3 4
Power of 3to 4 is :81
------ Program Menu ------
1: SumProductDifference
2: Power
3: Quit
Enter your selection: 3
Screenshot
------------------------------------------------------
Hope this helps.
Kindly like the solution if it is useful to you.
Let me know if you need more help.
Thanks.