1234567891011121314151617181920212223242526272829303132333435363738394041424344
#include <vector> #include <iostream> #include <iomanip> #include <string> #include <ctime> #include <cstdlib> struct Pet { std::string name; std::string type; int weight; Pet(std::string& n, std::string& t, int w) : name(n), type(t), weight(w) {} }; int main() { std::vector<Pet> pets; int PetNum; std::srand((unsigned)time(0)); std::cout << "How many pets do you have? "; std::cin >> PetNum; for (int p = 0; p < PetNum; ++p) { std::string PetName, PetType; std::cout << "What is the name of your pet? "; std::cin >> PetName; std::cout << "What type of pet is " << PetName << "? "; std::cin >> PetType; pets.emplace_back(PetName, PetType, (rand() % 100) + 1); } std::cout << std::endl; for (const auto& [name, type, weight] : pets) { std::cout << "Pet Name: " << std::setw(15) << name << std::endl; std::cout << "Pet Type: " << std::setw(15) << type << std::endl; std::cout << "Pet Weight: " << std::setw(13) << weight << std::endl << std::endl; } }
12345678
struct Pet { public: std::string name; std::string type; int weight; Pet(std::string& n, std::string& t, int w) : name(n), type(t), weight(w) {} };