I just cant do arrays!!

My teacher gave us this assignment.

You are to write a program to compute salaries and bonuses for a company. The plan is that each salesman who exceeds the average sales for a month gets a bonus and the others get only their salaries. You will read from "sales.txt" which will be a file containing salary and sales data for each employee. From this you will compute the average sales value and then produce a report of salaries and bonuses to a file named "bonuses.txt".

Each line of the input file, "sales.txt", will have first and last name for an employee, salary for the employee and monthly sales for the employee. You can create arrays of size 100 to prepare for reading the data.

After computing the average sales, then you can prepare the report. The bonus for those exceeding the average will be 5% of the amount they have exceeded the average sales.

Here is a sample file:

Jim Jones 5000 20000
Hillary Dunlap 4000 30000
Janice Joplin 4000 40000
Maurice Chevalier 5000 50000
Anne Gables 5000 60000

From this data it is apparent that the average sales is $40,000.00 and Maurice and Anne are the only ones earning a bonus. Maurice will earn a bonus on 50000-40000 = 10000 and Anne will earn a bonus on 60000-40000 = 20000. So Maurice earns a bonus of $500 and Anne a bonus of $1000.

Here is how the report should look:

Name Salary Sales Bonus Total Salary
-------------------- -------- -------- ------- ------------
Jim Jones 5000.00 20000.00 0.00 5000.00
Hillary Dunlap 4000.00 30000.00 0.00 4000.00
Janice Joplin 4000.00 40000.00 0.00 4000.00
Maurice Chevalier 5000.00 50000.00 500.00 5500.00
Anne Gables 5000.00 60000.00 1000.00 6000.00

I just cant do arrays at all. Anything else i can do. So if anyone can give me some help id appreciate it here is my code so far......

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
string array[100];
string first_name,last_name;
int salary,sales,bonus,total;
ifstream infile;
ofstream outfile;
infile.open("sales.txt");
outfile.open ("bonuses.txt");

//average of total sales
int average;

while (!infile.eof())
{infile,array
//calculate if eligible
if (sales > average)
{
//bonus
bonus = sales - average;
}


//total salary
total = sales + bonus;







//close file
infile.close();
outfile.close();

system ("pause");
return 0;
}

sorry the section where it shows how the report should look should be neater than that. It just post it weird.
Use the code and output formats and it will look like it's supposed to. I would suggest editing and reposting.
Ok.
First thing,
Instead of doing
while (!infile.eof())

Do this

1
2
string Current_String;
While(infile >> Current_String)


!infile.eof() will read in the last line of your file twice, which is a pain.

"infile >> Current_String" Actually evaluates to a bool expression while also functionally putting the next line form the file into the variable.

I personally have no idea whats going on here. It apears to me that theres alot of errors there.
1
2
3
4
5
6
7
8
9
10
int average;

while (!infile.eof())
{infile,array
//calculate if eligible
if (sales > average)
{
//bonus
bonus = sales - average;
}


First of all, You are trying to see if Sales is greater than average before average has any actual value.

Also, im not sure what your trying to do here with {infile,array


But moving on to your actual question...

This would be alot easier if you could put their name and their salery in different files, or atleast different lines.

But anyway, You would probably want to create 2 arrays. One for the empoyee names and another for the saleries. The names array would have to be a string array, and the salery array would have to be an integer array.
so something like this
1
2
int salary[100];
string names[100];


You would also want 2 int variables. One to keep track of how many galleries you have put in an array and another to keep a running total of the galleries. This way you can get your average without having to use another loop to sort through your arrays after youve gotten all the stuff from the file.

So you would probably want it to looks something like this

int ttl_saleries = 0;
int sum_of_saleries = 0;

From here you will need to have your while loop to get input from the file. However since the salleries and the names have to be in the same line im not really sure how you would go about splitting thos up, unless you can loop through a string character by character and after the second space add all characters to a new variable which would than be converted into an integer variable, and you could than put the name and the salery in different arrays.

When you have to figure out if someones salery is over the average you would use a for loop with your arrays.

1
2
3
4
5
int bonus;
for(i = 0; i<100, i++){
        if(salary[i] >average){
               bonus = (salary[i]-average)/.05);}
        outfile << names[i] << " " << bonus << " "<< salary << endl;}


Hope this helps.
Last edited on
Also forgot to include this in the last part.

Im just going to assume for this example that you have 2 different files, one for salary and one for name.
with name_txt, and salary_txt being the instreams
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string names_array[100];
int salary_array[100];
string name;
string salary_str;//i think you have to get the input form the file first as a string before you can convert it to an int
int salary;
int index = 0;
int current_salary;
int salary_sum
int average;
while(name_txt>>name){
         salary_txt>>salary_str;
         salary_str>>salary;
         names_array[index] = name;
         salary_array[index] = salary;
         i++;
         salary_sum += salary;

}


and to get average you would just do this

average = salary_sum/index;

So assuming you could find a way to split up the input from the file
this would work

Last edited on
Topic archived. No new replies allowed.