c++ code error - (working with vortexes)

G

GRoll21

Hey this is my first time posting on of these. the program just gets
the employee number, then it gets hours, how much they get paid, then
figures total. for some reason its not liking my last fucntion. this is
the 1 error it gives me. i'm not done with the program yet but I can't
really go on till I figure this out. I need to add somes pushbacks in
there for it to be done. i got it commented out at the bottom where it
says the problem is. its the last function. any help or advice would be
great. thanks to all.

c:\C++\vector\vector.cpp(85): error C2676: binary '*' :
'std::vector<_Ty>' does not define this operator or a conversion to a
type acceptable to the predefined operator
with
[
_Ty=int
]




#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

//prototypes

void getInfo(vector<int>, vector<int>, vector<float>, vector<float>);
// Get input for the hours and payRate arrays
void calcPay(vector<int>, vector<float>, vector<float>, int); //
Calculate the values for the wages array
void printInfo(vector<int>, vector<int>, vector<float>, vector<float>);
// Print all array values


int main()
{

vector<int> empId;
empId[0] = 5658845;
empId[1] = 4520125;
empId[2] = 7895122;
empId[3] = 8777541;
empId[4] = 8451277;
empId[5] = 1302850;
empId[6] = 7580489;



vector<int> hours(7); // holds employee's hours
vector<float> payRate(7); //holds employee's hourly pay rate
vector<float> wages(7); // holds employee's gross wages

getInfo(empId, hours, payRate, wages);
printInfo(empId, hours, payRate, wages);

return 0;

}

void getInfo(vector<int> empId, vector<int> hours, vector<float>
payRate, vector<float> wages)
{
for (int index = 0; index < 7; index++)
{ system("cls");
cout << "Info for employee Number: " << empId[index] <<"\n";
cout << "Hours: ";
cin >> hours[index];
while(hours[index] < 0)
{
cout << "\nPlease enter a non negative number. ";
cin >> hours[index];
}
cout << "Pay Rate: $";
cin >> payRate[index];
while(payRate[index] < 6.00)
{
cout <<"\nPlease enter a realistic pay rate. $";
cin >> payRate[index];
}

cout << "\n";
calcPay(hours, payRate, wages, index);
}

}

void printInfo(vector<int> empId, vector<int> hours, vector<float>
payRate, vector<float> wages)
{

//header
system("cls");
cout << "\t\tPayroll Information\n\n";
cout << "Employee ID Hours Pay Rate Pay\n";
cout << "----------- ----- -------- ---\n";

for (int index = 0; index < 7; index++)
{
cout << empId[index] << "\t" << setprecision(2)<< fixed << setw(14) <<
hours[index] << setw(11)<< payRate[index] << setw(12)<< wages[index]
<<"\n";
}
cout <<"\n";
}

void calcPay (vector<int> hours[], vector<float> payRate[],
vector<float> wages[],int index)
{
//// this -->> wages[index] = hours[index] * payRate[index];
}
 
M

mlimber

GRoll21 said:
Hey this is my first time posting on of these. the program just gets
the employee number, then it gets hours, how much they get paid, then
figures total. for some reason its not liking my last fucntion. this is
the 1 error it gives me. i'm not done with the program yet but I can't
really go on till I figure this out. I need to add somes pushbacks in
there for it to be done. i got it commented out at the bottom where it
says the problem is. its the last function. any help or advice would be
great. thanks to all.

c:\C++\vector\vector.cpp(85): error C2676: binary '*' :
'std::vector<_Ty>' does not define this operator or a conversion to a
type acceptable to the predefined operator
with
[
_Ty=int
] [snip]
void calcPay (vector<int> hours[], vector<float> payRate[],
vector<float> wages[],int index)
{
//// this -->> wages[index] = hours[index] * payRate[index];
}

You're mixing array notation and vectors, which is probably not your
intent. Your function should be:

void calcPay ( const vector<int>& hours,
const vector<float>& payRate,
vector<float>& wages,
const int index )
{ /* ... */ }

Note the &'s attached to the parameters to the function. They cause the
compiler to pass by reference rather than by value, and if you don't
put them on your vectors, the compiler will copy the entire vector,
which could be expensive. The const qualifier on the parameters is good
practice if you don't intend to modify the data (see the FAQ:
http://www.parashift.com/c++-faq-lite/const-correctness.html).

Cheers! --M
 
V

Victor Bazarov

GRoll21 said:
[...]
void calcPay (vector<int> hours[], vector<float> payRate[],
vector<float> wages[],int index)

Drop the brackets from the parameter declarations.
{
//// this -->> wages[index] = hours[index] * payRate[index];
}

V
 
G

GRoll21

i didn't add the const to them yet.. but will once i get this working.
i added & to all of them and i took out the braces i had in my last
function. now when i run it, it says linking.. then crashes the
program. i belive in order to make it work i need the push_back
function . but im not sure where to put it or when it needs it. aww.. i
like arrays. easier to me. even multi ones. but once i get vectors down
i'll grow to like them. any help is appreciated. thanks guys very much.
 
I

int2str

GRoll21 said:
vector<int> empId;
empId[0] = 5658845;
empId[1] = 4520125;
...

This is undefined behaviour. When you create the vector, it has no
elements allocated. Accessing empId[x] is undefined.

Either, specify the size when you create the vector, or better, use
push_back(). Like so:

vector<int> empId;
empId.push_back( 12345 );
empId.push_back( 54321 );
...

That way, adding another employee won't require you to change the code
in two places either.
vector<int> hours(7); // holds employee's hours
vector<float> payRate(7); //holds employee's hourly pay rate
vector<float> wages(7); // holds employee's gross wages

Instead of hard coding the vector size here, use the push_back as shown
above, then allocate the vectors like this:

vector<int> hours;
hours.resize( empId.size() );
...

But really, it looks like the information about employees is linked
together. As in, one Employee (ID) has a payRate and hours. So why not
encapsulate them?

class Employee
{
private:
int id;
int hours;
float payRate;
public:
Employee( int id = 0 );
int getID();
void setID( int id );
float calcWage();
...
}

std::vector<Employee> employees;
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top