I have a problem in c++ the teacher asking us to display a field of structures that contains n=100 students. Is this the right way to do it ?
#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 does not make sense. You should either declare an array of type
Students
or use some other standard container as for examplestd::vector<Students>
. And after the container will be defined you have to enter values for each element.For example
Or
To display the all ready filled container (an array or a vector) you can for example the range-based for statement
or an ordinary for loop
For a vector the condition of the loop will look