How to prompt to enter details of 5 users and output them

Hi good friends. I am stack here please help.
I want to write a program that prompts a user to enter details of up to 5 students
the displays the information on the screen.
Kindly help. I am new to C++
#include<iostream>
using namespace std;
int main ()
{
int i ;
int rn[5] ; // Roll Number
char name[99] ;
for (i = 0 ; i < 5 ; i++)
{
cout << endl << "Enter Name : " ;
cin >> name[i] ;
cout << endl << "Enter Roll No. : " ;
cin >> rn[i] ;
cout << endl ;
}
system ("pause") ;
return 0 ;
}
Lynch; Thanks but I want out put in this form;
e.g
John 22 yrs
Peter 23yrs
Paul 24yrs
Mike 32yrs
Jane 10yrs

Thanks alot
you can use a stucture to store student information.
And yu can create an array with size 5 to store this data.

1
2
3
4
5
6
typedef struct{
  string Name;
  int Age;
}Student;

Student students[5];


Lynch told you how to get te keyboard info using cin:

1
2
3
4
5
6
7
8
9
for (i = 0 ; i < 5 ; i++)
{
cout << "Enter Name of student " << i+1 << ": " ;
cin >> students[i].Name ;

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

}


now just print the info using cout:

cout << student[i].Name << " " << students[i].Age << "yrs" << endl;
Last edited on
This is what I have come up with but returning 1 error that I can bot trace. Help kindly

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

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

}
cout << students[i].name << " " << students[i].age << "yrs" << endl;
return 0;
}
first of all please use code tags.... makes your code much more readable...

second you just copied my code. that's not going to work. Please try to figure out what is going on.
the struct is a type like int, double, long etc. Put it outside the main.

in the forloop a variable i is used. where is it declared? do you want every student to contain a element i?

the forloop increases the i variable to 5 then stops. i remains 5 and you try to print students[5], is this what you want?

I will help you, i will not give you the answer directly.
Jikax,
Thanks alot for your help.Its really enlightening me and making me think. I have take i, which the counter, and declared it as below
Student;
int i;
Student students[5];
for (i = 0 ; i < 5 ; i

The program runs without error but does not out put the 5 students I have entered. Yes I want to print the students[5]
Yes I want to print the students[5]


actually you do not want that, it causes your application to crash;)

students[5] is the 6th element of the array, which does not exsist...
the array contains data on place 0-4.

so you want to print the students[0],students[1],students[2],students[3],students[4]
Last edited on
Jikax, Thank you very much. I have resolved the problem. Thats why we need guru's like you
np
Topic archived. No new replies allowed.