The problem was asking for employee numbers 1 through 10 (they gave me the array numbers to enter into the array) give the total sales for each employee by combining the 3 months together. In my addition function it does everything correctly....for the first part... It displays the numbers in the array perfectly but then when it goes to add the arrays and tosses out an element here and there resulting in incorrect totals. In my code I added that it should cout the array numbers that it is adding together after the first set of numbers it doesn't follow the array here is the code:
I followed what you guys were showing me (thank you btw) and I am now adding Employee #1's total to the rest which I do not want to do. I want to enter Employee #1's to each other stop display it then add Employee #2's total from the 3 numbers in the 3 months array stop display (continue until each piece is displayed 1~10) I have entered my new code for revision. I am new to C++ programming and I have not learned about classes yet so I honestly cannot use them.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void displaySales(int sales[10][3]);
void displayTotalSales(int total[10][3]);
int main ()
{
//declare array Jan Feb Mar
int employ[10][3] = {{2400, 3500, 2000},
{1500, 7000, 1000},
{600, 450, 2100},
{790, 240, 500},
{1000, 1000, 1000},
{6300, 7000, 8000},
{1300, 450, 700},
{2700, 5500, 6000},
{4700, 4800, 4900},
{1200, 1300, 400}};
//displays the sales for the month
displaySales(employ);
displayTotalSales(employ);
system("pause");
return 0;
}
//******Functions*******
void displaySales(i开发者_运维问答nt sales[10][3])
{
for(int emp = 0; emp < 10; emp++)
{
cout << "Employee # " << emp + 1
<< ": " << endl;
for (int month = 0; month < 3; month++)
{
cout << " Month " << month + 1
<< ": ";
cout << sales[emp][month] << endl;
} //end for
} //end for
} //end function
void displayTotalSales(int total[10][3])
{
int employ = 1; //employee number
int totalSales = 0; // total sales for the employee
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 3; j++)
{
totalSales += total[i][j];
cout << "Employee # " << employ << ": " << endl;
cout << endl;
cout << "Total sales for the month: " << "$" << total[i][j];
cout << endl;
}
cout << " Total Sales for the three months is: $" << totalSales << endl;
cout << endl;
employ++;
}
}
do {
...
totalSales = (total[i][j] + total[i][j+1] + total[i][j+2]);
j++;
} while (j < 3);
j goes out of bounds after the first iteration.
But seriously: Use classes! and Use containers!
Oh, and your curly brackets are totally messed up.
Its probably not appropriate for me to add this an answer but since there is no way for newbies to comment, I'll just say it here.
I agree with karl regarding learning about objects. when we learned about c and c++ at college we started out with structs and then moved on to classes and its really important to learn this stuff if you're serious about programming.
a class is just a way of describing an object in the real world. it has attributes and behaviours. For example you could have an employee class that could store all their earnings per month and it could have a function within it that allows you to calculate their recent earnings. These small additions to your code will make it easier to read, organise and reuse.
I seriously suggest you spend a few hours googling object oriented concepts and try some c++ examples. they're very easy.
First of all please form your code better! Indenting would make this a hell of a lot easier to understand and help you with.
I get the strong feeling that this is a homework question for a programming class but I'll try and help you with it anyway.
Basically your problem is that you are running over the end of the array, because when j == 2 for example when you use the statement:
totalSales = (total[i][j] + total[i][j+1] + total[i][j+2]);
you are trying to reference j+2 which is actually the 5th element of the array, which does not exist.
I did a 10 second rewrite of your addFunk (please name functions better)
You could try something like this:
void addFunk(int total[10][3])
{
int employ = 1; //employee number
int totalSales = 0; // total sales for the employee
for (int i = 0; i < 10; i++)
{
for ( int j = 0; j < 3; j ++)
{
totalSales += total[i][j];
}
cout << "employee num " << employ << "earned "
<< "$" << totalSales << endl;
totalSales = 0;
employ++;
totalSales = 0;
}
}
Regarding your update to the code, you say:
I am now adding Employee #1's total to the rest which I do not want to do.
The problem is this line:
int totalSales = 0; // total sales for the employee
Look at the comment you've put there: it's per-employee. Therefore, it should go inside the "per-employee loop":
for (int i = 0; i < 10; i++)
{
int totalSales = 0;
// Proceed as before with the per-month work.
Please read up about the "scope" of variables in C++.
Also, being new to C++, or new to programming in general, is not really an excuse to avoid classes. There's nothing particularly advanced about them per se; the code is only as complicated as you make it - and classes exist because using them properly helps organize (read: simplify) your code.
精彩评论