Program crash on data manipulation

So, during the running of this program when it gets to the part where someone is supposed to enter their hobbies, hit enter to write that it crashes.
1
2
cout<<"\nAlso "<<*p<<" What are some of your hobbies?\n";
cin>>per.hob[1024];

The string per.hob[1024 is inside of a structure.
1
2
3
4
5
6
struct database {
int age;
int salary;
string name[100];
string hob[1024];
};

The help will be much appreciated 8) Thank-You
Full source
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
55
56
57
58
59
60
61
62
#include <iostream>

using namespace std;
struct database {
int age;
int salary;
string name[100];
string hob[1024];
};



int main()
{
    database per;
    string *p;
    string *h;
    p = &per.name[100];
    h = &per.hob[1024];
    int x;

do{
cout<<"Hello and welcome to this small little program that I put together. First off please enter your name:";
cin>>per.name[100];
cout<<"So "<<*p<<" would you like to tell me a little bit more about yourself? \n";
cout<<"1.Yes \n2.No \n Please enter your selection: ";
cin>>x;
cout<<"Good to hear I like learning about new people\n";

do{
cout<<"So "<<*p<<" what is your age?";
cin>>per.age;
if(per.age > 106){
   cout<<" WOW! That's super crazy that you're "<<per.age<<" I'm surpirised you know how to work a computer!\n";
   cout<<"Why don't you enter your reall age and we can get on with this.\n";

   }
   else if(per.age < 5){
    cout<<"It is very sceptical to me that you are "<<per.age<<".";
    cout<<"Why don't you enter your reall age and we can get on with this.\n";
   }


}while(per.age < 5 , per.age > 106);


cout<<"How about your anual salary: ";
cin>>per.salary;
cout<<"\nAlso "<<*p<<" What are some of your hobbies?\n";
cin>>per.hob[1024];
cout<<"Well today so far I have learned.\n";
cout<<"Your name: "<<*p<<endl;
cout<<"Your age: "<<per.age<<endl;
cout<<"Your anual salary: "<<per.salary<<endl;
cout<<"And lastly what you like to do, which is: "<<*h<<endl;
cout<<"Well it's been awesome getting to know you "<<*p<<" 8) Would you like to tell me everything again?!\n";
cout<<"1.Yes! 2.No. :";
cin>>x;

}while(x != 2);

}
string hob[1024] allocates space for 1024 strings in the array hob. Valid indices for hob are 0-1023. Thus, in cin>>per.hob[1024]; the index used is out of range.
Last edited on
Thank-You for the reply but even after setting it too 100 the program still crashes for some reason.
You make the same mistake with several other variables in your code.

1
2
   p = &per.name[100];
    h = &per.hob[1024];

p and h point to memory you don't own.

cin>>per.name[100];
clobbers memory you don't own.


even after setting it too 100 the program still crashes for some reason.


Using an arbitrary index isn't much of a solution. You might want to brush up on arrays and come back to this.
Last edited on
Hey thanks again for your help 8) I ended up ditching the strings all together and using arrays instead. Which would have been a better idea to start off with I guess hehe.
Topic archived. No new replies allowed.