Feb 10, 2018 at 3:31am UTC
I create a program
which accept the name of the animal and age
And if I type stop the program should stop ans show me the input that I typed so far.
but when I type stop it create an out of range error.
How can I fix this issue?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string animalName;
int animalAge;
int numOfAni = 0;
vector<string> nameOfAnimals;
vector<int > age;
for (int i = 0; i < 5; i++)
{
cout << "Enter animal #" << i + 1 << " 's name:" ;
cin >> animalName;
nameOfAnimals.push_back(animalName);
// Options for exit the program.
if (animalName == "STOP" )
break ;
else if (animalName == "Stop" )
break ;
else if (animalName == "stop" )
break ;
//Ask user to input age of animals
cout << "Enter anial #" << i + 1 << " 's age:" ;
cin >> animalAge;
age.push_back(animalAge);
numOfAni++;
}
//Deplay the author of the program.
cout << endl;
cout << "\nLab 1 created by Minje Ban Computer Science" << endl << endl;
cout << "Animal" << "\n#\tName\tAge" << endl;
for (int i = 0; i < nameOfAnimals.size(); i++)
{
cout << i + 1 << "\t" << nameOfAnimals[i] << "\t" << age[i] << endl;
}
cout << endl;
system("pause" );
return 0;
}
I
Last edited on Feb 10, 2018 at 3:32am UTC