Compiling my Program problems.

When I tried to compile this, I seem to have alot of issues, but the issue I want to take care of seems to start with my function prototypes. I just can't seem to pinpoint the issue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <string>

using namespace std;

class profit{
  public:
        int first;
        int second;
        int third;
        int fourth;
        int total;
        double average;
        string name;
};

void readin(profit[], int);
void printArray(profit[], int);
void total(profit[], int);
void average(profit[], int);
void profitHigh(profit[], int);
void profit(profit[], int);
void lostprofit(profit[], int);
void even(profit[], int);

int main()
{
    int n;
    profit company[30];

    cout << "Enter amount of items to be analyzed" << endl;
    cin >> n;

    readin(company,n);
    printArray(company,n);
    total(company,n);
    average(company,n);
    profitHigh(company,n);
    profit(company,n);
    lostprofit(company,n);
    even(company,n);

    return 0;
}

void readin(profit arr[],int size)
{
    cout << "Enter the Item Name and the Profit for that quarter" << endl;
    for(j = 0; j < size; j++)
        cin >> arr[j].name >> arr[j].first >> arr[j].second >> arr[j].third >> arr[j].fourth;


}

void printArray(profit arr[],int size)
{
    cout << "Table of Data from item and quarterly sales" << endl;
    for(k = 0; k < size; k++)
        cout << arr[k].name << arr[k].first << arr[k].second << arr[k].third << arr[k].fourth;

   
}

void total(profit arr[],int size)
{

    for(int l = 0; l < size; l++)
    {
        arr[l].total = arr[l].first + arr[l].second + arr[l].third + arr[l].fourth
    }

   
}

void average(profit arr[],int size)
{

    for(a = 0; a < size; a++)
    {
        arr[a].average = arr[m].total / 4;
    }

    for(m = 0; m < size; m++)
    {
        cout << arr[m].name << " made $" << arr[m].total << " this year and averaged $" << arr[m].average << endl;
    }

}

void profitHigh(profit arr[],int size)
{
    string id;
    int temp;
    string quarter;

    for(n = 0; n < size; n++)
    {
        if(arr[n].first > temp)
        {
            temp = arr[n].first;
            id = arr[n].name;
        }
        else if(arr[n].second > temp)
        {
            temp = arr[n].second;
            id = arr[n].name;
        }
        else if(arr[n].third > temp)
        {
            temp = arr[n].third;
            id = arr[n].name;
        }
        else if(arr[n[.fourth > temp;
        {
            temp = arr[n].fourth;
            id = arr[n].name;
        }
    }

    for(p = 0; p < size; p++)
    {
        if(temp == arr[p].first)
            quarter = "1st";
        else if(temp == arr[p].second)
            quarter = "2nd";
        else if(temp == arr[p].third)
            quarter = "3rd";
        else if(temp == arr[p].fourth)
            quarter = "4th";

    }

    cout << id << " had the greatest profit of $" << temp << " of the " << quarter << " quarter sales" << endl;

    
}

void profit(profit arr[],int size)
{
    int quart1;
    int quart2;
    int quart3;
    int quart4;

    for(q = 0; q < size; q++)
    {
        if(arr[q].first > 0)
            quart1++;
        if(arr[q].second > 0)
            quart2++;
        if(arr[q].third > 0)
            quart3++;
        if(arr[q].fourth > 0)
            quart4++;
    }
    cout << "Quarter One had " << quart1 << " profitable gains." << endl;
    cout << "Quarter Two had " << quart2 << " profitable gains." << endl;
    cout << "Quarter Three had " << quart3 << " profitable gains." << endl;
    cout << "Quarter Four had " << quart4 << " profitable gains." << endl;

    cout << "/n/n" << endl;

 
}

void lostprofit(profit arr[],int size)
{
    int quart1;
    int quart2;
    int quart3;
    int quart4;

    for(w = 0; w < size; w++)
    {
        if(arr[w].first < 0)
            quart1++;
        if(arr[w].second < 0)
            quart2++;
        if(arr[w].third < 0)
            quart3++;
        if(arr[w].fourth < 0)
            quart4++;
    }
    cout << "Quarter One had " << quart1 << " losses." << endl;
    cout << "Quarter Two had " << quart2 << " losses." << endl;
    cout << "Quarter Three had " << quart3 << " losses." << endl;
    cout << "Quarter Four had " << quart4 << " losses." << endl;

    cout << "/n/n" << endl;

    
}

void even(profit arr[],int size)
{
    int quart1;
    int quart2;
    int quart3;
    int quart4;

    for(x = 0; x < size; x++)
    {
        if(arr[x].first < 0)
            quart1++;
        if(arr[x].second < 0)
            quart2++;
        if(arr[x].third < 0)
            quart3++;
        if(arr[x].fourth < 0)
            quart4++;
    }
    cout << "Quarter One had " << quart1 << " items that broke even." << endl;
    cout << "Quarter Two had " << quart2 << " items that broke even." << endl;
    cout << "Quarter Three had " << quart3 << " items that broke even." << endl;
    cout << "Quarter Four had " << quart4 << " items that broke even." << endl;

    cout << "/n/n" << endl;


}


Did a tiny edit (types) but still have questionable issues.
Last edited on
A few problems with this code just by looking.
1)Why do you have your functions out side of a class they should be public and your variables should be private.

2)Why do some of your void functions have a return if they are not returning nothing.

3)void total and void average should be int or double (cast the int to a double) and should return the result and print the result in the main.

4)Your main function has a input for the array but no validation for it. You don't want people entering wrong data like letter or doubles. Also the user should not go over the required amount.

edit
5)You cannot start a object as a array that is why you set a constructor in the class to accept a number to set that as the number for the array or send the whole array after created into the object.
Last edited on
1) I'm not sure what you meant by this. Our professor just only touched into doing "Class" and if I am doing anything wrong please point that out.

2)It was a force of habit. I have edited those out.

3)Our Prof ideally wants us to have a clean "main" so she wants everything done outside of main

4)Do you recommend a way to do this array? I need at least a 30 size array for this program. for now I just want to know why its giving me errors on the prototypes. I have editted it again to change some info
1)Okay then go over the lecture or read this - http://www.cplusplus.com/doc/tutorial/classes/
it should help understand classes. Something I noticed in your class is the name since you have a function of the same name and when inserted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class profit{
	private:
		int first;
		int second;
		int third;
		int fourth;
		int total;
		double average;
		string name;
	public:
		profit();//An appropriate constructor
		void readin(profit[], int);
		void printArray(profit[], int);
		void total(profit[], int);
		void average(profit[], int);
		void profitHigh(profit[], int);
		void profit(profit[], int);//Why name your function the same as your class
		//^It will take it as the constructor and will give error for overloading.
		void lostprofit(profit[], int);
		void even(profit[], int);
};


2)Here is some reading on functions - http://www.cplusplus.com/doc/tutorial/functions/
should also help you out.

3)Well have the functions print out the result too. Also arrays can't be objects so array[a].something is wrong. Read this most importantly the arrays as parameters - http://www.cplusplus.com/doc/tutorial/arrays/
should be really useful for you.

4)read the article for arrays ^. What you can do is maybe a constructor in the class that initializes the array and instead of having the main insert the stuff do it in the constructor. Also if you touched upon the topic of global have the number for size only if it needs to be 30 if it can be lower have it in the main like

1
2
3
4
5
6
7
8
9
int main()
{
    //profit is the class and company, company2, and company3 are the objects.
    profit company(25);//25 would be the max number of items the array will hold
    profit company2();//default constructor of max 30 items.
    profit company3(35);//maybe you want more than 30 items in the array.

    return 0;
}


Thank you very much for your post. I will go through it and I will give results asap.
Topic archived. No new replies allowed.