access violation error help
May 1, 2014 at 11:15am UTC
i am not able to get over this error:
0xC0000005: Access violation reading location 0x00155000.
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <iostream>
using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa [], int n);
void display1(student st);
void display2(const student * ps);
void display3(const student pa [],int n);
int main()
{
cout << " size: " ;
int size;
cin >> size;
while (cin.get() != '\n' )
continue ;
student * ptr_stu = new student[size];
int entered = getinfo(ptr_stu, size);
for (int i = 0; i < entered; i++) // started displaying garbage from here
{
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
};
display3(ptr_stu, entered);
delete [] ptr_stu;
cout << "done" ;
return 0;
}
int getinfo(student pa [], int n)
{
cout << " student 1; " << endl;
int k = 0;
for (int i =0; i < n ; i++,k++)
{
cout << " name " ;
if (cin.get() == '\n' )
break ;
cin.getline(pa[i].fullname,SLEN);
cout << " hobby : " ;
cin.getline(pa[i].hobby,SLEN);
cout << " ooplevel : " ;
cin >> pa[i].ooplevel;
cin.get();
if (i < n-1)
cout << "for student # " << i + 2 << endl;
};
return k;
}
void display1(student st)
{
cout << "name : " << st.fullname << endl;
cout << " hobby : " << st.hobby << endl;
cout << "ooplevel : " << st.ooplevel << endl;
}
void display2(const student * ps)
{
cout << "name : " << ps->fullname << endl;
cout << " hobby :" << ps->hobby << endl;
cout << " ooplevel : " << ps->ooplevel << endl;
}
void display3(const student pa [],int n)
{
for (; pa < (pa + n); pa++)
{
cout << " name : " << pa->fullname << endl;
cout << "hobby : " << pa->hobby << endl;
cout << " ooplevel : " << pa->ooplevel << endl;
};
}
Last edited on May 1, 2014 at 12:00pm UTC
May 1, 2014 at 11:56am UTC
I am not sure about the cause of your error. It may help if you debugged and tell us the exact point where you get the error. Some observations and suggestions from my end.
Firstly I suggest you should use
std::vector
instead of a dynamically allocated array. It will simplify your code very much.
Secondly, with the following code, I guess you are removing the EOL character from the input buffer after the int input.
1 2
while (cin.get() != '\n' )
continue ;
Instead you can simply use
cin.ignore()
.
Finally, use a
std::string
instead of a char array.
Last edited on May 1, 2014 at 11:56am UTC
May 1, 2014 at 12:10pm UTC
It's because your return value in getinfo
is one greater than your highest array index.
Ignore, misread a line of code further down. :-S
Last edited on May 1, 2014 at 12:25pm UTC
May 1, 2014 at 12:15pm UTC
for (; pa < (pa + n); pa++)
this seems to me like infinite loop
imagine that with numbers:
1 < (1 + n); 1++
will result in
2 < (2 + n); 2++
...
5 < (5 + n); 5++
1 2 3 4 5 6 7 8 9 10 11
void display3(const student pa[], int n)
{
const student* ppa = pa; // you need to store the original address
for (; pa < (ppa + n); pa++)
{
cout << " name : " << pa->fullname << endl;
cout << "hobby : " << pa->hobby << endl;
cout << " ooplevel : " << pa->ooplevel << endl;
};
}
these two lines of code eat the first letter of the name you type in
1 2
if (cin.get() == '\n' )
break ;
Last edited on May 1, 2014 at 12:22pm UTC
May 1, 2014 at 12:20pm UTC
finally!
thanx @ rich1
Topic archived. No new replies allowed.