why is the code not working? || srtuctures
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:38pm UTC
here's what the code in the tutorial had:
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
#include <iostream>
struct Employee
{
short id;
int age;
double salary;
};
void printInformation(Employee employee)
{
std::cout << "ID: " << employee.id << "\n" ;
std::cout << "Age: " << employee.age << "\n" ;
std::cout << "Salary: " << employee.salary << "\n" ;
}
int main()
{
Employee john = { 21, 27, 28.45 };
Employee james = { 22, 29, 19.29 };
// Выводим информацию о John-е
printInformation(john);
std::cout << "\n" ;
// Выводим информацию о James-е
printInformation(james);
return 0;
}
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!! :)
Topic archived. No new replies allowed.