Compiler trouble
I have a problem with this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <string>
#include <string.h>
#define N 3
#define H 7.5
#define OP 10.5
using namespace std;
struct employees {
short int ID;
string name;
int bph;
int oth;
float salary;
};
employees vet[N];
char wh[3];
void load_record(employees vector[]);
void print_record(employees vector[]);
void print_hpw(employees vector[]);
void find_sel(employees vector[]);
void print_menu();
int main(int argc, char *argv[]) {
load_record(vet);
do {
print_menu();
cout << "Exit? (yes/no): ";
cin >> wh;
} while (strcmp(wh, "no") == 0);
}
void load_record(employees vector[]) {
int i;
for (i = 0; i < N; i++) {
cout << "Insert employee name: ";
getline(cin, vector[i].name);
cout << "Insert work hour: ";
getline(cin, vector[i].bph);
cout << "Insert overwork hour: ";
getline(cin, vector[i].oth);
vector[i].ID = i;
vector[i].salary = ((vector[i].bph * H) + (vector[i].oth * OP));
}
}
void print_record(employees vector[]) {
cout << "ID NAME SALARY";
int i;
for(i = 0; i < N; i++) {
cout << vector[i].ID << " " << vector[i].name << " "
<< vector[i].salary;
}
}
void print_hpw(employees vector[]) {
float sMax = 0;
int tID = 0;
int i;
for(i = 0; i < N; i++) {
if (vector[i].salary > sMax) {
sMax = vector[i].salary;
tID = i;
}
}
cout << "The highest paid employee is: " << vector[tID].name << endl;
cout << "with this salary: " << vector[tID].salary << " Euro" << endl;
}
void find_sel(employees vector[]) {
int tmph, i;
cout << "Insert an amount of hour: ";
cin >> tmph;
for(i = 0; i < N; i++) {
if ((vector[i].bph + vector[i].oth) > tmph) {
cout << " " << vector[i].ID << " " << vector[i].name
<< " " << vector[i].salary << endl;
}
}
}
void print_menu() {
char m;
cout << "Choose one of this operations.\n"
<< " - Press 's' to print the whole employees list.\n"
<< " - Press 'h' to print the data about the highest paid employee\n"
<< " - Press 'f' to calculate how much employees worked more tha"
<< " x hour.\n";
cin >> m;
switch (m) {
case 's':
print_record(vet);
break;
case 'h':
print_hpw(vet);
break;
case 'f':
find_sel(vet);
break;
}
}
| |
When I try to compile this source code, the compiler give me this error:
-(line 51) error: no matching function for call to 'getline(std::istream&, int&)
But I know that the getline() function is defined in the <string> library. Why it doesn't work?
getline function is for strings only. use cin >> for ints.
You're right, I forgot :P
Thank you
P.S. Have you some advice to improve this code?
Topic archived. No new replies allowed.