So I spent some time trying to get to grips with creating Classes and using Constructors and this project really helped in my understanding, although I did have to google about how to return an int in a string variable.
Anyway, here is the code I came up with. Will be looking to add the ability to get user input to create a profile from scratch, but currently happy with what I have!
Hope this helps anyone who gets stuck or helps inspire some new ideas on how to solve this project.
#include <iostream>
#include "profile.hpp"
int main() {
Profile sam(30, "Sam Drakkila", "New York", "USA", "He/Him");
Profile kat(28, "Kathrine Deitch", "Chicago", "USA", "She/Her");
sam.add_hobby("Listening to Music");
sam.add_hobby("Watching Movies");
sam.add_hobby("Clubbing");
sam.add_hobby("Skateboarding");
sam.add_hobby("Windsurfing");
sam.add_hobby("Go Karting");
sam.add_hobby("Fishing");
sam.add_hobby("Kayaking");
sam.add_hobby("Reading");
kat.add_hobby("Reading");
kat.add_hobby("Playing Guitar");
kat.add_hobby("Swimming");
std::cout << sam.view_profile() << std::endl;
This file has been truncated. show original
#include <iostream>
#include <string>
#include "profile.hpp"
Profile::Profile(int new_age, std::string new_name, std::string new_city, std::string new_country, std::string new_pronoun)
: age(new_age), name(new_name), city(new_city), country(new_country), pronoun(new_pronoun) {
}
std::string Profile::view_profile() {
std::string bio = "Name: " + name + "\nAge: " + std::to_string(age) + "\nCity: " + city + "\nCountry: " + country + "\nPronoun: " + pronoun + "\nHobbies: ";
for (int i = 0; i < hobbies.size(); i++) {
if ((i + 1) % 3 == 0 && i < hobbies.size() - 1) {
bio += hobbies[i] += ",\n";
}
else if (i < hobbies.size() - 1) {
bio += hobbies[i] += ", ";
}
else if (i < hobbies.size()) {
bio += hobbies[i] += ".\n";
}
}
This file has been truncated. show original
#include <vector>
class Profile {
int age;
std::string name, city, country, pronoun;
std::vector <std::string> hobbies;
public:
Profile(int new_age, std::string new_name, std::string new_city, std::string new_country, std::string new_pronoun = "They/Them");
std::string view_profile();
void add_hobby(std::string new_hobby);
};