0

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(int 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++;
        }
}
5
  • 2
    Why are you doing the addition three times per employee? Commented Dec 12, 2010 at 20:47
  • When I copy my code into the editing it moves my indents and such over I am sorry for this inconvenience. Commented Dec 12, 2010 at 21:55
  • If you use chrome there is a nice extension that allows you to select text in a textarea and just press tab to indent (Textarea formatter). Commented Dec 12, 2010 at 22:20
  • @6502 or you can select the code and use the '010 101' button provided by StackOverflow. :) Commented Dec 12, 2010 at 23:10
  • @Karl Knechtel: yeah... the extension is more for writing code directly in the textarea (autoindent and tab/backspace handling) Commented Dec 13, 2010 at 5:54

4 Answers 4

3
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.

Sign up to request clarification or add additional context in comments.

5 Comments

Agreed about the last part - boost::multi_array is our friend :)
@Kos for data of a specific size, a nested boost::array works fine. Or we could have e.g. a std::vector of boost::array s, etc. etc. depending on the exact situation. Choose the right tool for the job. :)
So are there any situations where a std::vector with manual indexing would perform better than boost::multi_array (which- as I understand- is supposed to be a wrapper for the same thing with some useful abstraction on the top)?
std::vector is designed for frequent resizing, and willingly takes up extra space to get better speed when you do resize frequently. boost::multi_array is designed to allow for the size not being known ahead of time, and to allow resizing; but AFAIK it prefers space efficiency and thus resizing may be slower. A vector of vectors will let you change each nested vector's length independently, which is good when you want that ability (obviously) and bad when you don't (because it wastes lots of space and means you write lots more code).
A non-nested std::vector can't be compared to boost::multi_array, of course, because it's conceptually one-dimensional whereas the multi-array can be multi-dimensional (hence the name).
1

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.

1 Comment

+1 so that you can get a little reputation and start posting comments as comments. ;)
1

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

1 Comment

+1 Plus for mentioning editing and good naming convention etc. Not really part of the answer but generally educative :) (I also corrected you on the index, 4 is actually the fifth element)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.