In: Computer Science
Write a program that determines which of the company’s four divisions (Northeast, Southeast, Northwest, and Southwest) had the greatest sales for the quarter. It should include the following two functions, which are called by main.
Rewrite the “getSales” function in the “winning division”program.
◦The function has no return value;
◦The function accepts one reference variable to a double and one string variable as arguments.
◦Your program still needs to call getSalesfour times for each division to get their quarterly sales figures.
This is the code I have so far.
#include <iostream>
#include <string>
using namespace std;
//Prototypes
double getSales(string, double&);
void findHighest(double, double, double, double);
int main()
{
//Variables for each division's sales
double NE_sales, SE_sales, NW_sales, SW_sales;
double s1, s2, s3, s4;
//Getting the values for each division's
sales
NE_sales = getSales("Northeast", s1);
SE_sales = getSales("Southeast", s2);
NW_sales = getSales("Northwest", s3);
SW_sales = getSales("Southwest", s4);
//Finding the highest sales
findHighest(NE_sales, SE_sales, NW_sales,
SW_sales);
system("pause");
return 0;
}
//Getting sales for each division
double getSales(string name, double &salesRef)
{
double sales;
cout << "Please enter the sales for "
<< name << " division: ";
cin >> sales;
//Input validation
while (sales < 0)
{
cout << "Please enter a
positive number for the sales for " << name << "
division: ";
cin >> sales;
}
salesRef = sales;
//Returning sales for this division
return sales;
}
//Finding highest sales
void findHighest(double divNE, double divSE, double divNW, double
divSW)
{
double maxSales;
//Comparing each division
maxSales = max(max(max(divNE, divSE), divNW),
divSW);
cout << endl;
//Outputting result once max is known
if (maxSales == divNE)
cout << "The Northeast
division grossed the most with a total of $" << divNE
<< endl;
else if (maxSales == divSE)
cout << "The Southeast
division grossed the most with a total of $" << divSE
<< endl;
else if (maxSales == divNW)
cout << "THe Northwest
division grossed the most with a total of $" << divNW
<< endl;
else
cout << "The Southwest
division grossed the most with a total of $" << divSW
<< endl;
return;
}
I don't know if I did the reference variable right or
how to make the function have no return value.
Solution:
Code implementation:
#include <iostream>
#include <string>
using namespace std;
//Prototypes
double getSales(string, double&);
void findHighest(double, double, double, double);
int main()
{
//Variables for each division's sales
double s1, s2, s3, s4;
//Getting the values for each division's sales, now s1,s2,s3,s4
will be updated with the values entered by user.
getSales("Northeast", s1);
getSales("Southeast", s2);
getSales("Northwest", s3);
getSales("Southwest", s4);
//Finding the highest sales
findHighest(s1,s2,s3,s4);
//system("pause");
return 0;
}
//Getting sales for each division
double getSales(string name, double &salesRef)
{
double sales;
cout << "Please enter the sales for " << name
<< " division: ";
cin >> sales;
//Input validation
while (sales < 0)
{
cout << "Please enter a positive number for the sales for "
<< name << " division: ";
cin >> sales;
}
cout<<sales<<endl;
salesRef = sales; // return statement not required.
}
//Finding highest sales
void findHighest(double divNE, double divSE, double divNW, double
divSW)
{
double maxSales;
//Comparing each division
maxSales = max(max(max(divNE, divSE), divNW), divSW);
cout << endl;
//Outputting result once max is known
if (maxSales == divNE)
cout << "The Northeast division grossed the most with a total
of $" << divNE << endl;
else if (maxSales == divSE)
cout << "The Southeast division grossed the most with a total
of $" << divSE << endl;
else if (maxSales == divNW)
cout << "THe Northwest division grossed the most with a total
of $" << divNW << endl;
else
cout << "The Southwest division grossed the most with a total
of $" << divSW << endl;
return;
}
Code screenshot:
Code Input and output screenshot: