Gross Pay using Arrays (C++) - c++

I am writing a code to calculate the gross pay of seven employees using arrays. Here is what I have so far
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//Set all constants and variables
const int SIZE = 7; //Size of all arrays
int emID[SIZE] = {1234, 4563, 8765, 4568, 9867, 9235, 7684};
double Hours[SIZE],
Rate[SIZE],
Gross[SIZE];
int index;
Gross[index] = (Hours[index] * Rate[index]);
//Explain Program
cout << "This program calculates an employees gross pay\n";
for (Hours[index];index <= 6; index++)
{
cout << "How many hours did employee " << emID[index] << " work?\n";
cin >> Hours[index];
}
for (Rate[index]; index <= 6; index++)
{
cout << "Enter the pay rate for " << emID[index] << endl;
cin >> Rate[index];
}
for (Gross[index]; index <=6 ; index++)
{
cout << "The gross pay for " << emID[index] << " is " << Hours[index] * Rate[index];
}
}
Unfortunately the program terminates after the first "for" loop. Any suggestions?

There seem to be a few mistakes in your code, which I'm pointing out below.
index is unitialised, and the way you've used it results in Undefined Behavior. I think you meant to initialise to to 0.
You should reset the value of index to 0 between for loops. Currently you would iterate in the first for loop. After that, since index will be > 6, your code will not execute the other two for loops.
Your first term in the for loop declarations is wrong. I think you meant to declare index = 0 there. If not, you should leave it empty.
The line near the beginning where you calculate Gross[index] is wrong and redundant.

Related

Array prints out wrong numbers

I'm new to C++ and I have to make a program that lets the user enter a specified number of test scores and calculate the average, highest, and lowest scores. (It doesn't have to check if values are between 1 and 100.) For some reason, when it goes to print out the average score and sum of the scores, it just prints out random exponential numbers that are never the same. I haven't gotten to printing out the lowest and highest scores yet since I'm not sure if I'm even doing the average right. Again, I'm new to C++ so I'm sure I messed something up. Here's the code:
#include <iostream>
using namespace std;
int loopLimit = 0; //number of scores or how many times it will loop
double *testScores = {0}; //array scores are stored in
int main () {
cout << "How many test scores are you entering?" << endl;
cin >> loopLimit;
testScores = new double[loopLimit]; //changes array to needed size
for (int i = 0; i < loopLimit; i++) {
cout << "Enter test score #" << (i + 1) << endl;
cin >> *testScores;
}
double sum = 0.0;
double average = 0.0;
//double max = 0.0; //making these functions later
//double min = 0.0;
sum += testScores[loopLimit];
average = sum / loopLimit;
//Sum is here for testing purposes at the moment
cout << "Sum = " << sum << " Average = " << average << endl;
return 0;
}
Example output 1:
How many test scores are you entering?
3
Enter test score #1
100
Enter test score #2
100
Enter test score #3
100
Sum = 8.29874e-310 Average = 2.76625e-310
Example output 2:
How many test scores are you entering?
3
Enter test score #1
100
Enter test score #2
100
Enter test score #3
100
Sum = 8.94176e-310 Average = 2.98059e-310
Expected output:
How many test scores are you entering?
3
Enter test score #1
100
Enter test score #2
100
Enter test score #3
100
Sum = 300.0 Average = 100.0
I've been at this all week, and I honestly got nothing at this point.
OK, let's go over your code.
#include <iostream>
using namespace std;
Why are you importing all identifiers in std:: here? using namespace std; is not recommended.
int loopLimit = 0; //number of scores or how many times it will loop
double *testScores = {0}; //array scores are stored in
Bad: You should avoid global variables unless they're absolutely necessary. 99.9% of the time they're not. These could easily be local variables in main.
testScores is not an array, it's a pointer. Initializing it with {0} is just a weird way of writing testScores = nullptr;.
int main () {
cout << "How many test scores are you entering?" << endl;
cin >> loopLimit;
testScores = new double[loopLimit]; //changes array to needed size
You're using manual memory management here. Not technically wrong, but it would be much easier and less error prone to use a std::vector instead.
for (int i = 0; i < loopLimit; i++) {
cout << "Enter test score #" << (i + 1) << endl;
cin >> *testScores;
This line stores every input in *testScores, i.e. the location pointed to by testScores, which corresponds to the first index of the array allocated by new above. This means only testScores[0] is initialized (ending up containing the last number the user input), every other index is uninitialized.
You should use cin >> testScores[i] instead.
}
double sum = 0.0;
double average = 0.0;
//double max = 0.0; //making these functions later
//double min = 0.0;
sum += testScores[loopLimit];
This is an invalid memory access. As testScores points to a dynamic array of size loopLimit, the valid array indices go from 0 to loopLimit-1. Therefore testScores[loopLimit] accesses memory past the bounds of the array.
Also, it's just one element you're adding here. Even if this were a valid array index, this could would still make no sense. You should loop over all array elements here (like your for loop above), or just do this part of the calculation in your other loop. In fact, there's no need to store all numbers in memory if all you're interested in is their sum (which you can compute directly as you're reading the input).
average = sum / loopLimit;
average is computed from sum, which has a garbage value, so it's garbage too.
//Sum is here for testing purposes at the moment
cout << "Sum = " << sum << " Average = " << average << endl;
... and this is why you're getting garbage output.
return 0;
Here you're leaking the memory allocated with new. This is not really a problem in this case because your program is about to exit anyway, but in general you want delete[] testScores; here (unless you use a std::vector, which takes care of cleaning up for you).
}
A couple of things, first the line
cin >> *testScores;
is not storing the test scores in an array (I think this is what you want to do) instead at every iteration the new test score is being stored in the first element i.e testScores[0], rewriting the old value.
Next,
The line
sum += testScores[loopLimit];
looks outside the array. This means that you are looking at a random place in memory which probably has junk in it. That explains why you are seeing random numbers outputted.
Try to fix those two issues. If you don't need to save the test scores you can away keep a running sum of them, that would eliminate the need to store everything in an array.
I don't know why you want to write such a messed up code for just calculating average.But here's my approach.
Since you entered loopLimit value as 3.So according to your code,sum will contain have the value of sum[3].That is a garbage value.Since indexing starts with 0 and not 1.So your sum will have a garbage value and that is the reason why you are getting such incorrect value.I have modified your code and it works fine for me.
int loopLimit;
cout << "How many test scores are you entering?" << endl;
cin >> loopLimit;
double scores[loopLimit];
double sum=0.0;
for (int i = 0; i < loopLimit; i++)
{
cout << "Enter test score #" << (i + 1) << endl;
cin >> scores[i];
sum+=scores[i];
}
double avg=sum/loopLimit;
cout << "Sum = " << sum << " Average = " << avg << endl;
return 0;
Try avoiding using pointers during the early stage of programming as it can really mess up with your brain and can create problems.Try to go for a simple approach.
The possible issue is that you're doing this
double* testScores = {0};
which will initialize it to nullptr, potentially letting you read and write garbage data. What you should use instead is probably a dynamic resizing array such as
std::vector<double> testScores;
and to add a score to the list
double inScore = 0;
std::cin >> inScore;
testScores.push_back(inScore); // This adds to the list of scores.
#include <iostream>
using namespace std;
int loopLimit = 0; //number of scores or how many times it will loop
double *testScores = {0}; //array scores are stored in
int main () {
double sum = 0.0;
double average = 0.0;
cout << "How many test scores are you entering?" << endl;
cin >> loopLimit;
testScores = new double[loopLimit]; //changes array to needed size
for (int i = 0; i < loopLimit; i++) {
cout << "Enter test score #" << (i + 1) << endl;
cin >> testScores[i];
sum += testScores[i];
}
average = sum / loopLimit;
cout << "Sum = " << sum << " Average = " << average << endl;
delete [] testScores;
return 0;
}
*melpomene is right...that pointer "testScores" wont increment through your for loop so the array occupies garbage also put sum += testScores[i] in the loop to sum your input.

Run Time Check failure #2:

Below is my code so far. I am just doing this step by step, for a class assignment. Trying not to give the whole code I just want to know what is wrong with what I have so I can learn to fix it. Thanks for the help.
The goal is to create class driver then to get total lap times, compare them and put them in order of 1st thru 3rd place. I've made this in a simple program already. Now I am trying to make an array of the class driver.
So far this is working, It asks for input correctly, and gives an output but before the end of the program I get:
Debug Error!
Run Time Check Failure #2 - Stack around the variable 'driver' was corrupted.
None of the answers I've found here make sense to my application. I have 0 failures when I run the build before running the program.
I also know not to use single character variables, I just do this as I learn what I'm missing and later change them.
#include <iostream>
#include <string>
using namespace std;
class Driver
{
int carNumber;
public:
void setNumber(int number)
{
carNumber = number;
}
int getNumber()
{
return carNumber;
}
};
int main()
{
int x;
int i;
Driver driver[3];
for (i = 1; i <= 3; i++)
{
cout << "Enter Driver " << i << "'s car number: ";
cin >> x;
driver[i].setNumber(x);
}
for (i = 1; i <= 3; i++)
cout << "driver " << i << "'s number is " << driver[i].getNumber() << endl;
return 0;
}
start for loop as shown below because as we know that array stores n elements from location 0 to n-1.
for (i = 0; i <3; i++)
{
cout << "Enter Driver " << i << "'s car number: ";
cin >> x;
driver[i].setNumber(x);
}
Your array access indexes are wrong.
Arrays start at index 0, not 1, and end at 1 less than the number of elements. So for a declaration of driver[3], the valid indexes are 0, 1, and 2, not 1, 2 and 3. With the wrong indexes, you're writing past the end of the array, causing the stack corruption.
Your write loop should be more like
for (i = 0; i < 3; i++)
{
cout << "Enter Driver " << i << "'s car number: ";
cin >> x;
driver[i].setNumber(x);
}
You'll need to make a similar fix to the read loop.

My search function in C++ is pulling up all the answers and not a single one

I'm really confused. I have to make this lab for a class and I can't seem to have the search only display one result but all of the months of the year. I also can't seem to figure out why its not displaying the TotalRainfall when I input 0 into the month of the year.
Thank you.
#include <iostream>
#include <fstream>
const int MaxSize = 12; //How many weather lines will be available.
using namespace std;
struct WeatherInformation
{
int Month; //Months of the year
float TotalMonthsRainfall; //Total amount of rainfall
float HighTemp; //The Highest temperature of the month.
float LowTemp; //The Lowest temperature of the month.
float AverageTemp; //The Average temperature of the month.
};
WeatherInformation WeatherArray[MaxSize]; //Declaring a month array of MaxSize
void ReadFile(ifstream& MyinFile, WeatherInformation WeatherArray[]);
void WeatherMonthSearch (WeatherInformation WeatherArray[]);
int main()
{
float TotalRainfall = 0;
int count = 1; //Counts how many times the for loop goes.
int MonthOfWeather; //User input of the month.
char ProgramRedo; //User input if they want to reuse the program.
char exit_char; //User input to exit the program.
ifstream MyinFile; //Variable that uses file.
ReadFile (MyinFile, WeatherArray); //Call ReadFile Function
WeatherMonthSearch (WeatherArray); //Call WeatherMonthSearch Function
MyinFile.close(); //Closes file.
}
//Brett Holmes
//4/30/2013
//PreCondition:You need a file labeled weather.dat
//PostCondition: It puts the file variables into an array.
void ReadFile(ifstream& MyinFile, WeatherInformation WeatherArray[])
{
float TotalRainfall = 0;
char exit_char;
int count = 0;
int Month = 0;
cout << "Your Weather Machine" << endl << endl;
MyinFile.open("weather.dat");
if (!MyinFile)
{ //no
cout << "Can't open input file." << endl; //Tests the right file.
char exit_char; //End Program
cout << "Press any key to exit" << endl;
cin >> exit_char;
}
for(count = 1; count < MaxSize; count++) //Puts the file variables in the array.
{
WeatherArray[count].Month = WeatherArray[count].Month + 1;
MyinFile >> WeatherArray[count].TotalMonthsRainfall;
MyinFile >> WeatherArray[count].HighTemp;
MyinFile >> WeatherArray[count].LowTemp;
(WeatherArray[count].AverageTemp = ((WeatherArray[count].HighTemp + WeatherArray[count].LowTemp)/2));
(TotalRainfall = TotalRainfall + WeatherArray[count].TotalMonthsRainfall);
}
}
//Brett Holmes
//4/30/13
//PreCondition:You need to have the months already put into an array in a struct.
//PostCondition:Outputs the rainfall stats the user puts in then asks to run again.
//Outputs a error message if they type in the month wrong.
void WeatherMonthSearch (WeatherInformation WeatherArray[])
{
float TotalRainfall;
int months;
int MonthOfWeather;
char ProgramRedo;
do
{
bool MonthFound = false;
cout << "Please input the number of the Month. Ex. 1=Jan. 2=Feb. etc \n\n";
cin >> MonthOfWeather;
for(int i = 1; i <= MaxSize; i++)
{
months = WeatherArray[i].Month;
if(months == MonthOfWeather ) //Finds the artist and outputs the results
{
cout << "\nTotal Months Rainfall: " << WeatherArray[i].TotalMonthsRainfall << " \n";
cout << "Highest Temperature: " << WeatherArray[i].HighTemp << " \n";
cout << "Lowest Temperature: " << WeatherArray[i].LowTemp << " \n";
cout << "Average Temperature: " << WeatherArray[i].AverageTemp << " \n";
MonthOfWeather = true;
}
}
if(MonthOfWeather == 0)
{
cout << "The total rainfall for the year is: " << TotalRainfall << ".";
}
if(MonthFound == false)
{
cout << "\nMonth Number error. Month not found. Try again.\n\n";
MonthOfWeather = false;
}
cout << "Would you like to look up another month of weather?\n";
cout << "Enter a 'Y' if yes and 'N' if no.\n";
cin >> ProgramRedo;
}while(ProgramRedo == 'Y');
}
Several obvious problems:
Arrays in C++ is 0-based, so your for loop is off-by-one. In your search function, for(int i = 1; i <= MaxSize; i++) should be for(int i = 0; i < MaxSize; i++). Similarly, in your read function, for(count = 1; count < MaxSize; count++) should be for(count = 0; count < MaxSize; count++) (If you want to skip index 0 because you are using it as a signal value, then you should set MaxSize to 13 and have the loop start at 1.)
Why are you assigning a boolean to MonthOfWeather? Do you mean MonthFound?
You read function is not setting the months correctly. WeatherArray[count].Month = WeatherArray[count].Month + 1; should be WeatherArray[count].Month = count; if you are using a 1-based loop or WeatherArray[count].Month = count + 1; if the loop is 0-based.
You calculated your total rainfall in the read function, but the result is stored in a local variable so it's lost when the read is done. Either make TotalRainfall a global variable or do your calculations in your search function.
There are a lot of redundant variable definitions: for example, your weather data array is a global so there is no reason to actually pass it around; exit_char is declared twice in your read function; the first five lines of your main() declared variables that you never used.
Also, your read function does not actually exit the program on failure - it even still attempts to read from the stream and then call your search function! If error-checking is a requirement, you should either have the read function return a boolean and check that the read function succeeded before calling your search function, or simply call std::exit after that cin >> exit_char;.
So, one problem you have is that you have local variables that appear in multiple places, but appears like you expect them to actually contain the same information.
For example, I see three different TotalRainFall. One in main, which is just there, not used for anything, one in ReadFile which is calculated, and one in WeatherMonthSearch, which is not set to anything.
I suspect you want all three of these to actually do something. One way to achieve that would be to remove the local ones in ReadFile and WeatherMonthSearch, and instead pass in the one from main (as a reference into ReadFile).
There's also a few places where you use variables without initializing them. Make it a habit to initialize EVERYTHING and EVERYWHERE!
Enable warnings in your compiler - if you have any form or reasonably new compiler (gcc or MS Visual Studio of recent vintage), it should tell you at least some of these things.

I'm getting a weird error for a program that seems like it should "just work."

I present to you all a program I'm working on for my college programming course. I still have a little ways to go before it completely meets my assignment's requirements, but I've gotten a basic draft of the program error-free (supposedly) and it appears to run… but then it suddenly kicks me into Xcode's debugger and gives me:
Thread 1: EXC_BAD_ACCESS(code=2, address=0x7fff95c1e5f5)
Here's the command line output, up until it kicks me out:
-----------------------
Quarterly_sales_taxator
-----------------------
How many company divisions will we be dealing with? 2
Am I correct in assuming that there are 4 sales quarters? yes
Please enter the sales Company Division #1 brought in for Sales Quarter #1 20
(lldb)
Here's my code:
//
// quarterly_sales_taxator.cpp
// Ch. 7 program #7
//
// Created by John Doe on 11/27/12.
//
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;
void read_company_divisions_and_sales_quarters(double **, int, int);
//void write_company_divisions_and_sales_quarters_to_array(double **, int, int); // This will be used later on to read data from a file.
void display_quarterly_sales_array(double **, int, int);
string temp; // A global temporary placeholder variable; I use this several times.
int main()
{
int COMPANY_DIVISIONS,
SALES_QUARTERS = 4;
double **quarterly_sales_form;
cout << "\n\n-----------------------\nQuarterly_sales_taxator\n-----------------------\n\n";
cout << "\nHow many company divisions will we be dealing with? ";
getline(cin, temp);
stringstream(temp)>>COMPANY_DIVISIONS;
while (COMPANY_DIVISIONS < 1 || isdigit(COMPANY_DIVISIONS == false))
{
cout << "\n\n------"
<< "\nError:"
<< "\n------"
<< "\n\nYou have entered an invalid choice."
<< "\nPlease type a number greater than zero. ";
getline(cin, temp);
stringstream(temp)>>COMPANY_DIVISIONS;
}
cout << "\n\nAm I correct in assuming that there are 4 sales quarters? ";
getline(cin, temp);
// Convert to uppercase.
for (int count = 0; count < temp.length(); count ++)
{
temp[count] = toupper(temp[count]);
}
if (temp == "NO" || temp == "NOPE" || temp == "INCORRECT" || temp == "YOU ARE NOT" || temp == "YOU ARE INCORRECT" || temp == "NEGATIVE" || temp == "NEGATORY")
{
cout << "\nOk, then how many sales quarters are we dealing with? ";
getline(cin, temp);
stringstream(temp)>>SALES_QUARTERS;
}
cout << endl << endl;
// This sets up the 2d array.
quarterly_sales_form = new double *[COMPANY_DIVISIONS];
for (int count = 0; count < COMPANY_DIVISIONS; count ++)
{ quarterly_sales_form[COMPANY_DIVISIONS] = new double [SALES_QUARTERS]; }
read_company_divisions_and_sales_quarters(quarterly_sales_form, COMPANY_DIVISIONS, SALES_QUARTERS);
// write_company_divisions_and_sales_quarters_to_array(quarterly_sales_form, COMPANY_DIVISIONS, SALES_QUARTERS); // I'll add this feature later.
cout << "\n\nHere's what you entered:\n\n";
display_quarterly_sales_array(quarterly_sales_form, COMPANY_DIVISIONS, SALES_QUARTERS);
// Since we used a series of pointers, we need to free the allocated space back up.
for (int count = 0; count < COMPANY_DIVISIONS; count ++)
{ delete[] quarterly_sales_form[COMPANY_DIVISIONS]; }
delete[] quarterly_sales_form;
return 0;
}
/*############################################
# read_company_divisions_and_sales_quarters #
############################################*/
void read_company_divisions_and_sales_quarters(double **array, int DIVISIONS, int QUARTERS)
{
for (int count = 0; count < QUARTERS; count++)
{
for (int index = 0; index < DIVISIONS; index++)
{
cout << "\nPlease enter the sales Company Division #" << count+1 << " brought in for Sales Quarter #" << index+1 << " ";
getline(cin, temp);
stringstream(temp) >> array[count][index];
}
}
}
/*################################
# display_quarterly_sales_array #
#################################*/
void display_quarterly_sales_array(double **array, int DIVISIONS, int QUARTERS)
{
for (int count = 0; count < DIVISIONS; count++)
{
cout << "\nCompany division #" << count+1 << ":\n";
for (int index = 0; index < QUARTERS; index++)
{ cout << array[count][index] << ", "; }
}
}
Can some kind soul please tell me what I'm doing wrong?
{ quarterly_sales_form[COMPANY_DIVISIONS] = new double [SALES_QUARTERS]; }
In this line, COMPANY_DIVISIONS should be count.
In addition to what Dan Hulme said, it seems this line
stringstream(temp) >> array[count][index];
should really be
std::istringstream(temp) >> std::skipws >> array[index][count];
In addition to using std::istringstream rather than std::stringstream and making sure that an lvalue is at hand, which isn't strictly needed until the type read becomes more interesting, this also reverses the indices: index runs over COMPANY_DIVISIONS and count over SALES_QUARTERS.
The real question is, of course: Who hands out assignments like this? Pointer manipulations and allocations are best left to low-level library writers. This is C++ not C: we can and should use abstractions. Getting this code exception safe is a major challenge and there is no point in teaching people how to write broken (e.g. exception unsafe) code.

Two-Dimensional Array addition problem

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