I encounter troubles while doing my homework when my professor ask us to use structure to print out the division in a company that has the highest sales. I'd read articles about writing a structure, but still don't quite know how to set it up. Here's a link to my homework for reference:
http://toolkit.cs.ohlone.edu/~tt/inst/ohlone/public/jond/assignment.cs102fall18.17.html And below is my current code, there's many errors in it...
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct DIV {
float sale;
string name;
};
void populate_div_sales(DIV[], string[], int);
int findHighest (DIV[], int); // this function will now return the index for
// the highest division sales
void print_result(DIV[], string[], int); // displays result based on the index of
// highest division sales
//*************************************************
//************ main *******************************
//*************************************************
main ()
{
int top_div_index = 0; // will be assigned the index of the top division sales
DIV div_info[4].name; // array of type DIV holding the divisions' struct info
DIV div_sales[4].sale; // array of type DIV holding the sales of each divisions
// populate div_info array
div_info[0].name= "Northeast";
div_info[1].name= "Southeast";
div_info[2].name= "Northwest";
div_info[3].name= "Southwest";
populate_div_sales(DIV[], 4);
// leave debug statement in final product
cout << "debug print for array div_sales_array" << endl;
for (int i=0; i<4; i++) {
cout << div_sales[i] << endl;
}
top_div_index = findHighest(div_sales, top_div_index); //will no longer print the result
cout << "Deepa Sarker , online" << endl;
cout << "**********************" << endl;
cout << "sales of the four divisions of the company" << endl;
// leave debug statement in final product
cout << "debug for top_div_index: " << top_div_index << endl;
print_result(DIV[], top_div_index);
system ("pause");
return 0;
}
//************ subroutine definitions below *******************
//*************************************************
//************ populate_div_sales *****************
//*************************************************
// The params for the function definition are given to help get started
// - note the f_ preceding variable names. Use
// this convention to help relate and contrast both the calling and the
// receiving variables.
void populate_div_sales(DIV f_div_info[], int size)
{
float s;
cout << "Enter Division sales for all 4 department" << endl;
for(int i=0; i<size;i++)
{
while(true)
{
cout << i+1 <<": ";
cin >> s;
if(s >=0)
{
f_ div_info[i].sale =s;
break;
}
else
{
cout << "Number must be higher than 0" << endl;
}
}
}
}
//*************************************************
//************ findHighest ************************
//*************************************************
int findHighest (float DIV f_div_sales[], int top_div_index)
{
// Use a 'for' loop to help determine the array element with highest sales and
// saves the winner's index to save_index
float greatestSalesAmount = 0;
int save_index = 0;
for(int i = 0; i<top_div_index; i++){
if(DIV f_div_sales[i] > greatestSalesAmount){
greatestSalesAmount = DIV f_div_sales[i];
save_index=i;
}
}
return save_index;
}
//*************************************************
//************ print_result ***********************
//*************************************************
void print_result(float DIV[], int index)
{
// Be sure to include the division name and quarterly Sales in the final display
cout << "The div with the most sales is " << endl;
cout << f_div_info[index] << " with $" << endl;
cout <<f_div_sales[index] << " in sales.\n" << "\n"<< endl;
}