May 1, 2022 at 8:37am UTC
I'm getting this error when running this code
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct staffTWB {
string staffName;
int staffProf;
};
void printEntry(staffTWB);
int main() {
string staffName;
int staffProf;
while (staffName != "done") {
cout << "Please enter a staff name(done to leave the program):" << endl;
getline(cin, staffName, '\n');
if (staffName == "done") {
break;
}
else {
cout << "Please enter the profession of " << staffName << "\n";
cin >> staffProf;
cin.ignore();
if (staffProf >= 1 && staffProf <= 4) {
printEntry(staffTWB);
}
else {
cout << "Wrong entry!\n";
cin.clear();
cin.sync();
}
}
}
void printEntry(staffTWB);
{
if (staffProf == 1)
cout << "Staff " << staffName << " is a Receptionist\n";
else if (staffProf == 2)
cout << "Staff " << staffName << " is a Administrator\n";
else if (staffProf == 3)
cout << "Staff " << staffName << " is a Nurse\n";
else if (staffProf == 4)
cout << "Staff " << staffName << " is a Doctor\n";
}
return 0;
}
It states that is occurs on the printEntry(staffTWB); line. What can I do to stop this error from ocurring?
May 1, 2022 at 8:40am UTC
void printEntry(staffTWB);
You have ; at the end of that line that shouldn't be there.
May 1, 2022 at 8:46am UTC
I then get this error
Compilation failed due to following error(s).
15 | int main() {
May 1, 2022 at 8:52am UTC
You've forgot a closing } at the end of the main function.
May 1, 2022 at 8:56am UTC
When posting formatted code, please use code tags so that the code is readable.
[code]
formatted code goes here
[/code]
There are various issues around usage of struct and defining and using functions. Consider:
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
#include <iostream>
#include <string>
struct staffTWB {
std::string staffName;
int staffProf {};
};
void printEntry(const staffTWB&);
int main() {
staffTWB staff;
while (staff.staffName != "done" ) {
std::cout << "Please enter a staff name (done to leave the program):" ;
std::getline(std::cin, staff.staffName);
if (staff.staffName == "done" )
break ;
else {
std::cout << "Please enter the profession of " << staff.staffName << ": " ;
std::cin >> staff.staffProf;
if (staff.staffProf >= 1 && staff.staffProf <= 4)
printEntry(staff);
else {
std::cout << "Wrong entry!\n" ;
std::cin.clear();
}
std::cin.ignore();
}
}
}
void printEntry(const staffTWB& staff) {
if (staff.staffProf == 1)
std::cout << "Staff " << staff.staffName << " is a Receptionist\n" ;
else if (staff.staffProf == 2)
std::cout << "Staff " << staff.staffName << " is a Administrator\n" ;
else if (staff.staffProf == 3)
std::cout << "Staff " << staff.staffName << " is a Nurse\n" ;
else
std::cout << "Staff " << staff.staffName << " is a Doctor\n" ;
}
or as 'shortened 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
#include <iostream>
#include <string>
struct staffTWB {
std::string staffName;
int staffProf {};
};
void printEntry(const staffTWB&);
int main() {
for (staffTWB staff; (std::cout << "Please enter a staff name (done to leave the program):" ) && std::getline(std::cin, staff.staffName) && staff.staffName != "done" ; std::cin.clear(), std::cin.ignore()) {
std::cout << "Please enter the profession of " << staff.staffName << ": " ;
std::cin >> staff.staffProf;
if (staff.staffProf >= 1 && staff.staffProf <= 4)
printEntry(staff);
else
std::cout << "Wrong entry!\n" ;
}
}
void printEntry(const staffTWB& staff) {
constexpr const char * names[4] {"Receptionist" , "Administrator" , "Nurse" , "Doctor" };
std::cout << "Staff " << staff.staffName << " is a " << names[staff.staffProf - 1] << '\n' ;
}
Last edited on May 1, 2022 at 9:06am UTC