reading variables in struct type in for loop
c++, struct
Solution
`cin >> students[i].name[20];`
It shouldn't be an array of `20`. It means your entered first character is stored in index `20th` location which is in-correct and can cause undefined behavior as `name` array is indexed from `0` to `19` only.
Rather it should be `cin >> students[i].name`, which writes into base address of array `name`
Problem
I'w writing piece of code and I should read variables from for loop: ``` #include <iostream> using namespace std; #define NumOfStudents 100 #define NumOfCourses 15 struct Student{ int stdnum, FieldCode, age; double average, res[NumOfCourses]; char name[20]; //First and Last name length }; int main(){ struct Student students[NumOfStudents]; int i; cout << "\tNAME || STUDENT-NUMBER || FIELD-CODE || AGE"; for(i=0; i<NumOfStudents; i++){ cout << "\nSTUDENT #" << i+1 << ": "; cin >> students[i].name[20] >> students[i].stdnum >> students[i].FieldCode >> students[i].age; // cin >> students[i].name[20]; } } ``` EDIT: output is: ``` ./st NAME || STUDENT-NUMBER || FIELD-CODE || AGE STUDENT #1: test STUDENT #2: STUDENT #3: STUDENT #4: . . . STUDENT #100: ``` I can just enter name of first student and loop doesn't work correctly what is the problem?