I have an array of pointers to strings (char *name[maxnum]). I add elements to the array and output each
element right after to check their content and it is okay.
char[counter] = value;
cout << char[counter] << endl;
However, at a later point in the code I output all the elements, and they are all equal to element the last
element (maxnum). :-(
I do a similar process with an array of doubles (double [maxnum]) and the process above works fine.
#include <iostream>
#include <iomanip>
// Function declaration
void eatline();
// Global variables
char mainMenuSelection = ' ';
char employeeName[40];
char employeeType;
int main()
{
// Constant declarations
const int maxEmployees = 100;
const double hrThreshold = 40.0, wklyCommSalary = 250.,
overTimeRatio=1.5, wklySalesPerc=5.7;
// Variable declarations
int numberItems, val_employee_counter=0;
double wklyPay = -1.,
fixedWklySalary,
hourlyWage, hoursWorked,
grossWklySales,
ratePerItem,
validwklyPay[maxEmployees];
char *validEmployeeName[maxEmployees];
// While user does not want to quit program
while (mainMenuSelection != 'Q' && mainMenuSelection !='q') {
switch (employeeType) {
break;
default: // default
cout << "Incorrect employee type entered.\n";
cout << "Try again!\n";
break; // Need to go back to enter employee info if something is wrong
} // end switch
} // end while
} // end if
if (val_employee_counter >= 1) { //Issue: If second employeename is too long, first will be sub by it
cout << endl << endl;
cout << setw(40) << left << " Employee Name" << setw(15) << right << " Pay " << endl;
cout << setw(40) << left << " *************" << setw(15) << right << "*******" << endl;
for (int i=0; i<=(val_employee_counter-1); i++) {
cout << i+1 << ". " << setw(40) << left << validEmployeeName[i] << "$" << setw(14) << right <<
validwklyPay[i] << endl;
}
cout << endl << endl;
} // end if
else {
cout << "\nNo employee information has been entered!\n";
} // end else
} // end else if
} // end while
return 0;
}
}
The statements/blocks in question are:
char *validEmployeeName[maxEmployees];
validEmployeeName[val_employee_counter] = employeeName;
ou are using arrays of char. You want to use strcpy() to copy the names:
This makes the pointer point to the employeName array, whose content changes for each entry, and
finally it contains the last employee name that was entered. In other words, all of your
validEmployeeName[] pointers end up pointing to the employeeName array, therefore the output will
show them to be the same.
To get the name into the pointer array element that you just obtained from new, use strcpy.
//
// Instead of your statement use this:
strcpy(validEmployeeName[val_employee_counter], employeeName);
//
(Also setting validEmployeeName[...] equal to something other than the value given by new means that
the memory can never be freed. This is a Bad Thing. It is the programmer's responsibility to make sure
that for every memory block obtained by new it is returned to the operating system by a delete before
exiting. This is important!)
Do I need a loop?