Created a structure. I introduce 2 f.vozrast elements. I want to print, but prints only the second entered element f.vosrast .

 struct sname { char fio[256]; int vozrast; }; sname f; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); for (int i = 0; i < 2; i++) { cin >> f.vozrast; cout << "Возраст:" << f.vozrast; } return 0; } 

Apparently when you enter the second age, the result is overwritten in place of the first. How to Split f.vozrast on the "array" and could be f.vozrast in an elemental way?

  • ideone.com/4ozWKd - displays both. Or do you think that you enter the last name and age in the same variable? :) - Harry

1 answer 1

You do not want this?

 struct sname { char fio[256]; int vozrast; }; sname f[2]; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); for (int i = 0; i < 2; i++) { cin >> f[i].vozrast; cout << "Возраст:" << f[i].vozrast << endl; } return 0; } 
  • Yes, exactly that. Why is sname f [2] specifically 2? Because I have 2 elements in the structure? - Maxim Plysenko
  • How much you need - do so much. You wanted 2 ( for (int i = 0; i < 2; i++) ) - so I wrote two. Want - do more ... - Harry