Q.Basic structure program
Unable to find an error.
This error comes when running this program.
*** stack smashing detected ***: terminated
Can someone tell what's the problem in this program
Edit: Actually, aside from potential char array buffer overflows, your book b array is only size 2, but you are looping over it 3 times.
Change the '3' on lines 12 and 20 to '2', or better yet, make a constant int in like constint NumBooks = 2;
________________________
The more insidious problem is that you allow the user to potentially cause a buffer overflow because you are using cin >> into char arrays.
You should use std::strings instead, ideally.
1 2 3 4 5
struct book {
int roll;
string name;
string add;
};
Or, if you must use char arrays because of some asinine professorial requirement, then use:
1 2 3 4
cin >> b[i].roll;
cin.ignore(); // ignore newline left in buffer from cin >> call.
cin.getline(b[i].name, 20);
cin.getline(b[i].add, 20);
Really very thank you for helping.
I'm a beginner & was doing a question from a book.
I got that the array size was 2 so was having the problem, I was confused by the accessing element with the size so was unable to find error.
Actually I am learning c before c++ and using c++syntax and compiler as c++ supports all c features. and I don't know about std::strings, const, but got this one cin.getline(b[i].name, 20);