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;
}

Recommended Answers

All 4 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.