matrix - Access Struct in C++? -


i have problem in c++ teacher asking display field of structures contains n=100 students. right way ?

#include <iostream> #include <math.h>  using namespace std;  struct students{     string name;     int id;     int mark1;     int mark2;     int mark3; };  int main(){     int t[3];     int i;      for(i=0;i<=3;i++){         t[i] = i;     }     for(i=0;i<=3;i++){         students t[i];         cout<< t[i].name<<endl;         cout<< t[i].id<<endl;         cout<< t[i].mark1<<endl;         cout<< t[i].mark2<<endl         cout<< t[i].mark3<<endl;     }     return 0; } 

the program not make sense. should either declare array of type students or use other standard container example std::vector<students>. , after container defined have enter values each element.

for example

const size_t n = 100; students students[n];  ( size_t = 0; < n; i++ ) {     std::cout << "enter name of student " << + 1 << ": ";     std::cin >> students[i].name;     // ... } 

or

#include <vector>  //...  const size_t n = 10; std::vector<students> students; students.reserve( n );  ( size_t = 0; < n; i++ ) {     students s;     std::cout << "enter name of student " << + 1 << ": ";     std::cin >> s.name;     // ...     students.push_back( s ); } 

to display ready filled container (an array or vector) can example range-based statement

for ( const students & s : students ) {     std::cout << "name " << s.name << std::endl;     //... } 

or ordinary loop

for ( size_t = 0; < n; i++ ) {     std::cout << "name " << students[i].name << std::endl;     //... } 

for vector condition of loop look

for ( std::vector<students>::size_type = 0; < students.size(); i++ ) //... 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -