fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. int main(){
  6. // declare a vector of string
  7. std::vector<std::string> animalsList;
  8.  
  9. // Input 5 animal names and push them to the vector.
  10. std::cout << "Enter name of 5 animals of your choice. e.g., cat, dog, etc.. "<< std::endl;
  11. for (int i = 0; i < 5; i++){
  12. std::cout << ">> ";
  13. std::string animalName; // string to store an animal name.
  14. std::getline(std::cin, animalName); // Input an animal name.
  15. animalsList.push_back(animalName); // push the animal name to the vector
  16. }
  17.  
  18. // output all the animal names.
  19. std::cout << std::endl;
  20. for (int i = 0; i < animalsList.size(); i++) // here animalsList.size() will be 5
  21. {
  22. std::cout << i + 1 << ": " << animalsList[i] << std::endl;
  23. }
  24. return 0;
  25. }
Success #stdin #stdout 0s 3476KB
stdin
cat
dog
monkey
donkey
bear
stdout
Enter name of 5 animals of your choice. e.g., cat, dog, etc..  
>> >> >> >> >> 
1: cat
2: dog
3: monkey
4: donkey
5: bear