the saveEverything function gets the User vector but not the Exercise vector (which is part of the User) and it doesn't transfer so I can't save the information to the file. What is it caused by?
saveEverything function:
void saveEverything(std::vector<User> users){
std::fstream users_database;
users_database.open("users_database.txt",std::ios::out |
std::ios::trunc);
if(!users_database)
throw 1;
for(int i =0;i<users.size();i++){
users_database<<users[i].getUserFirstName()<<"|"
<<users[i].getUserLastname()<<"|"<<users[i].getUserAge()<<"|"
<<users[i].getUserWeight()<<std::endl;
}
users_database<<"||ENDOFUSERS||"<<std::endl;
std::vector<Exercise> exercises;
for(int i=0;i<users.size();i++){
users_database<<"||U||"<<std::endl;
users_database<<users[i].getUserFirstName()<<"|"
<<users[i].getUserLastname()<<std::endl;
exercises = users[i].getExercises();
for(Exercise exercise: exercises){
users_database<<exercise.getExerciseName()<<"|"
<<exercise.getExerciseWeight()<<"|"<<exercise.getExerciseSeries()
<<"|"<<exercise.getExerciseReps()<<std::endl;
}
users_database<<"||END||"<<std::endl;
}
std::cout<<"Saving completed!"<<std::endl;
}
class User:
class User {
friend std::ostream &operator<<(std::ostream &os, const User
&user);
private:
std::string UserFirstName;
std::string UserLastname;
unsigned int UserAge;
double UserWeight;
std::vector<Exercise>UserExercises;
getExercise() method:
std::vector<Exercise> User::getExercises(){
return UserExercises;
}
i tried to add copy contructors but they didn't work. Maybe you have an idea what is the reason or how else to save this information to the file?
Your User
copy constructor is broken
User(const User ©_user)
: UserFirstName{copy_user.UserFirstName},
UserLastname{copy_user.UserLastname}, UserAge{copy_user.UserAge},
UserWeight{copy_user.UserWeight}{}
It fails to copy the UserExercises
vector. This means that everytime you copy a User
object you get an empty UserExercises
vector.
One large benefit of using vectors and strings etc. is that you don't have to write copy constructors, assignment operators and destructors because the default ones do the right thing.
Just delete this bugged copy constructor, the default one is all you need.
I would also remove
~User()= default;
which is just wasted space and keystrokes.