954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please Gurus Kindly help with tracing the error

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
#include
#include
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[i].name ;

cout << "Enter age: " ;
cin >> students[i].age ;

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

mweshk
Newbie Poster
8 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

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

mweshk
Newbie Poster
8 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
cout << students[i].name << " " << students[i].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

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You