How to input into a Structure Array?

Classroom ID Classroom Name Rental Fees
CR1 Apple 50.00
CR2 Bottle 30.00
CR3 Caramel 80.00
..................................................
fyi: struct classroomType
classroomType.classrooms[20]

I wanted to write a C++ code to insert
Classroom ID: CR6
Classroom Name: Faith
Rental Fees: 75.00


What I did was:
classroomType classrooms[5] = {"CR6", "Faith", 75.00};

But I don't think that I am right. Please help. Thank you.
Last edited on
I don't see anything here or aren't I looking properly? Take a look at this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct classroomType
{
std::string classID, Name;
double fees;
}classrooms[20];

int main() 
{
for (unsigned int i=0;i<20;++i)
{
  cin>>classrooms[i].classID;
  cin>>classrooms[i].Name;
  cin>>classrooms[i].fees;
}
//do the rest here
//....
return 0;
}
1
2
3
4
5
struct ClassRoom {
    string id;
    string name;
    float rentalFee;
};

1
2
3
4
ClassRoom classrooms[20];
classrooms[5].id = "CR6";
classrooms[5].name = "Faith";
classrooms[5].rentalFee = 75.00;


EDIT:
A more advanced way would be to create constructor for the struct:
1
2
3
4
5
6
7
8
9
10
11
struct ClassRoom {
    string id;
    string name;
    float rentalFee;
    // A constructor:
    ClassRoom( string _id, string _name, float _rentalFee) {
        id = _id;
        name = _name;
        rentalFee = _rentalFee;
    };
};

1
2
3
ClassRoom classrooms[20];
// calling constructor
classrooms[5] = ClassRoom ("CR6", "Faith", 75.00);

But I am afraid I am only making it harder for you to understand :).
Last edited on
Thank you so much for teaching me! :)
Topic archived. No new replies allowed.