Hi Gurus, I have an assignment am doing. The user should enter details of 5 students and return results on the screen.I have the code below and its return 1 error. Can someone kindly help me.With regards

#include<iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
typedef struct
{
string name;
int age;
int i;
}
Student;
Student students[5];
for (i = 0 ; i < 5 ; i++)
{
cout << "Enter Name of student " << i+1 << ": " ;
cin >> students.name ;

cout << "Enter age: " ;
cin >> students.age ;

}
cout << students.name << " " << students.age << "yrs" << endl;
return 0;
}

int i should be outside struct definition. Still post what error you are getting

Edited 4 Years Ago by DJSAN10: n/a

Yes thats where the error was coming from but doesnt give me the desired results
I wanted output like this;
John 22yrs
Peter 12yrs
mike 11yrs
joyce 10yrs
cate 11yrs

Kindly assist

Post your latest code along with error (if any) or output. We already know your expected output now. Post what you are getting

cout << students.name << " " << students.age << "yrs" << endl;

Put this in a new loop from 0 to 4 again. What you have done will not print since i will be incremented to 5 and there is no element at 5th position in array.You will again have to loop through entire array from the beginning and print element one by one

This question has already been answered. Start a new discussion instead.