Writing from file to class - c++

Making a project I have to write patient database and I have a problem. Looking through guids in iтternet I dont understand how to read from file to class and how to write class into file(I realized it using common input, it is better to rewrite it).
And I dont understand how to make base constructor for char, like with int
point()
{
x=y=z=0;
}
need like
Patient()
{
name="Mike";
surename="Smith";
adress="New York";
ilness="Cold"
card_number=1;
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int add();
int read();
string path = "base.txt";
class Patient
{
private:
char *name;
char *surename;
char *adress;
char *ilness;
int *card_number;
public:
void set()
{
cout << "Enter name:";
cin >> name;
cout << "Enter surename:";
cin >> surename;
cout << "Enter adress:";
cin >> adress;
cout << "Enter diagnosis:";
cin >> ilness;
cout << "Enter card number:";
cin >> *card_number;
}
void show()
{
cout << "Name:" << name << endl;
cout << "Surename:" << surename << endl;
cout << "Adress:" << adress << endl;
cout << "Cardnumber:" << card_number << endl;
cout << "Diagnosis:" << ilness << endl;
}
void print()
{
cout << name << surename << adress << ilness << card_number << endl;
}
Patient()
{
}
Patient(const char* s)
{
int length = 0;
while (s[length++] != ' ')
;
name = new char[length];
for (int i = 0; i < length; i++)
name[i] = s[i];
length = 0;
while (s[length++] != ' ')
;
surename = new char[length];
for (int i = 0; i < length; i++)
surename[i] = s[i];
length = 0;
while (s[length++] != ' ')
;
adress = new char[length];
for (int i = 0; i < length; i++)
adress[i] = s[i];
length = 0;
while (s[length++] != ' ')
;
ilness = new char[length];
for (int i = 0; i < length; i++)
ilness[i] = s[i];
length = 0;
while (s[length++] != '\n')
;
card_number = new int[length];
for (int i = 0; i < length; i++)
card_number[i] = s[i];
}
};
int main()
{
char a;
cout << "Choose the action:" << endl;
cout << "a. Add new patient" << endl;
cout << "b. Delete patient" << endl;
cout << "c. Find for card number" << endl;
cout << "d. Find for ilnesses" << endl;
cin >> a;
switch (a)
{
case 'a':
add();
break;
case 'b':
read();
break;
default:
cout << "error" << endl;
}
system("pause");
return 0;
}
int add()
{
fstream file;
file.open(path, fstream::out | fstream::in | ofstream::app);
if (!file.is_open())
{
cout << "Error" << endl;
}
else
{
string str;
cout << "Opend" << endl;
cout << "Enter name" << endl;
cin >> str;
file << str<<" ";
cout << "Enter surename" << endl;
cin >> str;
file << str << " ";
cout << "Enter adress" << endl;
cin >> str;
file << str << " ";
cout << "Enter ilness" << endl;
cin >> str;
file << str << " ";
cout << "Enter card number" << endl;
cin >> str;
file << str<<"\n";
cout << "Sucsesfully added\n";
}
file.close();
return 0;
}
int read()
{
fstream file;
file.open(path, fstream::out | fstream::in | ofstream::app);
if (!file.is_open())
{
cout << "Error" << endl;
}
else
{
cout << "Opend" << endl;
string str;
Patient first;
while (file.read((char*)&first,sizeof(Patient)))
{
first.print();
}
}
file.close();
return 0;
}
Database example
Mike Smith New_York Cold 1
Charles Williams London Flu 2
Henry Roberts York Coronovirus 3
Robert Garcia Sydney Cold 4
methods how to write from file to class and from class to file

If you want to serialise your data structures, it's best to use a library and data format, such as JSON.
Personally, I recommend nlohmann::json because of it's ease of use and flexibility.
Overloading the constructor and serialising data
Overloading your constructor is nothing other than overloading another method in C++.
If your class is Patient:
#include <string>
#include <nlohmann/json.hpp>
using nlohmann::json;
using std::string; // Don't use using namespace std;!
class Patient {
public:
Patient() = default;
explicit Patient(const string& name, const string& surname,
const string& addr, const string& illness):
m_name(name), m_surname(surname), m_address(addr), m_illness(illness) {}
explicit Patient(const json& data) { fromJson(data); }
virtual ~Patient() = default;
json toJson() const {
return {
{ "Name", m_name },
{ "Surname", m_surname },
{ "Address", m_address },
{ "Illness", m_illness }
};
}
void fromJson(const json& data) {
if (data.contains("Name") && data["Name"].is_string()) {
m_name = data["Name"].get<string>();
} // and so on
}
void dumpJson(const string& filePath) const {
ofstream out(path, std::ios::out);
// error checking
out << filePath;
}
private: // members
string m_name;
string m_surname;
string m_address;
string m_illness;
};
I haven't tested this code and won't guarantee it will work first try, but it should point you in the right direction. This code requires at least C++11.

Related

Passing a struct array and updating array in C++

For one of my options, I had to add a row of data to my struct array, I was able to update the array but it does not update the actual array. So I am trying to update my struct array by passing it as a reference. I placed the '&' sign before the name of the variable, but all my indexes within the function errored.. I'm still a newbie to programing and I'm not "allowed " to use any vectors according to my Professor. . I looked online and placed the '&' on the function name as well as on the prototype. I also changed it from void to the "data type" to return the updated array, but no luck... I hope this comes out clear....
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
struct Data {
string first_name;
string last_name;
int employee_id;
string phone_number;
};
void readFile( ifstream& , string, Data [],int&);
void getEmployeeDetail(Data [], int&);
void getEmployeeLast(Data [], int&);
Data getAddEmployee(Data &, int&);
void getRemoveEmployee();
void getManagerOnly();
void getStaffOnly();
const int SIZE = 15;
Data employee[SIZE];
int main() {
ifstream inputFile;
ofstream outputFile;
int count = 0;
int selection;
const int EMPLOYEE_DETAIL = 1,
EMPLOYEE_LAST = 2,
ADD_EMPLOYEE = 3,
REMOVE_EMPLOYEE = 4,
MANAGER_ONLY = 5,
STAFF_ONLY = 6,
QUIT = 7;
readFile( inputFile ,"employeeData.txt", employee,count);
do {
cout << "\t Please select your option.\n"
<< "1. List all employee details\n"
<< "2. List employee by last name\n"
<< "3. Add a new employee\n"
<< "4. Remove an employee\n"
<< "5. Show all managers only\n"
<< "6. Show all staff only\n"
<< "7. Quit\n"
<< "Please enter your selection.\n" << endl;
cin >> selection;
while (selection < EMPLOYEE_DETAIL || selection > QUIT) {
cout << " Pleasse enter a valid selection.\n";
cin >> selection;
}
switch (selection) {
case EMPLOYEE_DETAIL:
getEmployeeDetail(employee,count);
break;
case EMPLOYEE_LAST:
getEmployeeLast(employee, count);
break;
case ADD_EMPLOYEE:
getAddEmployee(employee, count);
break;
case REMOVE_EMPLOYEE:
getRemoveEmployee();
break;
case MANAGER_ONLY:
getManagerOnly();
break;
case STAFF_ONLY:
getStaffOnly();
break;
case QUIT:
cout << "Program ending\n";
break;
}
} while (selection != QUIT);
system("pause");
return(0);
}
void readFile(ifstream& inputFile, string data, Data employee[],int &count) {
inputFile.open(data);
if (!inputFile) {
cout << " Error in opening file\n";
exit(1);
}
else {
while (!inputFile.eof()) {
inputFile >> employee[count].first_name;
inputFile >> employee[count].last_name >> employee[count].employee_id >> employee[count].phone_number;
count++;
}
inputFile.close();
}
return;
};
void readFile(ifstream& inputFile, string data, Data employee[],int &count) {
inputFile.open(data);
if (!inputFile) {
cout << " Error in opening file\n";
exit(1);
}
else {
while (!inputFile.eof()) {
inputFile >> employee[count].first_name;
inputFile >> employee[count].last_name >> employee[count].employee_id >> employee[count].phone_number;
count++;
}
inputFile.close();
}
return;
};
void getEmployeeDetail( Data employee[], int& count) {
cout << "Firt Name" << "\t" << "Last Name " << "\t" << "Employee ID " << "\t" << "Phone Number \n";
cout << "--------------------------------------------------------------------------\n";
for(int i =0; i < count; i++)
cout <<employee[i].first_name<<"\t\t"<<employee[i].last_name<<"\t\t"<<employee[i].employee_id<<"\t\t"<<employee[i].phone_number<< "\n ";
cout << endl;
}
void getEmployeeLast(Data employee[], int& count) {
string searchName;
cout << "Please enter the last name of the employee you want to search for.\n";
cin >> searchName;
/*
for (int i = 0; i < count; i++)
if (searchName ==employee[i].last_name) {
cout << employee[i].last_name;
}
*/
string matchString = searchName;
for (int i = 0; i < count; i++) {
if (employee[i].last_name.find(matchString, 0) != std::string::npos) {
cout << employee[i].last_name << endl;
}
else {
cout << "No employee was located with that name.\n";
exit(1);
}
}
}
Data getAddEmployee(Data &employee, int& count) {
string firstName, lastName, phoneNumber;
int employeeID, size;
for (int i = 0; i < count; i++)
cout << employee[i].first_name << "\t\t\t" << employee[i].last_name << "\t\t" << employee[i].employee_id << "\t\t" << employee[i].phone_number << "\n ";
cout << endl;
cout << count <<"\n";
size = count + 1;
cout << size << "\n";
cout << "Please enter the new employee First Name.\n";
cin >> firstName;
cout << "Please enter the new employee Last Name.\n";
cin >> lastName;
cout << "Please enter the new employee's ID.\n";
cin >> employeeID;
cout << "Please enter the new employee Phone Number.\n";
cin >> phoneNumber;
employee[10].first_name = firstName;
employee[10].last_name = lastName;
employee[10].employee_id = employeeID;
employee[10].phone_number = phoneNumber;
for (int i = 0; i < size; i++) {
cout << employee[i].first_name << "\t\t\t" << employee[i].last_name << "\t\t" << employee[i].employee_id << "\t\t" << employee[i].phone_number << "\n ";
cout << endl;
}
return employee;
}
First of all, your naming could be better. Like employeeList instead employee to specify its a list/array of employee, also addEmployee instead of getAddEmployee.
Second, instead of pasting whole code, it's better to paste the relevant block of code based on your question to make it clearer.
The solution is, your array is basically already on global scope, you don't need to pass it. Can just directly modify the array.
struct Data {
string first_name;
string last_name;
int employee_id;
string phone_number;
};
void addEmployee();
const int SIZE = 15;
Data employeeList[SIZE];
int current = 0;
int main(){
Data emp1 = {"John", "Doe", 100, "12345"};
employeeList[0] = emp1;
current++;
addEmployee();
for(int i=0; i<current; i++){
Data emp = employeeList[i];
cout<<i+1<<" "<<emp.first_name<<" "<<emp.phone_number<<endl;
}
}
void addEmployee(){
Data emp2 = {"Sam", "Smith", 200, "67890"};
employeeList[current] = emp2;
current++;
}
Can try it here: https://onlinegdb.com/drhzEOpis

String Type Vector C++

In the code I shared below, I want to output vector data type in the AddStoreCustomer() section. This is how the code works. But there is a bug in the code. If I enter a single word in the customer variable, the code works fine. But when I enter two words in this variable with a space between them,the code doesn't work.
#include <iostream>
#include <vector>
using namespace std;
class Store {
int StoreID;
string StoreName;
string StoreCity;
string StoreTown;
string StoreTel;
vector <string> StoreCustomer;
public:
Store(){}
Store(int sid, string sname, string scity, string stown, string stel, string scustomer) {
setStoreID(sid);
setStoreName(sname);
setStoreCity(scity);
setStoreTown(stown);
setStoreTel(stel);
setStoreCustomer(scustomer);
}
void setStoreID(int sid) {
StoreID = sid;
}
void setStoreName(string sname) {
StoreName = sname;
}
void setStoreCity(string scity) {
StoreCity = scity;
}
void setStoreTown(string stown) {
StoreTown = stown;
}
void setStoreTel(string stel) {
StoreTel = stel;
}
void setStoreCustomer(string scustomer) {
StoreCustomer.push_back(scustomer);
}
int getStoreID() {
return StoreID;
}
string getStoreName() {
return StoreName;
}
string getStoreCity() {
return StoreCity;
}
string getStoreTown() {
return StoreTown;
}
string getStoreTel() {
return StoreTel;
}
vector <string> getStoreCustomer() {
return StoreCustomer;
}
~Store(){}
};
Store s2[50];
void AddStore() {
int id, menu;
string name, city, town, tel;
for (int i = 0; i < 1; i++) {
cout << "Lutfen Magaza ID Numarasini Girin: ";
cin >> id;
s2[i].setStoreID(id);
cout << "Lutfen Magaza Adini Girin: ";
cin >> name;
s2[i].setStoreName(name);
cout << "Lutfen Magazanin Bulundugu Ili Girin: ";
cin >> city;
s2[i].setStoreCity(city);
cout << "Lutfen Magazanin Bulundugu Ilceyi Girin: ";
cin >> town;
s2[i].setStoreTown(town);
cout << "Lutfen Magazanin Telefon Numarasini Girin: ";
cin >> tel;
s2[i].setStoreTel(tel);
cout << "Magaza Eklendi" << endl;
cout << "\n\n";
}
cout << "Menuye Donmek icin 0 " << endl;
cin >> menu;
if (menu == 0) {
StoreMenu();
}
else {
cout << "Tanımlanamayan Giris!!!" << endl;
//Menu();
}
}
int Search2(const int& y) {
for (int j = 0; j < 100; j++) {
if (s2[j].getStoreID() == y)
return j;
}
return -1;
}
void AddStoreCustomer() {
int id, found, menu;
string customer;
cout << "Lutfen Musteri Eklemek Istediginiz Magazanin ID Numarasini Girin:";
cin >> id;
found = Search2(id);
if (found == -1)
{
cout << "Magaza Bulunamadi" << endl;
}
else {
cout << "Magaza Bulundu.\n" << "Lutfen Magaza Veri Tabanina Eklemek Istediginiz Musterinin Bilgilerini Girin: ";
cin >> customer;
s2[found].setStoreCustomer(customer);
cout << "Girilen Musteri Bilgisi Secilen Magazaya Eklendi";
cout << "Magaza ID: " << s2[found].getStoreID() << "\n Magaza Adi: " << s2[found].getStoreName() << "\n Magazanin Bulundugu Il: " << s2[found].getStoreCity() << "\n Magazanin Bulundugu Ilce:" << s2[found].getStoreTown() << "\n Magaza Iletisim Numarasi: " << s2[found].getStoreTel() << endl;
cout << "Musteriler: " << endl;
for (int i = 0; i < s2[found].getStoreCustomer().size(); i++)
{
cout << "\t\t" << s2[found].getStoreCustomer()[i] << endl;
}
}
cout << "Tekrar Giris Yapmak icin 1 Menuye Donmek icin 0 " << endl;
cin >> menu;
if (menu == 0) {
StoreMenu();
}
else if (menu == 1) {
AddStoreCustomer();
}
This happens because you are using cin>> to read input, which reads until the first whitespace symbol. If you want to read a whole line, consider using std::getline() instead:
getline(cin, customer);

How to delete stored data in a text file?

The code below was modified based on my original one to fix an error thanks to #RemyLebeau.
It woks great but a new issue have showed up where when I want to delete a student data using option 4 from the menu, after entering their ID, it says their data is deleted but its not. And at the end of the console the message "error reading data from file", under the Standard Error Stream (cerr), keeps showing up. And all my tries to fix it failed.
error_display
//Project on Student Management
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Course
{
string courseName;
string grade;
string status;
};
class Student
{
int id;
int year;
float cgpa;
vector<Course> courses;
string academic_advisor;
public:
Student();
void getData();
void showData() const;
int getID() const { return id; }
friend ostream& operator<<(ostream &out, const Student &s);
friend istream& operator>>(istream &in, Student &s);
};
void writeStr(ostream &out, const string &str)
{
size_t size = str.size();
out.write((char*)&size, sizeof(size));
if (size)
out.write(str.c_str(), size);
}
void readStr(istream &in, string &str)
{
str.clear();
size_t size;
if (in.read((char*)&size, sizeof(size)))
{
if (size > 0)
{
str.resize(size);
in.read(&str[0], size);
}
}
}
ostream& operator<<(ostream &out, const Course &c)
{
writeStr(out, c.courseName);
writeStr(out, c.grade);
writeStr(out, c.status);
return out;
}
istream& operator>>(istream &in, Course &c)
{
readStr(in, c.courseName);
readStr(in, c.grade);
readStr(in, c.status);
return in;
}
ostream& operator<<(ostream &out, const Student &s)
{
out.write((char*)&s.id, sizeof(s.id));
out.write((char*)&s.year, sizeof(s.year));
out.write((char*)&s.cgpa, sizeof(s.cgpa));
writeStr(out, s.academic_advisor);
size_t NumberOfcourses = s.courses.size();
out.write((char*)&NumberOfcourses, sizeof(NumberOfcourses));
for(int i = 0; i < NumberOfcourses; ++i)
{
if (!(out << s.courses[i]))
break;
}
return out;
}
istream& operator>>(istream &in, Student &s)
{
s.courses.clear();
in.read((char*)&s.id, sizeof(s.id));
in.read((char*)&s.year, sizeof(s.year));
in.read((char*)&s.cgpa, sizeof(s.cgpa));
readStr(in, s.academic_advisor);
size_t NumberOfcourses;
if (in.read((char*)&NumberOfcourses, sizeof(NumberOfcourses)))
{
s.courses.reserve(NumberOfcourses);
Course c;
for(size_t i = 0; i < NumberOfcourses; ++i)
{
if (in >> c)
s.courses.push_back(c);
else
break;
}
}
return in;
}
Student::Student()
{
id = 0;
year = 0;
cgpa = 0.0f;
}
void Student::getData()
{
courses.clear();
cout << "\n\nEnter Student Details......\n";
cout << "Enter ID No. : "; cin >> id;
cout << "Enter Intake Year of the Student: "; cin >> year;
cout << "Enter number of Taken courses: ";
size_t NumberOfcourses;
cin >> NumberOfcourses;
cin.ignore();
courses.reserve(NumberOfcourses);
Course c;
for(int a = 0; a < NumberOfcourses; ++a)
{
cout << "\nEnter Subject Name: ";
getline(cin, c.courseName);
cout << "Enter Grade of Subject: ";
getline(cin, c.grade);
cout << "Enter Subject Status: ";
getline(cin, c.status);
courses.push_back(c);
}
cout << "Enter student CGPA: ";
cin >> cgpa;
cin.ignore();
cout << endl;
cout << "Enter Name of Academic Advisor of Student: ";
getline(cin, academic_advisor);
}
void Student::showData() const
{
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << id << endl;
cout << "Intake Year : " << year << endl;
cout << "Subjects Taken in Previous Semester : " << endl;
for(size_t t = 0; t < courses.size(); ++t)
{
cout << "\t" << courses[t].courseName << ": " << courses[t].grade << " (" << courses[t].status << ") ";
cout << endl;
}
cout << "CGPA : " << cgpa << endl;
cout << "Name of academic advisor of Student : " << academic_advisor << endl;
cout << endl;
}
void addData()
{
Student s;
s.getData();
ofstream fout("Students.txt", ios::binary|ios::app);
if (fout << s)
cout << "\n\nData Successfully Saved to File....\n";
else
cerr << "\n\nError Saving Data to File!\n";
}
void displayData()
{
ifstream fin("Students.txt", ios::binary);
Student s;
while (fin >> s)
{
s.showData();
}
if (fin)
cout << "\n\nData Reading from File Successfully Done....\n";
else
cerr << "\n\nError Reading Data from File!\n";
}
void searchData()
{
int n;
cout << "Enter ID Number you want to search for : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Details of ID No. " << n << " are: \n";
s.showData();
flag = true;
break;
}
}
if (fin)
{
if (!flag)
cout << "The ID No. " << n << " not found....\n\n";
cout << "\n\nData Reading from File Successfully Done....\n";
}
else
cerr << "\n\nError Reading Data from File!\n";
}
void deleteData()
{
int n;
cout << "Enter ID Number you want to delete : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
ofstream tout("TrashStud.txt", ios::binary|ios::app);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " has been deleted:\n";
s.showData();
tout << s;
flag = true;
}
else
{
fout << s;
}
}
if (fin)
{
if (fout)
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("tempStud.txt");
}
else
{
remove("Students.txt");
rename("tempStud.txt", "Students.txt");
cout << "\n\nData Successfully Deleted from File....\n";
}
}
else
{
cerr << "\n\nError Saving Data to File!\n";
}
}
else
{
cerr << "\n\nError Reading Data from File!\n";
}
}
void modifyData()
{
int n;
cout << "Enter ID Number you want to Modify : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be modified with new data:\n";
s.showData();
cout << "\n\nNow Enter the New Details....\n";
s.getData();
flag = true;
}
fout << s;
}
if (fin)
{
if (fout)
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("TempStud.txt");
}
else
{
remove("Students.txt");
rename("TempStud.txt", "Students.txt");
cout << "\n\nData Successfully Updated in File....\n";
}
}
else
{
cerr << "\n\nError Saving Data to File!\n";
}
}
else
{
cerr << "\n\nError Reading Data from File!\n";
}
}
void project()
{
int ch;
do
{
system("cls");
cout << "...............STUDENT MANAGEMENT SYSTEM..............\n";
cout << "====================SEGI University=================\n";
cout << "0. Exit from Program\n";
cout << "1. Write Data to File\n";
cout << "2. Read Data From File\n";
cout << "3. Search Data From File\n";
cout << "4. Delete Data From File\n";
cout << "5. Modify Data in File\n";
cout << "Enter your choice : ";
cin >> ch;
system("cls");
switch (ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: modifyData(); break;
}
system("pause");
}
while (ch != 0);
}
int main()
{
project();
}
The error is because fin ( in deleteData () function) runs till the end of file , and at the end fin stores nullptr(boolean conversion to false) that is why else statement runs.And its obvious that why the while loop breaks .
for checking if the file is open or not you can use if(fin.is_open()) for fin and similarly for fout like if(fout.is_open())

How to display all the data I entered into my text file?

The following is a code to enter some details of students into a text file and from the main menu, the user can display them using option 2.
My problem is in the section where it should display all the "Taken Courses by student".
When I choose to display all data, the "mentioned section using the for loop only display the last value I enter when writing to the file.
How can I make it display all my entries?
The issue is under the showdata and displaydata functions.
//Project on Student Management
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
class Student
{
int id;
int year;
char grade[50];
float cgpa;
int NumberOfcourses;
char courseName[100];
char academic_advisor[100];
char status[100];
public:
void getData();
void showData();
int getID(){return id;}
}s;
void Student::getData()
{
cout<<"\n\nEnter Student Details......\n";
cout<<"Enter ID No. : "; cin>>id;
cout << "Enter Intake Year of the Student: "; cin >> year;
cout << "Enter number of Taken courses: ";
cin >> NumberOfcourses;
for(int a=1; a<=s.NumberOfcourses; a++)
{
cout<<"\nEnter Subject Name: ";
cin.ignore();
cin.getline(courseName, 100);
cout << "Enter Grade of Subject: ";
cin >> grade;
cout<<"\nEnter Subject Status: ";
cin.ignore();
cin.getline(status, 100);
}
cout << "Enter student CGPA: ";
cin >> s.cgpa;
cout << endl;
cout << "Enter Name of Academic Advisor of Student: "; cin.ignore();
cin.getline(s.academic_advisor, 100);
}
void Student::showData()
{
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << id << endl;
cout << "Intake Year : " << year << endl;
cout << "Subjects Taken in Previous Semester : " << endl;
for(int t=1; t<=NumberOfcourses; t++)
{
cout << "\t" << courseName << ": " << grade << " ("<< status << ") ";
cout << endl;
}
cout << "CGPA : " << cgpa << endl;
cout << "Name of academic advisor of Student : " << academic_advisor << endl;
cout << endl;
}
void addData()
{
ofstream fout;
fout.open("Students.txt", ios::out|ios::app);
s.getData();
fout.write((char*)&s,sizeof(s));
fout.close();
cout<<"\n\nData Successfully Saved to File....\n";
}
void displayData()
{
ifstream fin;
fin.open("Students.txt",ios::in);
while(fin.read((char*)&s,sizeof(s)))
{
s.showData();
}
fin.close();
cout<<"\n\nData Reading from File Successfully Done....\n";
}
void searchData()
{
int n, flag=0;
ifstream fin;
fin.open("Students.txt",ios::in);
cout<<"Enter ID Number you want to search for : ";
cin>>n;
while(fin.read((char*)&s,sizeof(s)))
{
if(n==s.getID())
{
cout<<"The Details of ID No. "<<n<<" are: \n";
s.showData();
flag++;
}
}
fin.close();
if(flag==0)
cout<<"The ID No. "<<n<<" not found....\n\n";
cout<<"\n\nData Reading from File Successfully Done....\n";
}
void deleteData()
{
int n, flag=0;
ifstream fin;
ofstream fout,tout;
fin.open("Students.txt",ios::in);
fout.open("TempStud.txt",ios::out|ios::app);
tout.open("TrashStud.txt",ios::out|ios::app);
cout<<"Enter ID Number you want to delete : ";
cin>>n;
while(fin.read((char*)&s,sizeof(s)))
{
if(n==s.getID())
{
cout<<"The Following ID No. "<< n <<" has been deleted:\n";
s.showData();
tout.write((char*)&s,sizeof(s));
flag++;
}
else
{
fout.write((char*)&s,sizeof(s));
}
}
fout.close();
tout.close();
fin.close();
if(flag==0)
cout<<"The ID No. "<< n <<" not found....\n\n";
remove("Students.dat");
rename("tempStud.txt","Students.txt");
}
void modifyData()
{
int n, flag=0, pos;
fstream fio;
fio.open("Students.txt", ios::in|ios::out);
cout<<"Enter ID Number you want to Modify : ";
cin>>n;
while(fio.read((char*)&s,sizeof(s)))
{
pos=fio.tellg();
if(n==s.getID())
{
cout<<"The Following ID No. "<<n<<" will be modified with new data:\n";
s.showData();
cout<<"\n\nNow Enter the New Details....\n";
s.getData();
fio.seekg(pos-sizeof(s));
fio.write((char*)&s,sizeof(s));
flag++;
}
}
fio.close();
if(flag==0)
cout<<"The ID No. "<<n<<" not found....\n\n";
}
void project()
{
int ch;
do
{
system("cls");
cout<<"...............STUDENT MANAGEMENT SYSTEM..............\n";
cout<<"======================================================\n";
cout<<"0. Exit from Program\n";
cout<<"1. Write Data to File\n";
cout<<"2. Read Data From File\n";
cout<<"3. Search Data From File\n";
cout<<"4. Delete Data From File\n";
cout<<"5. Modify Data in File\n";
cout<<"Enter your choice : ";
cin>>ch;
system("cls");
switch(ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: modifyData(); break;
}
system("pause");
}while(ch);
}
int main()
{
project();
}
Console Display with problem
Your Student class can only hold 1 set of course information. Your getData() loop is overwriting the same variables over and over, that is why you only see the last course entered. You need to allocate an array (or better, use a std::vector) to hold multiple courses per Student.
There are other problems with your code as well.
Try something more like this instead:
//Project on Student Management
#include <iostream>
#include <fstream>
using namespace std;
struct Course
{
char courseName[100];
char grade[50];
char status[100];
};
class Student
{
int id;
int year;
float cgpa;
int NumberOfcourses;
Course *courses;
char academic_advisor[100];
public:
Student();
~Student();
void getData();
void showData() const;
int getID() const { return id; }
friend ostream& operator<<(ostream &out, const Student &s);
friend istream& operator>>(istream &in, Student &s);
};
ostream& operator<<(ostream &out, const Course &c)
{
out.write(c.courseName, sizeof(c.courseName));
out.write(c.grade, sizeof(c.grade));
out.write(c.status, sizeof(c.status));
return out;
}
istream& operator>>(istream &in, Course &c)
{
in.read(c.courseName, sizeof(c.courseName));
in.read(c.grade, sizeof(c.grade));
in.read(c.status, sizeof(c.status));
return in;
}
ostream& operator<<(ostream &out, const Student &s)
{
out.write((char*)&s.id, sizeof(s.id));
out.write((char*)&s.year, sizeof(s.year));
out.write((char*)&s.cgpa, sizeof(s.cgpa));
out.write(s.academic_advisor, sizeof(s.academic_advisor));
out.write((char*)&s.NumberOfcourses, sizeof(s.NumberOfcourses));
for(int i = 0; i < s.NumberOfcourses; ++i)
{
out << s.courses[i];
}
return out;
}
istream& operator>>(istream &in, Student &s)
{
delete[] s.courses;
s.courses = NULL;
s.NumberOfcourses = 0;
in.read((char*)&s.id, sizeof(s.id));
in.read((char*)&s.year, sizeof(s.year));
in.read((char*)&s.cgpa, sizeof(s.cgpa));
in.read(s.academic_advisor, sizeof(s.academic_advisor));
int NumberOfcourses;
if (in.read((char*)&NumberOfcourses, sizeof(NumberOfcourses)))
{
s.courses = new Course[NumberOfcourses];
for(int i = 0; i < NumberOfcourses; ++i)
{
if (in >> s.courses[i])
s.NumberOfcourses++;
else
break;
}
}
return in;
}
Student::Student()
{
id = 0;
year = 0;
cgpa = 0.0f;
NumberOfcourses = 0;
courses = NULL;
academic_advisor[0] = '\0';
}
Student::~Student()
{
delete[] courses;
}
void Student::getData()
{
cout << "\n\nEnter Student Details......\n";
cout << "Enter ID No. : "; cin >> id;
cout << "Enter Intake Year of the Student: "; cin >> year;
cout << "Enter number of Taken courses: ";
cin >> NumberOfcourses;
cin.ignore();
delete[] courses;
courses = new Course[NumberOfcourses];
for(int a = 0; a < NumberOfcourses; ++a)
{
cout << "\nEnter Subject Name: ";
cin.getline(courses[a].courseName, 100);
cout << "Enter Grade of Subject: ";
cin.getline(courses[a].grade, 50);
cout << "\nEnter Subject Status: ";
cin.getline(courses[a].status, 100);
}
cout << "Enter student CGPA: ";
cin >> cgpa;
cin.ignore();
cout << endl;
cout << "Enter Name of Academic Advisor of Student: ";
cin.getline(academic_advisor, 100);
}
void Student::showData() const
{
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << id << endl;
cout << "Intake Year : " << year << endl;
cout << "Subjects Taken in Previous Semester : " << endl;
for(int t = 0; t < NumberOfcourses; ++t)
{
cout << "\t" << courses[t].courseName << ": " << courses[t].grade << " (" << courses[t].status << ") ";
cout << endl;
}
cout << "CGPA : " << cgpa << endl;
cout << "Name of academic advisor of Student : " << academic_advisor << endl;
cout << endl;
}
void addData()
{
Student s;
s.getData();
ofstream fout("Students.txt", ios::binary|ios::app);
if (fout << s)
cout << "\n\nData Successfully Saved to File....\n";
else
cerr << "\n\nError Saving Data to File!\n";
}
void displayData()
{
ifstream fin("Students.txt", ios::binary);
Student s;
while (fin >> s)
{
s.showData();
}
if (!fin)
cerr << "\n\nError Reading Data from File!\n";
else
cout << "\n\nData Reading from File Successfully Done....\n";
}
void searchData()
{
int n;
cout << "Enter ID Number you want to search for : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Details of ID No. " << n << " are: \n";
s.showData();
flag = true;
break;
}
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else
{
if (!flag)
cout << "The ID No. " << n << " not found....\n\n";
cout << "\n\nData Reading from File Successfully Done....\n";
}
}
void deleteData()
{
int n;
cout << "Enter ID Number you want to delete : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
ofstream tout("TrashStud.txt", ios::binary|ios::app);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be deleted:\n";
s.showData();
tout << s;
flag = true;
}
else
{
fout << s;
}
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else if (!fout)
{
cerr << "\n\nError Saving Data to File!\n";
}
else
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("tempStud.txt");
}
else
{
remove("Students.txt");
rename("tempStud.txt", "Students.txt");
cout << "\n\nData Successfully Deleted from File....\n";
}
}
}
void modifyData()
{
int n;
cout << "Enter ID Number you want to Modify : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be modified with new data:\n";
s.showData();
cout << "\n\nNow Enter the New Details....\n";
s.getData();
flag = true;
}
fout << s;
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else if (!fout)
{
cerr << "\n\nError Saving Data to File!\n";
}
else
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("TempStud.txt");
}
else
{
remove("Students.txt");
rename("TempStud.txt", "Students.txt");
cout << "\n\nData Successfully Updated in File....\n";
}
}
}
void project()
{
int ch;
do
{
system("cls");
cout << "...............STUDENT MANAGEMENT SYSTEM..............\n";
cout << "======================================================\n";
cout << "0. Exit from Program\n";
cout << "1. Write Data to File\n";
cout << "2. Read Data From File\n";
cout << "3. Search Data From File\n";
cout << "4. Delete Data From File\n";
cout << "5. Modify Data in File\n";
cout << "Enter your choice : ";
cin >> ch;
system("cls");
switch (ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: modifyData(); break;
}
system("pause");
}
while (ch != 0);
}
int main()
{
project();
}
Alternatively:
//Project on Student Management
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Course
{
string courseName;
string grade;
string status;
};
class Student
{
int id;
int year;
float cgpa;
vector<Course> courses;
string academic_advisor;
public:
Student();
void getData();
void showData() const;
int getID() const { return id; }
friend ostream& operator<<(ostream &out, const Student &s);
friend istream& operator>>(istream &in, Student &s);
};
void writeStr(ostream &out, const string &str)
{
size_t size = str.size();
out.write((char*)&size, sizeof(size));
if (size)
out.write(str.c_str(), size);
}
void readStr(istream &in, string &str)
{
str.clear();
size_t size;
if (in.read((char*)&size, sizeof(size)))
{
if (size > 0)
{
str.resize(size);
in.read(&str[0], size);
}
}
}
ostream& operator<<(ostream &out, const Course &c)
{
writeStr(out, c.courseName);
writeStr(out, c.grade);
writeStr(out, c.status);
return out;
}
istream& operator>>(istream &in, Course &c)
{
readStr(in, c.courseName);
readStr(in, c.grade);
readStr(in, c.status);
return in;
}
ostream& operator<<(ostream &out, const Student &s)
{
out.write((char*)&s.id, sizeof(s.id));
out.write((char*)&s.year, sizeof(s.year));
out.write((char*)&s.cgpa, sizeof(s.cgpa));
writeStr(out, s.academic_advisor);
size_t NumberOfcourses = s.courses.size();
out.write((char*)&NumberOfcourses, sizeof(NumberOfcourses));
for(int i = 0; i < NumberOfcourses; ++i)
{
if (!(out << s.courses[i]))
break;
}
return out;
}
istream& operator>>(istream &in, Student &s)
{
s.courses.clear();
in.read((char*)&s.id, sizeof(s.id));
in.read((char*)&s.year, sizeof(s.year));
in.read((char*)&s.cgpa, sizeof(s.cgpa));
readStr(in, s.academic_advisor);
size_t NumberOfcourses;
if (in.read((char*)&NumberOfcourses, sizeof(NumberOfcourses)))
{
s.courses.reserve(NumberOfcourses);
Course c;
for(size_t i = 0; i < NumberOfcourses; ++i)
{
if (in >> c)
s.courses.push_back(c);
else
break;
}
}
return in;
}
Student::Student()
{
id = 0;
year = 0;
cgpa = 0.0f;
}
void Student::getData()
{
courses.clear();
cout << "\n\nEnter Student Details......\n";
cout << "Enter ID No. : "; cin >> id;
cout << "Enter Intake Year of the Student: "; cin >> year;
cout << "Enter number of Taken courses: ";
size_t NumberOfcourses;
cin >> NumberOfcourses;
cin.ignore();
courses.reserve(NumberOfcourses);
Course c;
for(int a = 0; a < NumberOfcourses; ++a)
{
cout << "\nEnter Subject Name: ";
getline(cin, c.courseName);
cout << "Enter Grade of Subject: ";
getline(cin, c.grade);
cout << "\nEnter Subject Status: ";
getline(cin, c.status);
s.courses.push_back(c);
}
cout << "Enter student CGPA: ";
cin >> cgpa;
cin.ignore();
cout << endl;
cout << "Enter Name of Academic Advisor of Student: ";
getline(cin, academic_advisor);
}
void Student::showData() const
{
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << id << endl;
cout << "Intake Year : " << year << endl;
cout << "Subjects Taken in Previous Semester : " << endl;
for(size_t t = 0; t < courses.size(); ++t)
{
cout << "\t" << courses[t].courseName << ": " << courses[t].grade << " (" << courses[t].status << ") ";
cout << endl;
}
cout << "CGPA : " << cgpa << endl;
cout << "Name of academic advisor of Student : " << academic_advisor << endl;
cout << endl;
}
void addData()
{
Student s;
s.getData();
ofstream fout("Students.txt", ios::binary|ios::app);
if (fout << s)
cout << "\n\nData Successfully Saved to File....\n";
else
cerr << "\n\nError Saving Data to File!\n";
}
void displayData()
{
ifstream fin("Students.txt", ios::binary);
Student s;
while (fin >> s)
{
s.showData();
}
if (!fin)
cerr << "\n\nError Reading Data from File!\n";
else
cout << "\n\nData Reading from File Successfully Done....\n";
}
void searchData()
{
int n;
cout << "Enter ID Number you want to search for : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Details of ID No. " << n << " are: \n";
s.showData();
flag = true;
break;
}
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else
{
if (!flag)
cout << "The ID No. " << n << " not found....\n\n";
cout << "\n\nData Reading from File Successfully Done....\n";
}
}
void deleteData()
{
int n;
cout << "Enter ID Number you want to delete : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
ofstream tout("TrashStud.txt", ios::binary|ios::app);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be deleted:\n";
s.showData();
tout << s;
flag = true;
}
else
{
fout << s;
}
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else if (!fout)
{
cerr << "\n\nError Saving Data to File!\n";
}
else
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("tempStud.txt");
}
else
{
remove("Students.txt");
rename("tempStud.txt", "Students.txt");
cout << "\n\nData Successfully Deleted from File....\n";
}
}
}
void modifyData()
{
int n;
cout << "Enter ID Number you want to Modify : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be modified with new data:\n";
s.showData();
cout << "\n\nNow Enter the New Details....\n";
s.getData();
flag = true;
}
fout << s;
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else if (!fout)
{
cerr << "\n\nError Saving Data to File!\n";
}
else
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("TempStud.txt");
}
else
{
remove("Students.txt");
rename("TempStud.txt", "Students.txt");
cout << "\n\nData Successfully Updated in File....\n";
}
}
}
void project()
{
int ch;
do
{
system("cls");
cout << "...............STUDENT MANAGEMENT SYSTEM..............\n";
cout << "======================================================\n";
cout << "0. Exit from Program\n";
cout << "1. Write Data to File\n";
cout << "2. Read Data From File\n";
cout << "3. Search Data From File\n";
cout << "4. Delete Data From File\n";
cout << "5. Modify Data in File\n";
cout << "Enter your choice : ";
cin >> ch;
system("cls");
switch (ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: modifyData(); break;
}
system("pause");
}
while (ch != 0);
}
int main()
{
project();
}

Why is my copy constructor being called when I try to add an element to my vector?

I am trying to add a customer to my vector and when I run my program the copy constructor is called. I am doing an assignment where I need a vector of customers and have to be able to add customers, display customers, find customers and load/store the data. Have I created the vector wrong? I am only new to c++ and really unsure about vectors.
Code from Main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include "Customer.h"
using namespace std;
void OutputFileStream();
void parseLine(const string& str);
void InputFileStream();
void save(vector<Customer> customers);
void load(vector<Customer>& customers);
void addCustomer(vector<Customer>& vect);
void displayAll(vector<Customer>& customers);
//void printActions();
vector<Customer> customers;
void OutputFileStream()
{
cout << "Creating and writing to file: Customer.txt" << endl;
ofstream outStream("customers.txt"); // write mode (overwrites existing data)
if (outStream.good())
{
int customerID = 150033;
outStream << "This is a line of text.\n";
outStream << "This is another line of text.\n";
outStream << "This is a line of text.\n";
int numOfPurchases = 4;
int purchases = 0;
outStream << customerID << "Mr" << "Jack" << "New" << numOfPurchases << purchases << endl;
//int *purchases = customers[0].getPurchases();
outStream.close(); // close file
cout << "File written.\n" << endl;
}
else
cout << "Unable to open file";
}
void parseLine(const string& str) {
stringstream strStream(str); //create string stream from the string
// int customerID;
string title;
string name;
string type;
//int numOfPurchases;
//int purchases;
string s;
int customerID = 150033;
getline(strStream, s, ';');
customerID = stoi(s);
getline(strStream, title, ';');
getline(strStream, name, ';');
getline(strStream, type, ';');
int numOfPurchases = 4;
getline(strStream, s, ';');
numOfPurchases = stoi(s);
int purchases = 0;
getline(strStream, s, ';');
purchases = stoi(s);
int* purchasesArray = new int[3];
purchasesArray[0] = (purchases & (255 << 16)) >> 16;
purchasesArray[1] = (purchases & (255 << 8)) >> 8;
purchasesArray[2] = purchases & 255;
for (int i = 0; i < 3; i++)
{
cout << purchasesArray[i];
}
cout << " CustomerID: " << customerID << "Title:" << title << " Name: " << name << " Type:" << type << " Number of Purchases: " << numOfPurchases << "Purchases: " << purchases << endl;
}
void InputFileStream() {
cout << "Reading from a semi-colon delimited txt file" << endl;
string line;
ifstream inStream("customers.txt"); //opens file as an input file stream
if (inStream.good()) //if the file is opened successfully and not empty
{
while (getline(inStream, line)) //reads line until false return
{
parseLine(line);
}
inStream.close();
}
else
cout << "unable to open file or the file is empty!";
}
void save(vector<Customer> customers)
{
ofstream out("customers.txt");
if(out)
{
for (Customer& c : customers)
{
out << c.save();
}
out.flush();
out.close();
}
else
{
cout << "Error Writing to File" << endl;
}
}
void load(vector<Customer>& customers)
{
ifstream in("customers.txt");
if (in) {
string line;
while (!in.eof())
{
getline(in, line);
if (line != "")
{
Customer c;
c.parse(line);
customers.push_back(c);
}
}
}
}
void addCustomer(vector<Customer>& customers) {
Customer customer;
cin >> customer;
customers.push_back(customer);
}
void displayAll(vector<Customer>& customers)
{
cout << "\nvector contains:\n";
for (Customer c : customers)
cout << c.getCustomerID() << " " << c.getTitle() << c.getName() << c.getNumOfPurchases() << c.getPurchases() << c.getType() << endl;
cout << endl;; //same as calling customer
}
//
//int getCustomerByPurchaseNumber(int numOfPurchases) {
// vector<Customer> customers;
// int pos = -1;
// for (int i = 0; i < customers.size(); i++) {
// if (customers.at(i).getNumOfPurchases() == numOfPurchases) {
// return i;
// }
// }
// return pos;
//}
//
//void findCustomerByPurchaseNumber(vector<Customer>& customers) {
// vector<Customer> customers;
// int numOfPurchases;
// cout << "Please Enter Your Purchase Number:" << endl;
// cin >> numOfPurchases;
// int pos = customers.get(pos);
// getCustomerByPurchaseNumber(numOfPurchases);
// if (pos == -1) {
// cout << "Number of Purchase Not Found! " << endl;
// return customers;
// }
// else {
// cout << "Number Of Purchase Found! " << endl;
//
// return customers* = customers;
// }
//
//}
int main()
{
vector<Customer> customers;
Customer c1 = { 150031, "Mr", "John", 5, 333,362,341, "New" };
customers.push_back(c1);
//InputFileStream();
/* Customer customer;
customer.setCustomerID(150032);
customer.setTitle("Mr");
customer.setName("Joey");
customer.setNumOfPurchases(3);
customer.setPurchases(366, 352, 334);
customer.setType("New");
cout << customer.getCustomerID() << endl;
cout << customer.getTitle() << endl;
cout << customer.getName() << endl;
cout << customer.getNumOfPurchases() << endl;
cout << customer.getPurchases() << endl;
cout << customer.getType() << endl;
*/
return 0;
}
Code from Customer.cpp:
#include "Customer.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include<utility>
using namespace std;
//default constructor
Customer::Customer() {
}
//Full constructor
Customer::Customer(int customerID, string title, string name, int numOfPurchases, int purchase1, int purchase2, int purchase3, string type)
{
this->customerID = customerID;
this->title = title;
this->name = name;
this->numOfPurchases = numOfPurchases;
purchases = new int[3];
purchases[0] = purchase1;
purchases[1] = purchase2;
purchases[2] = purchase3;
this->type = type;
}
Customer::Customer(const Customer& source) //copy constructor
{
cout << "copy constructor called" << endl;
this->customerID = source.customerID;
this->title = source.title;
this->name = source.name;
this->numOfPurchases = source.numOfPurchases;
this->purchases = new int[3];
purchases[0] = source.purchases[0];
purchases[1] = source.purchases[1];
purchases[2] = source.purchases[2];
this->type = source.type;
}
//overloaded assignment operator=
Customer& Customer::operator= (Customer& otherCustomer)
{
cout << "Overloaded assignment operator= called" << endl;
//self-assignment guard
if (this == &otherCustomer)
return *this; //refernce to the same object
// copy data from the source (rhs) to this object (the destination)
name = otherCustomer.name;
//must make a new scores object to store a copy of the other student
if (purchases != nullptr)
delete[] purchases;
purchases = new int[3];
for (int i = 0; i < 3; i++) {
purchases[i] = otherCustomer.purchases[i];
}
//return this existing object so we can chain this operator
return *this;
}
string Customer::save()
{
stringstream out;
out << this->customerID << ";";
out << this->title << ";";
out << this->name << ";";
out << this->type << ";\n";
out << this->numOfPurchases << ";";
int* purchases = 0;
out.flush();
return out.str();
}
void Customer::parse(string line)
{
stringstream in(line);
string customerIDLine;
getline(in, customerIDLine, ';');
customerID = stoi(customerIDLine);
getline(in, title, ';');
getline(in, name, ';');
getline(in, type, ';');
string numOfPurchases;
getline(in, numOfPurchases, ';');
int s = stoi(numOfPurchases);
int* purchasesArray = new int[3];
purchasesArray[0] = (s & (255 << 16)) >> 16;
purchasesArray[1] = (s & (255 << 8)) >> 8;
purchasesArray[2] = s & 255;
}
void Customer::addCustomer(vector<Customer>& customers )
{
//after read data
int customerID;
cout << "Please Enter Customer ID: " << endl;
cin >> customerID;
string title;
cout << "Please Enter Title: " << endl;
getline(cin, title);
string name;
cout << "Please Enter Name: " << endl;
getline(cin, name);
string type;
cout << "Please Enter Type: " << endl;
getline(cin, type);
int numOfPurchases;
cout << "Please Enter Number of Purchases: " << endl;
cin >> numOfPurchases;
int purchase1;
cout << "Please Enter First Purchase: " << endl;
cin >> purchase1;
int purchase2;
cout << "Please Enter Second Purchase: " << endl;
cin >> purchase2;
int purchase3;
cout << "Please Enter Third Purchase: " << endl;
cin >> purchase3;
Customer c;
customers.push_back(c);
//Customer c();
}
Customer::~Customer() {
cout << "Destructor ~Customer called" << endl;
delete[] purchases;
}
// Overloaded insertion operator (Outputs Character object data as an output stream)
// Defined in header file as a "friend" function, as it is not a member function
//
ostream& operator<<(ostream& out, Customer& customer)
{
cout << "Customer details ( output by insertion operator<< )" << endl;
cout << "Customer ID: " << customer.customerID << endl;
cout << "Title: " << customer.title << endl;
cout << "Name: " << customer.name << endl;
cout << "Number of purchases: " << customer.numOfPurchases << endl;
cout << "Purchases: ";
for (int i = 0; i < 3; i++)
{
if (i > 0) cout << ",";
cout << customer.purchases[i];
}
cout << "Type: " << customer.type << endl;
return out;
}
istream& operator>> (istream& in, Customer& customer)
{
cout << "Enter Customer details ( using the extraction operator>> )" << endl;
cout << "Enter Customer ID: " << endl;
cin >> customer.customerID;
cout << "Enter Title: " << endl;
getline(cin, customer.title);
cout << "Enter Name: " << endl;
getline(cin, customer.name);
cout << "Enter Number of Purchases: ";
cin >> customer.numOfPurchases;
cout << "Enter Purchases: ";
cin >> customer.purchases[0];
cin >> customer.purchases[1];
cin >> customer.purchases[2];
cout << "Enter Type";
getline(cin, customer.type);
cout << endl;
return in;
}
int Customer::getCustomerID()
{
return customerID;
}
string Customer::getTitle()
{
return title;
}
string Customer::getName()
{
return name;
}
int Customer::getNumOfPurchases()
{
return numOfPurchases;
}
int* Customer::getPurchases()
{
return purchases;
}
string Customer::getType()
{
return type;
}
void Customer::setCustomerID(int customerID)
{
if (customerID < 1) {
cout << "Customer ID has to be equal to 1 or more" << endl; //Changed all the "throw invalid_argument" messages to cout as they were causing an issue with my main.cpp file and an abort message kept appearing every time I ran my main.cpp file.
}
this->customerID = customerID;
}
void Customer::setTitle(string title)
{
if (title.length() < 2) {
cout << "Title has to be more than or equal to 2 characters" << endl;
}
this->title = title;
}
void Customer::setName(string name)
{
if (name.length() < 4) {
cout << "Length of name should be more than or equal to 4 characters" << endl;
}
this->name = name;
}
//Got help ith this on stack overflow as I was using "&&" instead of using "||" for the if statement
void Customer::setNumOfPurchases(int numOfPurchases)
{
if(numOfPurchases <0 || numOfPurchases > 10000){
cout << "Number of purchases should be between 0 to 10000" << endl;
}
this->numOfPurchases = numOfPurchases;
}
void Customer::setPurchases(int purchase1, int purchase2, int purchase3)
{
if (purchase1 < 0 || purchase2 < 0 || purchase3 < 0) {
cout << "Purchases must be more than or equal to zero" << endl;
}
}
//Got help from stack overflow on comparing strings as I originally didnt use "type.compare"
void Customer::setType(string type) {
if (type.compare("New") !=0 || type.compare("Either") !=0) {
cout << "Type of purchase has to be New or Either" << endl;
}
}
Code from Customer.h:
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include <vector>
class Customer
{
private:
int customerID;
string title;
string name;
int numOfPurchases;
int* purchases;
string type;
public:
Customer(); // default constructor
Customer(int customerID, string title, string name, int numOfPurchases, int purchase1, int purchase2, int purchase3, string type);
//copy overload assignment
Customer& operator=(Customer& otherCustomer);
Customer(const Customer& source);
string save();
void parse(string line);
void addCustomer(vector<Customer>& customers);
~Customer(); //destructor
//Getters and Setters
void setCustomerID(int customerID);
void setTitle(string title);
void setName(string name);
void setNumOfPurchases(int numOfPurchases);
void setPurchases(int purchase1, int purchase2, int purchase3);
void setType(string type);
int getCustomerID();
string getTitle();
string getName();
int getNumOfPurchases();
int* getPurchases();
string getType();
void printCustomer() {
cout << customerID << "," << title << "," << name << "," << numOfPurchases << "," << purchases << "," << type << endl;
}
friend std::ostream& operator<<(std::ostream& out, Customer& customer); // overloaded operator<<
friend istream& operator>> (istream& in, Customer& customer); // overloaded operator >>
};
Vectors hold objects. If you have an object outside of the vector there is no way to have that same object inside the vector. You can move from one instance to one in the vector (that will move the instances inner parts, but there are still two instances). push_back does make a copy of its parameter when it cannot be moved from.
As you have a constructor that takes all necessary parameters, you can use emplace_back to avoid the copy and construct the instance right in place in the vector:
customers.emplace_back( customerID, title, name, numOfPurchases, purchase1, purchase2, purchase3, type);
If you check reference on std::vector<T,Allocator>::push_back object can be added to vector either by being copied (option1) or being moved (option 2).
So you can create a move constructor for your customer class:
class_name ( class_name && );
And then call push_back using that move consturctor (option 2) on vector:
void push_back( T&& value );
The std::vector<Customer> holds the element by value so just using std::vector::push_back will copy your local object into the vector.
To mitigate this you could either implement move semantics and move the the Customer object like this:
#include <utility>
....
Customer c;
c.parse(line);
customers.push_back(std::move(c));
or construct the object in-place:
// Post c++17 you could use `push_back()` here as well as
// c++17 has mandatory copy elision.
customers.emplace_back(Customer{}); // or customers.emplace_back(); only
customers.back().parse(line);
Both solutions require c++11.
You could also store a reference/pointer and allocate the object on the heap.
std::vector<std::unique_ptr<Customer>> customerVector;
customer_vector.push_back(std::make_unique<Customer>());
// or customer_vector.push_back(std::make_shared<Customer>());
Or
std::vector<Customer*> customerVector;
// Beware for memory leaks, needs to be deleted.
// Basically never use code like this post c++11).
customer_vector.push_back(new Customer{});