exception with program
May 31, 2016 at 5:40pm UTC
Hi guys does anybody know why I'm getting an std::out_of_range exception in my program? here is the code below.
Thanks
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> names;
vector<string> passwords;
int awnser;
string name;
string password;
int age;
cout << "have an account? or want to register?" << endl;
cout << "1 for yes,enter 2 to register" << endl;
cin >> awnser;
if (awnser == 1){
cout << "enter name" << endl;
cin >> name;
cout << "enter password" << endl;
cin >> password;
}else if (awnser ==2){
cout << "enter a name" << endl;
cin >> name;
names.push_back(name);
cout << "enter a password" << endl;
cin >> password;
passwords.push_back(password);
}
for (int i = 0;i < names.size();i++){
cout << names.at(1) << endl;
}
}
May 31, 2016 at 5:47pm UTC
Line 37: You've only pushed one name (name[0]), therefore names[1] is out of bounds. If you want to print the array, you should be using names[i], not names[1].
May 31, 2016 at 5:52pm UTC
true that,thanks I'll change that
Topic archived. No new replies allowed.