I have written my code for an employee management system that
stores Employee class objects into a vector, I am getting no errors
until I try and compile, I am getting the error: C2679 binary '==':
no operator found which takes a right-hand operand of type 'const
std::string' (or there is no acceptable conversion). But I am not
sure why any help would be great, Thank you!
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Employee { public: Employee(); string GetName(); string GetStatus(); float GetSalary(); int GetAge(); int GetYearHired(); private: string m_Name; string m_Status; float m_Salary; int m_Age; int m_YearHired; }; Employee::Employee() { m_Salary = 0; m_Age = 0; m_YearHired = 0; } string Employee::GetName() { string fName; string lName; cout << "Please enter the new employee's first name: "; cin >> fName; cout << "Please enter the new employee's last name: "; cin >> lName; m_Name = fName + lName; return m_Name; } string Employee::GetStatus() { string status; cout << "Please enter the employee's status (full time, part time, or manager): "; cin >> status; return m_Status; } float Employee::GetSalary() { float salary; cout << "Please enter the employee's salary: "; cin >> salary; return m_Salary; } int Employee::GetAge() { int age; while (true) { cout << "Please enter the employee's age: "; cin >> age; if (age > 0) break; else cout << "Error: Please enter a positive value."; } return m_Age; } int Employee::GetYearHired() { int yearHired; cout << "Please enter what year the employee was hired: "; cin >> yearHired; return m_YearHired; } class Staff { vector<Employee*> emps; vector<Employee*>::const_iterator iter; public: Staff(); virtual ~Staff(); void Add(); void Remove(); void Clear(); void Display(); }; Staff::Staff() { emps.reserve(20); } Staff::~Staff() { Clear(); } void Staff::Add() { Employee* emp = new Employee; emp->GetName(); emp->GetStatus(); emp->GetSalary(); emp->GetAge(); emp->GetYearHired(); emps.push_back(emp); } void Staff::Remove() { Employee* emp; cout << "Which employee would you like to remove?"; emp->GetName(); iter = find(emps.begin(), emps.end(), emp->GetName()); // Trying to find the employee in the datbase. if (iter != emps.end()) // If the employee is found in the vector it is removed. { cout << "\n" << *iter << " was removed\n\n"; emps.erase(iter); // removes employee from the vector. } else // If the employee is not found in the vector, it tells the user that the employee was not found. { cout << "Employee not found, please choose anoter employee.\n\n"; } } void Staff::Clear() { cout << "\nDo you really want to clear all employees? (yes/no)\n"; // Asking the user if they want to clear the database string response; // Storing the response of the user. cin >> response; // Getting the users response (yes/no). if (response == "yes") // If response is yes. { vector<Employee*>::iterator iter = emps.begin(); // Declares an iterator for the emps vector and sets it to the beginning of the vector. for (iter = emps.begin(); iter != emps.end(); ++iter) // Iterates through vector. { delete *iter; // Deletes the iterators in the vector, freeing all memory on the heap. *iter = 0; // Sets iterator to zero so it does not become a dangling pointer. } emps.clear(); // Clear vector of pointers. } else // If response is no. { cout << "\nAll employee's remain in the database.\n"; } } void Staff::Display() { Employee* emp; if (emps.size() == 0) // Checking to see if the database is empty. cout << "\nThere are no Employee's in the database, add Employee's to view them here.\n"; else // If the database contains any Employee's. { cout << "\nThe database contains: \n"; for (iter = emps.begin(); iter != emps.end(); ++iter) // Displaying the database. { cout << "-------------------------------------------------"; cout << "Employee's Name : " << emp->GetName() << endl; cout << "Employee's Status : " << emp->GetStatus() << endl; cout << "Employee's Salary : " << emp->GetSalary() << endl; cout << "Employee's Age : " << emp->GetAge() << endl; cout << "Year employee was hired : " << emp->GetYearHired() << endl; cout << "-------------------------------------------------"; } } } int main() { int option = 0; Staff stf; // Welcoming the user to the Employee Management System program. cout << "Welcome to our Employee Management System! To get started see the menu options below:\n"; // Main loop while (option != 5) // The loop will repeat until the user enters 5 as the option. { cout << "--------------------------------------------------------------------------------------"; cout << "\nMenu Options: \n"; cout << "\nTo select an option, please type in the number that corresponds to that option.\n"; cout << "1 - Add an Employee\n2 - Remove an Employee\n3 - Clear the database\n4 - Display Employee's in Database\n5 - Quit" << endl; cout << "\nWhat would you like to do? "; cout << "--------------------------------------------------------------------------------------"; // Start of the validity check. bool validInput = false; while (!validInput) // The loop will repeat until the users input is valid. { cin >> option; // User inputs first option choice. validInput = true; // Assign the input as valid. if (cin.fail()) // Tests to make sure the value assigned is valid for the variable type. { cout << "\nPlease choose a menu option by number\n"; cin.clear(); // Clears stream error. cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Removes an invalid characters. validInput = false; // Sets the input back to false, repeats the loop. } } switch (option) { case 1: { stf.Add(); break; } case 2: { stf.Remove(); break; } case 3: { stf.Clear(); break; } case 4: { stf.Display(); break; } case 5: // If option == 5. cout << "\nThank you for using the Employee Management Program!\n"; // Thanks the user for using the Employee Management program. break; default: // If the user does not put in a valid option, it tells them to try again. cout << "\nThat's not a valid option. Please try again.\n"; break; } } system("pause"); return 0; } |
The problem is with find function in Remove method:
Actual: iter = find(emps.begin(), emps.end(), emp->GetName());
Updated: iter = find(emps.begin(), emps.end(), emp);
You need to pass only Employee class object but not string.
Updated Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Employee
{
public:
Employee();
string GetName();
string
GetStatus();
float GetSalary();
int GetAge();
int GetYearHired();
private:
string m_Name;
string m_Status;
float m_Salary;
int m_Age;
int m_YearHired;
};
Employee::Employee()
{
m_Salary = 0;
m_Age = 0;
m_YearHired = 0;
}
string Employee::GetName()
{
string fName;
string lName;
cout << "Please
enter the new employee's first name: ";
cin >>
fName;
cout << "Please
enter the new employee's last name: ";
cin >>
lName;
m_Name = fName +
lName;
return m_Name;
}
string Employee::GetStatus()
{
string status;
cout << "Please
enter the employee's status (full time, part time, or manager):
";
cin >>
status;
return m_Status;
}
float Employee::GetSalary()
{
float salary;
cout << "Please
enter the employee's salary: ";
cin >>
salary;
return m_Salary;
}
int Employee::GetAge()
{
int age;
while (true)
{
cout << "Please enter the employee's age: ";
cin >> age;
if (age > 0)
break;
else
cout << "Error: Please enter a positive value.";
}
return m_Age;
}
int Employee::GetYearHired()
{
int yearHired;
cout << "Please
enter what year the employee was hired: ";
cin >>
yearHired;
return
m_YearHired;
}
class Staff
{
vector<Employee*>
emps;
vector<Employee*>::const_iterator iter;
public:
Staff();
virtual ~Staff();
void Add();
void Remove();
void Clear();
void Display();
};
Staff::Staff()
{
emps.reserve(20);
}
Staff::~Staff()
{
Clear();
}
void Staff::Add()
{
Employee* emp = new
Employee;
emp->GetName();
emp->GetStatus();
emp->GetSalary();
emp->GetAge();
emp->GetYearHired();
emps.push_back(emp);
}
void Staff::Remove()
{
Employee* emp;
cout << "Which
employee would you like to remove?";
emp->GetName();
/* Pass
only Employee class object */
iter =
find(emps.begin(), emps.end(),
emp); // Trying to find the
employee in the datbase.
if (iter !=
emps.end())
// If the employee is found in the vector it is removed.
{
cout << "\n" << *iter << " was
removed\n\n";
emps.erase(iter);
// removes employee from the vector.
}
else
// If the employee is not found in the vector, it tells the user
that the employee was not found.
{
cout << "Employee not found, please choose anoter
employee.\n\n";
}
}
void Staff::Clear()
{
cout << "\nDo you
really want to clear all employees?
(yes/no)\n"; // Asking
the user if they want to clear the database
string
response; // Storing the
response of the user.
cin >> response;
// Getting the users response (yes/no).
if (response == "yes")
// If response is yes.
{
vector<Employee*>::iterator iter = emps.begin(); // Declares
an iterator for the emps vector and sets it to the beginning of the
vector.
for (iter = emps.begin(); iter != emps.end(); ++iter)
// Iterates through vector.
{
delete *iter; // Deletes the iterators in the vector,
freeing all memory on the heap.
*iter = 0; // Sets iterator to zero
so it does not become a dangling pointer.
}
emps.clear(); // Clear vector of pointers.
}
else
// If response is no.
{
cout << "\nAll employee's remain in the database.\n";
}
}
void Staff::Display()
{
Employee* emp;
if (emps.size() ==
0) // Checking to see if the database is empty.
cout << "\nThere are no Employee's in the database, add
Employee's to view them here.\n";
else
// If the database contains any Employee's.
{
cout << "\nThe database contains: \n";
for (iter = emps.begin(); iter != emps.end(); ++iter)
// Displaying the database.
{
cout <<
"-------------------------------------------------";
cout << "Employee's
Name :
" << emp->GetName() << endl;
cout << "Employee's
Status
: " << emp->GetStatus() << endl;
cout << "Employee's
Salary
: " << emp->GetSalary() << endl;
cout << "Employee's
Age
: " << emp->GetAge() << endl;
cout << "Year employee was hired : " <<
emp->GetYearHired() << endl;
cout <<
"-------------------------------------------------";
}
}
}
int main()
{
int option = 0;
Staff stf;
// Welcoming the user to
the Employee Management System program.
cout << "Welcome
to our Employee Management System! To get started see the menu
options below:\n";
// Main loop
while (option !=
5) // The loop will repeat until the user
enters 5 as the option.
{
cout <<
"--------------------------------------------------------------------------------------";
cout << "\nMenu Options: \n";
cout << "\nTo select an option, please type in the number
that corresponds to that option.\n";
cout << "1 - Add an Employee\n2 - Remove an Employee\n3 -
Clear the database\n4 - Display Employee's in Database\n5 - Quit"
<< endl;
cout << "\nWhat would you like to do? ";
cout <<
"--------------------------------------------------------------------------------------";
// Start of the validity check.
bool validInput = false;
while (!validInput) // The loop will repeat until the users input
is valid.
{
cin >> option; // User inputs first option
choice.
validInput = true; // Assign the
input as valid.
if (cin.fail()) //
Tests to make sure the value assigned is valid for the variable
type.
{
cout << "\nPlease choose a menu option by number\n";
cin.clear(); // Clears stream error.
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //
Removes an invalid characters.
validInput = false; // Sets the input back
to false, repeats the loop.
}
}
switch (option)
{
case 1:
{
stf.Add();
break;
}
case 2:
{
stf.Remove();
break;
}
case 3:
{
stf.Clear();
break;
}
case 4:
{
stf.Display();
break;
}
case 5: // If option == 5.
cout << "\nThank you for using the Employee Management
Program!\n"; // Thanks the user
for using the Employee Management program.
break;
default: // If the user
does not put in a valid option, it tells them to try again.
cout << "\nThat's not a valid option. Please try
again.\n";
break;
}
}
system("pause");
return 0;
}
_____________________________________________________________________________________________
Sample Run:
I have written my code for an employee management system that stores Employee class objects into...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...
Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...
I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() { char option; vector<movie> movies; while (true) { printMenu(); cin >> option; cin.ignore(); switch (option) { case 'A': { string nm; int year; string genre; cout << "Movie Name: "; getline(cin, nm); cout << "Year: "; cin >> year; cout << "Genre: "; cin >> genre; //call you addMovie() here addMovie(nm, year, genre, &movies); cout << "Added " << nm << " to the catalog" << endl; break; } case 'R': { string mn; cout << "Movie Name:"; getline(cin, mn); bool found; //call you removeMovie() here found = removeMovie(mn, &movies); if (found == false) cout << "Cannot find " << mn << endl; else cout << "Removed " << mn << " from catalog" << endl; break; } case 'O': { string mn; cout << "Movie Name: "; getline(cin, mn); cout << endl; //call you movieInfo function here movieInfo(mn, movies); break; } case 'C': { cout << "There are " << movies.size() << " movies in the catalog" << endl; // Call the printCatalog function here printCatalog(movies); break; } case 'F': { string inputFile; bool isOpen; cin >> inputFile; cout << "Reading catalog info from " << inputFile << endl; //call you readFromFile() in here isOpen = readFile(inputFile, &movies); if (isOpen == false) cout << "File not found" << endl;...
Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...
I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...
Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly. Hello there. I am trying to implement the djikstras shortest path algorithm into my code in C++ with a vector of vectors. However, when I test I get an error "Exception thrown at 0x75FFC762 in graphMain.exe: Microsoft C++ exception: std::out_of_range at memory location 0x009CF26C" for line 115 in graph.cpp. Could you help me debug? I am so close! ----------------------------FILE NAME: pch.h--------------------------------------- // pch.cpp: source...
C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...