Troubles on Homework

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;
}




Kiara Chern,

While I dig into your code:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

Andy
You have progressed since http://www.cplusplus.com/forum/beginner/244257/


Code in plain text is hard to read. Code tags do help. See http://www.cplusplus.com/articles/jEywvCM9/


The beginning of http://www.cplusplus.com/doc/tutorial/structures/
has an example (essentially):
1
2
3
4
5
6
7
8
9
10
struct Product {
  int weight;
  double price;
};

int main() {
  Product apple;
  apple.weight = 2;
  apple.price = 3.14;
}

That is not really different from:
1
2
3
4
int main() {
  int answer;
  answer = 42;
}

Both apple and answer are objects that hold some value. The (2, 3.14) is just slightly more complex value than the (42).
Both Product and int are typenames.


Did your earlier program have:
1
2
string div_info[4];
float div_sales[4];

If yes, you probably should now have an array of divisions:
DIV divisions[4];


You have declarations:
1
2
3
void populate_div_sales( DIV[], string[], int );
int findHighest( DIV[], int );
void print_result( DIV[], string[], int );

and implementations:
1
2
3
void populate_div_sales( DIV f_div_info[], int size ) { /*code*/ }
int findHighest( float DIV f_div_sales[], int top_div_index ) { /*code*/ }
void print_result( float DIV[], int index ) { /*code*/ }

Are they referring to same functions?

I'll hide the parameter names from the implementations and show "side-by-side":
1
2
3
4
5
6
7
8
void populate_div_sales( DIV[], string[], int );
void populate_div_sales( DIV[], int ) { /*code*/ }

int findHighest( DIV[], int );
int findHighest( float DIV[], int ) { /*code*/ }

void print_result( DIV[], string[], int );
void print_result( float[], int ) { /*code*/ }

Seriously different, are they not?
Hello Kiara Chern,

One of the things you will find when writing program is to be consistent with the way you write different parts.

Starting with the header files and the struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;  // <--- Best not to use.

struct DIV
{
	float sale;
	string name;

	//DIV(std::string nameSent);
};

Since "iomanip" works with "iostream" I like to keep them together followed by "string" and then any other header files you might need. This way if you miss one it is easier to spot.

For the struct and other things I fine that pressing "Enter" and starting the opening curly brace on the next line makes it easier to read and to find the matching closing brace when they line up in the same column. Also proper indenting helps.

Since it first shows up in the struct "double" is the preferred floating point type over "float". The double has better precision than the float and store some numbers better.

Other than that the struct is fine the way it is designed.

Referring back to what keskiverto said the prototypes must match the function definitions. In void populate_div_sales(DIV[], string[], int);. In the prototype you have three parameters, but as keskiverto pointed out the function only has two parameters. Neither of which is an array of "std::strings". Yes string[] says that you are trying to pass an array of strings. Now a "std::string" can be thought of as an array, but it is not. So in this case it would be "std::string" or just "string".

Before I get started on m"main" it is int main(). Leaving off the return value causes an error.

In "main" DIV div_info[4] is all you need. The ".name" causes an error. The type of the variable is "DIV" which is a struct, so each element of the array is a struct with "sales" and "name". The next line where the variable name ends wit ".sales " is not needed. This definition of two arrays only used half the struct for each array. You only need one.

The next for lines put the name in the array of structs. This is OK and works, but I know there is an easier way I am just not sure how to do this with struct because I do not get that deep into structs as I do with classes.

Line 38 in your for loop. This should be "div_info[i].sales" not "div_sales[i]". In your version you are trying to print the whole struct not the part that you need. As a note: for one line of code the {}s are not needed. This also applies to if statements and while loops.

In line 52 the function call only needs the variable name. the type or []s are not needed.

Line 53. It is best to avoid using "system" anything.

In place of "system("pause");" I use this:
1
2
3
4
5
6
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();


For the functions:

It is not necessary, although yo can change the variable names in a function, I removed the "f_" part and just used the same variable names that were defined in "main". Since the scope is different this is OK.

For the last two functions: (float DIV div_info[], int top_div_index). Including "float" causes errors and problems in the function because you are trying to give "div_info" two different types. You need to remove the "float".

Inside the "findHighest" function you have the line if (DIV f_div_sales[i] > greatestSalesAmount). Two problems here first you are trying to redefine the variable with the "DIV". Second the [i] needs to be a constant not variable. So remove the "DIV" and finish accessing the struct with div_sales[i].sale. What you are trying to do is access the element of the array, which is a struct, but not the information in the struct.

The same applies for the " print_result" function". You are accessing an element of the array, but missing the information in the struct.

Now that I have the program able to compile I will have to give it a run and see what happens.

Note: Before you post your code compile it and make sure it has no errors. This way if there are any errors that yo do not understand you can include them in your post and ask for help. To be clear post the entire message not what you think it means. The line numbers and message mean more to others than you might think.

Hope that helps,

Andy
Topic archived. No new replies allowed.