Jan 18, 2021 at 8:36pm UTC
hi, i'm experimenting with structures and i followed the tutorial i saw online and did exactly what it said (i think) but the code isn't working for some reason, please tell me what i did wrong
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
#include<iostream>
#include<string>
using namespace std;
struct Employee
{
string LastName;
string FirstName;
string DadsName;
string position;
char sex;
string year_joined;
double salary;
double bonus;
};
void EmployeeChart(Employee employee)
{
cout << "Last name: " << employee.LastName;
cout << "First name: " << employee.FirstName;
cout << "Dad's name: " << employee.DadsName;
cout << "Position: " << employee.position;
cout << "Gender: " << employee.sex;
cout << "Year joined: " << employee.year_joined;
cout << "Salary: " << employee.salary;
cout << "Bonus: " << employee.bonus;
}
int main()
{
Employee laura = { Laura, Fidarova, Alanovna, stud, female, 2015, 4500 };
EmployeeChart(laura);
return 0;
}
Last edited on Jan 18, 2021 at 8:37pm UTC
Jan 18, 2021 at 8:50pm UTC
Quite a few errors. And the example is not very good.
The biggest error is that string literals need to be in double quotes.
A single char needs to be in single quotes.
Also we would normally pass a struct as a constant reference so to avoid the copy.
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
#include <iostream>
#include <string>
using namespace std;
struct Employee
{
string LastName;
string FirstName;
string DadsName;
string position;
char sex;
int year_joined;
double salary;
double bonus;
};
void EmployeeChart(const Employee& employee)
{
cout << "Last name: " << employee.LastName << '\n' ;
cout << "First name: " << employee.FirstName << '\n' ;
cout << "Dad's name: " << employee.DadsName << '\n' ;
cout << "Position: " << employee.position << '\n' ;
cout << "Gender: " << employee.sex << '\n' ;
cout << "Year joined: " << employee.year_joined << '\n' ;
cout << "Salary: " << employee.salary << '\n' ;
cout << "Bonus: " << employee.bonus << '\n' ;
}
int main()
{
Employee laura = { "Laura" , "Fidarova" , "Alanovna" , "stud" , 'F' , 2015, 45000, 1000 };
EmployeeChart(laura);
return 0;
}
Last edited on Jan 18, 2021 at 8:51pm UTC
Jan 19, 2021 at 2:25am UTC
Okay! I get it now, thank you so much!! :)