Hello. My task is to describe the structure of a note which contains the following fields: First Name, Last Name, Telephone Number, Date of Birth (an array of 3 numbers). The latter is a prerequisite. I described the structure, made input and output. Everything works, but all records except the last one in the year of birth have a strange meaning different from the one entered. I attach an output screen and a code to the question. conclusion

// Struct_simpled.cpp: определяет точку входа для консольного приложения. // #include "stdafx.h" #include "stdio.h" #include "iostream" #include "conio.h" using namespace std; struct note { char name[30]; char sec_name[50]; char phone[15]; int date[3]; }; note point[3]; int _tmain(int argc, _TCHAR* argv[]) { for (int i = 1; i <= 3; i++) //ввод структуры { cout << "input the Name (30 char's)" << endl; cin >> point[i].name; cout << "input the second name (50 char's)" << endl; cin >> point[i].sec_name; cout << "input phone number (15 characters)" << endl; cin >> point[i].phone; cout << "input the date of birthday" << endl; for (int j = 1; j <= 3; j++) { cin >> point[i].date[j]; } } for (int i = 1; i <= 3; i++) // вывод структуры { cout << point[i].name << " " << point[i].sec_name << " " << point[i].phone << " "; for (int j = 1; j <= 3; j++) { cout << point[i].date[j]<<"."; } cout << endl; } _getch(); return 0; } 

Question: why and because of what the last element of the date array is littered?

    1 answer 1

    Arrays in C ++ are indexed with 0.

    int date[3] are the date[0] , date[1] and date[2] elements.

    You work from 1:

     for (int j = 1; j <= 3; j++) { cout << point[i].date[j]<<"."; } 

    should actually look like

     for (int j = 0; j < 3; j++) { cout << point[i].date[j]<<"."; } 

    And not only that - you and the elements of the node point[3]; array node point[3]; handling wrong.

    It is even strange that with such an explicit exit beyond the bounds of the array, you did not end in an emergency ... You were not lucky.

    • Well, how to access the structure correctly? I just do not even know such a nuance. We were stupidly given a pack of assignments and told to write - everything, but this is already a lyrical digression UPD It seems to understand what the humor is regarding node point - MarKo
    • Yes, just replace all the cycles, as I showed in the answer - to the cycle from 0 to 2. - Harry
    • By the way, I also forgot about null characters in the Chars! So you need to throw them on 1 character. - MarKo