Pulling data from a structure? - c++

i currently have this code
struct Students {
int studentID;
char firstName[12];
char lastName[12];
char email[25];
};
void showStudentAddMenu()
{
int i;
cout << "How many students would you like to add? (Max 10) ";
cin >> addStudentNumber;
Students student[10];
for (i = 0; i < addStudentNumber; i++)
{
cout << "Student ID: ";
cin >> student[i].studentID;
cout << "Students first name: ";
cin >> student[i].firstName;
cout << "Student last name: ";
cin >> student[i].lastName;
cout << "Student email: ";
cin >> student[i].email;
}
}
void studentLookup()
{
Students student[10];
for (int i = 0; i < 10; i++)
{
cout << "Student ID: " << student[i].studentID << endl;
cout << "Student first name: " << student[i].firstName << endl;
cout << "Student last name: " << student[i].lastName << endl;
cout << "Student email: " << student[i].email << endl;
}
}
int main()
{
do
{
showMainMenu();
cin >> selector;
if (selector == 1)
{
showStudentAddMenu();
}
else if (selector == 2)
{
studentLookup();
}
else if (selector == 3)
{
}
else if (selector == 4)
{
}
else if (selector == 5)
{
exit(0);
}
} while (selector != 5);
return 0;
}
but I'm struggling to pull the data from the structure itself, I just have a bunch of random characters being returned after I input a users data, and try to pull it.
Student ID: -858993460
Student first name: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ɺm0ê²V
Student last name: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ɺm0ê²V
Student email: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ɺm0ê²V
I have a feeling I need to include pointers somehow but I am completely lost and any help would be appreciated, thanks!

From my gesture, you've called all the 10 students struct, since not all of them are filled by the user, it shows some garbage. I've made a dynamic program which reads the limit by the user and defines the arrayed struct.
Look at the following code:
#include <iostream>
const int MAX = 10;
int limit = 0;
struct Students {
int studentID;
string firstName;
string lastName;
string email;
} student[MAX];
void addStudentsPrompt(void);
void studentLookup(void);
int main(void) {
addStudentsPrompt();
studentLookup();
return 0;
}
void addStudentsPrompt(void) {
std::cout << "How many students to register? (Max 10) ";
std::cin >> limit;
for (int i = 0; i < limit; i++) {
std::cout << "Student ID: ";
std::cin >> student[i].studentID;
std::cout << "Students first name: ";
std::cin >> student[i].firstName;
std::cout << "Student last name: ";
std::cin >> student[i].lastName;
std::cout << "Student email: ";
std::cin >> student[i].email;
std::cout << std::endl;
}
}
void studentLookup(void) {
int getSize = sizeof(student) / sizeof(student[0]);
for (int i = 0; i < limit; i++) {
std::cout << "Student ID: " << student[i].studentID << std::endl
<< "Student first name: " << student[i].firstName << std::endl
<< "Student last name: " << student[i].lastName << std::endl
<< "Student email: " << student[i].email << std::endl << std::endl;
}
}
Simply two void functions used which sets and gets the data defined by the user. I recommend you using the string rather than applying character array for better results as I've done in the above code.
Example Output
// --- stdin ---
How many students to register? (Max 10) 2
Student ID: 100
Students first name: Hello
Student last name: World
Student email: Helloworld#gmail
Student ID: 101
Students first name: john
Student last name: Doe
Student email: johndoe#something
// --- stdout ---
Student ID: 100
Student first name: Hello
Student last name: World
Student email: Helloworld#gmail
Student ID: 101
Student first name: john
Student last name: Doe
Student email: johndoe#something
Enjoy Coding!

Students should be named Student because it only contains information about one student.
You currently declare Students student[10]; locally in each function. Each array is unique and what you enter in one function will not be visible outside the function in which it was declared. You need to pass the information to the functions using it.
Since the user of the program is free to enter any number of students, use a std::vector to store them. A std::vector can grow dynamically.
Instead of having a char[] with a hardcoded lenght for name etc., use std::string. It will, just like std::vector, grow dynamically.
Example:
#include <iostream>
#include <string>
#include <vector>
struct Student {
int studentID;
std::string firstName;
std::string lastName;
std::string email;
};
// a function to print one student
std::ostream& operator<<(std::ostream& os, const Student& s) {
return os
<< "Student ID: " << s.studentID << '\n'
<< "Student first name: " << s.firstName << '\n'
<< "Student last name: " << s.lastName << '\n'
<< "Student email: " << s.email << '\n'
;
}
// return a std::vector<Student>
std::vector<Student> showStudentAddMenu()
{
using std::cin, std::cout;
std::vector<Student> students;
cout << "How many students would you like to add? ";
// check that extraction was successful before using addStudentNumber:
if(size_t addStudentNumber; cin >> addStudentNumber) {
students.reserve(addStudentNumber);
Student s;
for (size_t i=0; i<addStudentNumber; ++i)
{
cout << "Student ID: ";
cin >> s.studentID;
cout << "Students first name: ";
cin >> s.firstName;
cout << "Student last name: ";
cin >> s.lastName;
cout << "Student email: ";
if(cin >> s.email)
students.push_back(s); // store one student
else
break; // if input failed
}
}
return students;
}
void studentLookup(const std::vector<Student>& students)
{
// print all students
for (const Student& s : students)
{
std::cout << s; // using our operator<< that we defined under Student
}
}
int main() {
std::vector<Student> students = showStudentAddMenu();
studentLookup(students);
}

Fields of the struct are not null-terminated, thus they are not valid C-strings and cannot be printed like ones.
For a struct defined this way compiler would know the size of each field, so you can print them with something like:
Student student {...};
std::cout << std::string_view(student.firstName, sizeof(Students::firstName)) << std::endl;

Related

How do I add vectors into my structs to create an inventory system in which I can add multiple different wines to the system using only one struct?

I have a school assignment in which I have to create a Wine Inventory System where the user can add multiple different wines without a finite number I assume.
I need to create vectors but I'm not sure. I don't know what to try.
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Wine1
{ //struct for Wine pssibly needs Vector
string name;
string year;
string place;
string price;
} wine;
void printwine(Wine1 wine);
int main()
{
string str; //input for data
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
cout << endl;
printwine2(wine2);
cout << endl;
printwine3(wine3);
}
void printwine(Wine1 wine)
{ //data the user typed as output
cout << "Wine1" << endl;
cout << "the name is: " << wine.name << endl;
cout << "it's year is: " << wine.year << endl;;
cout << "its country of creation is: " << wine.place << endl;;
cout << "it's price is: " << wine.price << endl;
}
It should output the name of the wine, the year, it's country, and it's price, for each wine which was added.
A good starting should be using vector of Wine1.
std::vector<Wine1> wineVec;
wineVec.reserve(/*size*/) // reserve the memory if you know the number of wines beforehand
The printwine function should now take std::vector<Wine1>(preferably const-reference as the data is read-only) and iterate through the vector to print the attributes of the Wine1.
Something like:
#include <string>
#include <iostream>
#include <vector>
void printwine(const std::vector<Wine1>& vecWine)
{
for (const auto& wine : vecWine)
{
// do printing each: wine.name, wine.year,... so on
}
}
int main()
{
std::vector<Wine1> vecWine;
int wineNumber = 2;
vecWine.reserve(wineNumber);
std::string name, year, place, price;
for (int i = 0; i < wineNumber; ++i)
{
// get the user input for name, year, place, and price
std::cin >> name >> year >> place >> price;
vecWine.emplace_back(Wine1{ name, year, place, price });
}
printwine(vecWine);
}
That said, you should read more about std::vector
to get to know more, how it works.
Also, good to read about, how to overload operator>> and operator<<, so that you could even write code, much simpler.
Following is an incomplete code, which I leave you to complete after covering the topics which I mentioned.
void printwine(const std::vector<Wine1>& vecWine)
{
for (const auto& wine : vecWine)
{
std::cout << wine << '\n';
}
}
int main()
{
std::vector<Wine1> vecWine(wineNumber);
for (Wine1& wine : vecWine)
{
std::cin >> wine;
}
printwine(vecWine);
}
I believe there is a misunderstanding: you do not intend your struct Wine1 to contain a vector but instead, you want a vector of Wine1's.
I suggest a data structure similar to the following:
struct Wine {
string name;
string year;
string place;
string price;
};
void printwinelist(vector<Wine>& list){
for(Wine& w : list){
printwine(w);
}
}
vector<Wine> winelist;
The main method has to be rewritten accordingly, to append additional objects to the vector.
While I could rewrite your code accordingly I suspect, that a better next step for you would be to read up on some of the concepts used, such as vectors.
You probably want something like this:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Wine1
{ //struct for Wine pssibly needs Vector
string name;
string year;
string place;
string price;
};
void printwine(Wine1 wine);
int main()
{
vector<Wine1> wineinventory;
// read 3 wines
for (int i = 3; i < 10; i++)
{
Wine1 wine;
string str; //input for data
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
wineinventory.push_back(wine); // store in vectore
}
// print all wines in the vector
for (int i = 0; i < wineinventory.size(); i++)
{
cout << "Wine " << i << ":" endl;
printwine(wineinventory[i]);
}
}
Disclaimer: this is untested code, I'm not even sure if it compiles, but you should get the idea.
There is still much room for improvement.

Loop over list of objects skipped

I'm making a list of Student Objects, and want to iterate over them and output the values they have. I'm not sure why the for loop is being skipped over. Any help/guidance would be appreciated. Here is the loop:
void studentInfo(list<Student> stuList) {
cout << "in studentinfo" << endl;
for (list<Student>::iterator it = stuList.begin(); it != stuList.end(); ++it) {
cout << "in student info loop" << endl;
cout << it->toString();
}
cout << "after loop" << endl;
}
I get the cout messages that are right before and after the loop. Here is the toString() method if you need.
string Student::toString() {
stringstream outString;
outString << "Name: " << name << "\nID: " << id << "\nAge: " << age << endl;
return outString.str();
}
This is how the list is being populated, I'm adding 2 student objects usually when testing:
void addToList(list<Student> stuList) {
string tempName;
int tempID;
int tempAge;
int numStudents;
cout << "How many students will you be entering?\n";
cin >> numStudents;
for (int i = 0; i < numStudents; i++) {
cout << "Enter student name: \n";
cin >> tempName;
cout << "Enter student id: \n";
cin >> tempID;
cout << "Enter student age: \n";
cin >> tempAge;
stuList.push_back(Student(tempName, tempID, tempAge));
}
}

Try to do cin >> string[array] and there is an error (""string subscript out of range")

The code :
class DataBase {
private:
int age, stuNumNew;
string stuName, command;
string ver = "Alpha";
float stuNum = 1;
public:
void Start() {
cout << "Welcome back to Sudent DataBase <ver " << ver << ">." << endl;
cout << "Type 'Add' for add a student.";
cin >> command;
if (command == "Add") {
Add();
}
else if (command == "Search") {
cout << "Student number: ";
cin >> stuNumNew; //Get input from user
cout << "Name: " << stuName[stuNumNew] << endl << "Age: " << age[&stuNumNew] << endl;
Start();
}
}
void Add() {
cout << "Name: ";
**cin >> stuName[stuNum] * *; //get the name
cout << "Age: ";
cin >> age[stuNum]; //get the age
cout << "Student Number: " << stuNum;
stuNum = stuNum++;
stuNumNew = stuNum;
Start();
}
};
Vectors allow you to create variable size arrays
The issue you're having is caused by using a fixed size variable. What you need in this case is a vector because what you are trying to do is assign names and numbers to arrays that are of variable sizes so that you can add new students in your program.
Vectors allow you to do just that.
Here is the modified code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class DataBase {
private:
int searchInput;
/* declare a vector of type int to contain your students' ages */
vector<int> studentAges;
/* declare a vector of type std::string to contain your students' names */
vector<string> studentNames;
string command;
string ver = "Alpha";
/* stuNum is removed because vectors keep track of their own sizes */
public:
void Start() {
cout << "Welcome back to Student DataBase <ver " << ver << ">." << endl;
cout << "Type 'Add' to add a new student or type 'Search' to search the database of students." << endl << " > ";
cin >> command;
if (command == "Add") {
Add();
} else if (command == "Search") {
/* You may want to build in a conditional that checks if there are any students yet. */
cout << "Student number: ";
cin >> searchInput;
if (searchInput < studentNames.size()) {
/* Make sure that the users input (the number) is actually a student's id number
* otherwise reject their input to keep the program from crashing or having undefined behavior.
*/
cout << "Name: " << studentNames[searchInput] << endl << "Age: " << studentAges[searchInput] << endl;
} else {
cout << "Student number invalid" << endl;
}
} else {
/* Tell them if their input is invalid */
cout << "Invalid option." << endl;
}
Start();
}
void Add() {
/* Append new positions for input */
studentNames.push_back("");
studentAges.push_back(0);
cout << "Name: ";
cin >> studentNames.back();
cout << "Age: ";
cin >> studentAges.back();
/* The student number is the newest index of the vector */
cout << "Student Number: " << studentNames.size() - 1 << endl;
}
};
int main() {
DataBase d;
d.Start();
}
You can also combine your variables studentNames and studentAges into a students vector if you create a class (or struct) that keeps both values together:
class Student {
public:
Student(string name, int age) {
this->age = age;
this->name = name;
}
string name;
int age;
};
Then you can use it with vector:
vector<Student> students;
NOTE:
You may want to add some input protecting because if you enter a string where it wants a number it breaks the program.

How can I solve this pointer/memory issue?

I trying to write a program that asks the user for movie information. stores the information of a movie as a struct in a vector and then output the result to the screen with the 2 functions having a return type of void.
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
void make_movie(struct movie *film);
void show_movie(vector <movie> data, int cnt);
struct movie {
string name;
string director;
int year;
int duration;
};
int main() {
int count = 0;
char input;
vector <movie> record;
movie *entry = nullptr;
do {
make_movie(entry);
record.push_back(*entry);
count++;
cout << endl;
cout << "Do you have more movie info to enter?\n";
cout << "Enter y / Y for yes or n / N for no: ";
cin.ignore();
cin >> input;
cout << endl;
} while (input == 'y' || input == 'Y');
show_movie(record, record.size());
return 0;
}
void make_movie(struct movie *film) {
cout << "Enter the title of the movie: ";
cin.ignore();
getline(cin, film -> name);
cout << "Enter the director's name: ";
cin.ignore();
getline(cin, film -> director);
cout << "Enter the year the movie was created: ";
cin >> film -> year;
cout << "Enter the movie length (in minutes): ";
cin >> film -> duration;
}
void show_movie(vector <movie> data, int cnt) {
cout << "Here is the info that you entered: " << endl;
for (int i = 0; i < cnt; i++) {
cout << "Movie Title: " << data[i].name << endl;
cout << "Movie Director: " << data[i].director << endl;
cout << "Movie Year: " << data[i].year << endl;
cout << "Movie Length: " << data[i].duration << endl;
cout << endl;
}
}
I am getting a error that says that i am trying to access a prohibited memory address.
The least amount of changes you need to make is to change:
movie *entry = nullptr;
do {
make_movie(entry);
record.push_back(*entry);
to:
movie entry;
do {
make_movie(&entry);
record.push_back(entry);
Further improvements would be:
Change make_movie to accept parameter by reference, then your program does not use any pointers and therefore is not vulnerable to any of the problems associated with pointers.
Change make_movie to return by value instead of taking a reference parameter.
cin.ignore(); is being used incorrectly. Your program will lose the first character of several of the input strings. Instead, remove all of those calls, and at the end of the make_movie function, ignore the rest of the current line. Also, change cin >> input; to use getline.
your bug
movie *entry = nullptr;
and
you have extra cin.ignore();
cout << "Enter the title of the movie: ";
// cin.ignore();
getline(cin, film -> name);
cout << "Enter the director's name: ";
// cin.ignore();
getline(cin, film -> director);
how to fix
movie main_info;
movie* entry = &main_info;
test
intput:
Enter the title of the movie: any_thing
Enter the director's name: yourself
Enter the year the movie was created: 2016
Enter the movie length (in minutes): 120
Do you have more movie info to enter?
Enter y / Y for yes or n / N for no: n
output
Here is the info that you entered:
Movie Title: any_thing
Movie Director: yourself
Movie Year: 2016
Movie Length: 120

Create a vector of base class objects and store derived class objects within

I am trying to create an employee database (Vector of Employees). There are 3 types of employees ie. employees is the base class and Manager, Engg and Scientist are derived class.
Every employee has first name and last name. In addition to the name, each of the 3 types of employees have unique stats ie. Manager has number of meetings/week whereas the Engg has work experience and so on.
I have a couple of questions
1. Should I upcast the derived objects to the base class or downcast the base class to the derived class?
2. How do I use polymorphism to override methods, since I want the user to add an employee type and based on the type selected the respective entry fields should appear ie. in case of a Manager, in addition to the first and last names, the program should also ask for meetings/week?
Here is my Class file
class Employee{
public:
Employee();
Employee(string fName, string lName, int sal);
virtual void printEmp();
string getFirstName();
string getLastName();
protected:
string m_fName;
string m_lName;
int m_sal;
};
class Manager : public Employee{
public:
Manager();
Manager(string fName, string lName, int sal, int meets, int hols);
void printEmp();
protected:
int m_meets;
int m_hols;
};
Here is the implementation
Employee::Employee(){
m_fName = "Default";
m_lName = "Default";
m_sal = 0;
}
Employee::Employee(string fName, string lName, int sal){
m_fName = fName;
m_lName = lName;
m_sal = sal;
}
void Employee::printEmp(){
cout << "First Name: " << m_fName << endl
<< "Last Name: " << m_lName << endl
<< "Salary: " << m_sal << endl;
}
string Employee::getLastName(){
return m_lName;
}
string Employee::getFirstName(){
return m_fName;
}
Manager::Manager(string fName, string lName, int sal, int meets, int hols) : Employee(fName, lName, sal), m_meets(meets), m_hols(hols)
{
//empty
}
void Manager::printEmp(){
Employee::printEmp();
cout << "Meets/Week: " << m_meets << endl
<< "Holidays/Year: " << m_hols << endl << endl;
Here is the main
int main(){
bool exit = false;
vector<Employee*> dBVector;
while (!exit){
cout << "Welcome to Employee Database, Enter an option to continue..." << endl;
cout << "1) Add an Employee, 2) Delete an Employee, 3) Save Database, 4) Exit" << endl;
int input;
cin >> input;
string fNameInp;
string lNameInp;
int salInp;
string lNameSearch;
int i; // for loop in Delete employee case
bool deleted = false;
switch (input){
case 1: //Add
cout << "1) Add a Manager, 2) Add an Engg, 3) Add a Researcher" << endl;
int empInput;
cin >> empInput;
if (empInput == 1){
cout << "Enter First Name: ";
cin >> fNameInp;
cout << "Enter Last Name: ";
cin >> lNameInp;
cout << "Enter Salary: ";
cin >> salInp;
cout << "Number of meetings/week: ";
int meetsInp;
cin >> meetsInp;
cout << "Number of holidays/year: ";
int holsInp;
cin >> holsInp;
Manager mEmp(fNameInp, lNameInp, salInp, meetsInp, holsInp);
Employee &emp = mEmp;
dBVector.push_back(&mEmp);
dBVector[dBVector.size()-1]->printEmp();
}
else if (empInput == 2){
cout << "Enter First Name: ";
cin >> fNameInp;
cout << "Enter Last Name: ";
cin >> lNameInp;
cout << "Enter Salary: ";
cin >> salInp;
cout << "Cpp Experience (Y/N): ";
string cppInp;
cin >> cppInp;
cout << "Years of experience: ";
float expInp;
cin >> expInp;
cout << "Engg Type (Chem, Mech, IT): ";
string typInp;
cin >> typInp;
Engg eEmp(fNameInp, lNameInp, salInp, cppInp, expInp, typInp);
Employee &emp = eEmp;
dBVector.push_back(&eEmp);
dBVector[dBVector.size() - 1]->printEmp();
}
else if (empInput == 3){
cout << "Enter First Name: ";
cin >> fNameInp;
cout << "Enter Last Name: ";
cin >> lNameInp;
cout << "Enter Salary: ";
cin >> salInp;
cout << "School of PhD: ";
string schoolInp;
cin >> schoolInp;
cout << "Topic of PhD: ";
string topImp;
cin >> topImp;
Researcher rEmp(fNameInp, lNameInp, salInp, schoolInp, topImp);
Employee &emp = rEmp;
dBVector.push_back(&rEmp);
dBVector[dBVector.size() - 1]->printEmp();
}
break;
case 2: // Delete Emp
for (int x = 0; x < dBVector.size(); x++){
dBVector[x]->getLastName();
cout << endl;
}
cout << "Input Last name of the employee to delete: " << endl;
cin >> lNameSearch;
for (i = 0; i < dBVector.size(); i++){
if (dBVector[i]->getLastName() == lNameSearch){
dBVector.erase(dBVector.begin() + i);
cout << dBVector[i]->getFirstName() << "has been deleted from database";
deleted = true;
break;
}
}
if (deleted == false && i == dBVector.size()){
cout << "No Employee with Last Name - " << lNameSearch << " exists in Database." << endl;
}
else
break;
case 3: //save
cout << "saving..." << endl;
break;
case 4: //exit
exit = true;
break;
}
}
}
Please Help!
Firstly, if you want to use polymorphism you need to store pointers in your vector. As the vector is the sole owner of the employees something like std::vector<std::unique_ptr<Employee>> would be suitable.
Edit: I see you have updated the vector to use pointers. But you are storing a pointer to a local stack allocated object, e.g mEmp. This will not work, when the mEmp variable goes out-of-scope at the closing brace the object will be deleted and you will be left with a dangling pointer in your vector that points to a deleted object. Using this dangling pointer is undefined behaviour. You need to allocate the Manager on the heap using new. Then the object will not be deleted when the variable goes out-of-scope but you do need to remember to delete the object when you are done. Something like unique_ptr makes this easy.
Regarding your questions:
Try to minimize explicit casting, especially downcasting. At the point that you store the Employee in the vector it will be implicitly upcast from the derived class to Employee but other than that there is not much need for casting.
You have roughly the right idea when it comes to overriding methods, if you call the virtual printEmp method on an Employee pointer it will call the override in the derived class.
If you are happy for the user input to be the responsibility of the Employee classes you could simply add a virtual method that initializes the employee using suitable input from the user. But I would be tempted to keep that separate from your domain objects. You need a switch statement on the user choice of employee type anyway so polymorphism doesn't gain you much there.
If you really want to use polymorphism for the employee creation I would suggest using something like the Abstract Factory pattern.
Here is my suggestion anyway:
#include <vector>
#include <string>
#include <iostream>
#include <memory>
class Employee {
public:
Employee(std::string fName, std::string lName, int sal);
virtual ~Employee();
virtual void printEmp();
protected:
std::string m_fName;
std::string m_lName;
int m_sal;
};
class Manager : public Employee {
public:
Manager(std::string fName, std::string lName, int sal, int meets, int hols);
void printEmp() override;
protected:
int m_meets;
int m_hols;
};
Employee::Employee(std::string fName, std::string lName, int sal)
: m_fName(fName), m_lName(lName), m_sal(sal) {
}
Employee::~Employee() {
}
void Employee::printEmp(){
std::cout << "First Name: " << m_fName << "\n"
<< "Last Name: " << m_lName << "\n"
<< "Salary: " << m_sal << "\n";
}
Manager::Manager(std::string fName, std::string lName, int sal, int meets, int hols)
: Employee(fName, lName, sal), m_meets(meets), m_hols(hols){
}
void Manager::printEmp(){
Employee::printEmp();
std::cout << "Meets/Week: " << m_meets << "\n"
<< "Holidays/Year: " << m_hols << "\n";
}
std::unique_ptr<Manager> createManager() {
std::cout << "Enter First Name: ";
std::string fNameInp;
std::cin >> fNameInp;
std::cout << "Enter Last Name: ";
std::string lNameInp;
std::cin >> lNameInp;
std::cout << "Enter Salary: ";
int salInp;
std::cin >> salInp;
std::cout << "Number of meetings/week: ";
int meetsInp;
std::cin >> meetsInp;
std::cout << "Number of holidays/year: ";
int holsInp;
std::cin >> holsInp;
std::cout << "\n";
return std::make_unique<Manager>(fNameInp, lNameInp, salInp, meetsInp, holsInp);
}
std::unique_ptr<Employee> createEmployee() {
int input;
std::cout << "1) Add a Manager, 2) Add an Engg, 3) Add a Researcher\n";
std::cin >> input;
switch (input){
case 1:
return createManager();
default:
return nullptr;
}
}
int main() {
std::vector<std::unique_ptr<Employee>> dBVector;
std::cout << "Welcome to Employee Database, Enter an option to continue...\n";
std::cout << "1) Add an Employee"
<< ", 2) Delete an Employee"
<< ", 3) Save Database"
<< ", 4) Exit\n";
int input;
std::cin >> input;
switch (input){
case 1:
dBVector.push_back(createEmployee());
break;
default:
break; // Do nothing
}
dBVector.at(0)->printEmp();
}
Live demo
You may want to store pointers in the vector to avoid slicing, as others mentioned. Then each employee could have their own input method and ask the right questions to initialize themselves (main would not implement that but only call the respective employee's virtual input function). Input and output are conceptually sysmmetrical operations which hints at implementing them symmetrically, i.e. in this case both as member functions.