dimanche 15 mars 2015

terminate called after throwing an instance of 'std::out_of_range'


Vote count:

-1




I'm a absolute beginner for programming. I got this error while building up the following code. Please help me. ***error: **terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 8) >= this->size() (which is 8) Aborted (core dumped)*****



#include<iostream>
#include<vector>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
cout<<"\n Welcome to space travel calculation program";

string cPlanet, name;
double weight, speed, tTime, nWeight;
int num;
vector<string> planet;
vector<int> distance;
vector<double> sGravity;

planet.push_back("Mercury");
distance.push_back(36);
sGravity.push_back(0.27);

planet.push_back("Venus");
distance.push_back(67);
sGravity.push_back(0.86);

planet.push_back("Earth");
distance.push_back(93);
sGravity.push_back(1.00);

planet.push_back("Mars");
distance.push_back(141);
sGravity.push_back(0.37);

planet.push_back("Jupiter");
distance.push_back(483);
sGravity.push_back(2.64);

planet.push_back("Saturn");
distance.push_back(886);
sGravity.push_back(1.17);

planet.push_back("Uranus");
distance.push_back(1782);
sGravity.push_back(0.92);

planet.push_back("Neptune");
distance.push_back(2793);
sGravity.push_back(1.44);
num=planet.size();

cout<<"\n Please tell me your name: ";
getline(cin,name);


cout<<"\n Please choose which planet you want to travel to from the following list:"
<<"\n 1.Mercury"
<<"\n 2.Venus"
<<"\n 3.Earth"
<<"\n 4.Mars"
<<"\n 5.Jupiter"
<<"\n 6.Saturn"
<<"\n 7.Uranus"
<<"\n 8.Neptune :";
getline(cin,cPlanet);

cout<<"\n What is your weight on Earth?";
cin>>weight;

cout<<"\n At what speed do you wish to travel? :";
cin>>speed;

if(cPlanet==planet.at(num))
{
tTime=(distance.at(num))/speed;
nWeight=weight*sGravity.at(num);

cout<<"\n Your Name: "<<name
<<"\n Weight On Earth: "<<weight
<<"\n Planet you wish to visit: "<<cPlanet
<<"\n The speed you will be travelling at: "<<speed
<<"\n Total time it will take to reach "<<planet.at(num)<<": "<<tTime
<<"\n Your weight on "<<planet.at(num)<<": "<<nWeight;
}

return 0;
}


asked 31 secs ago







terminate called after throwing an instance of 'std::out_of_range'

1 commentaire:

  1. Need to add :

    num=0;
    while(cPlanet!=planet.at(num))
    {
    num++;
    }

    before if condition..... :)

    RépondreSupprimer