I've made some adjustments according to the responses to the thread, thanks for your input everyone!
Employee.h:
Code:
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class employee
{
public:
employee(string _name, string _payrate)
{
name = _name;
payrate = _payrate;
}
string& get_name()
{
return name;
}
string& get_payrate()
{
return payrate;
}
void edit()
{
string input;
int selection;
bool goodinput = false; //loop conditional, input must match conditions, else continue
while (goodinput == false)
{
cout << "What would you like to change?" << endl // prompt
<< "1. Name" << endl << "2. Payrate" << endl;
getline(cin, input); //get input
stringstream(input) >> selection; //extract selection integer
if (selection == 1)
{
cout << "Please enter a name for this employee: ";
getline(cin, name);
goodinput = true;
}
else if (selection == 2)
{
goodinput = true;
cout << "Please enter a pay rate for this employee (hourly): ";
getline(cin, payrate);
}
else
{
goodinput = false; // invalid selection - continue loop
cout << "Please enter a valid selection, 1 or 2." << endl;
}
}
}
private:
string name;
string payrate;
};
#endif
Experimentations.cpp (main)
Code:
// Experimentations.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <assert.h>
#include "Employee.h"
using namespace std;
vector<employee> employees;
employee create()
{
cout << "Please enter the new employee's name: ";
string name;
getline(cin, name);
cout << "Please enter the new employee's payrate: ";
string payrate;
getline(cin, payrate);
employee new_employee(name, payrate);
return new_employee;
}
employee & get_employee(string name)
{
for (unsigned int i = 0; i < employees.size();)
{
if (name.compare(employees[i].get_name()) == 1)
{
return employees[i];
}
else
{
i++;
}
}
throw;
}
void list_employees(vector<employee> & employees)
{
for (unsigned int i = 0; i < employees.size(); i++)
{
cout << employees[i].get_name() << endl;
}
}
int main()
{
int selection;
string input;
bool goodinput = false;
while (goodinput == false)
{
cout << "Welcome to the employee database, what would you like to do?" << endl // prompt
<< "1. Add an employee" << endl << "2. Modify an employee" << endl;
getline(cin, input); //get input
stringstream(input) >> selection; //extract selection integer
if (selection == 1)
{
goodinput = true;
employees.push_back(create());
}
else if (selection == 2)
{
cout << "Enter the name of the employee you'd like to edit: ";
getline(cin, input);
get_employee(input).edit();
goodinput = true;
}
else
{
goodinput = false; // invalid selection - continue loop
cout << "Please enter a valid selection, 1 or 2." << endl;
}
}
return 0;
}
I'm not quite sure how to logically arrange the functions that actually handle the employees vector. I'm thinking about making menu objects to simplify that aspect of programming this thing, any suggestions?