Declaring an object that is an array?

closed account (LN3RX9L8)
I have to declare three objects of arrays with a named constant for the size. This is how I have it declared but in my program it says I have an error.
and Course is the name of my class. Can anyone give me tips on how to correctly write objects that are arrays
1
2
3
4
5
6
7
    
    const int size=100; 
    
    Course array [size];
    array[].setfirstName("Grant");
    
    
Last edited on
1
2
3
    const int size=100; 
    
    Course array [size];


This much is correct. You are creating an array of 'Course's. Now, array is 100 Course objects.


array[].setfirstName("Grant Goulden");

This does not make sense because you have 100 courses... so which one's name are you setting? You can't set all of their names this way.

If your intent is to set all of their names, you will need to write a loop and set each Course's name individually.

If your intent is just to set the first course's name:

1
2
3
4
array[0].setfirstName("foobar");
//    ^
//    |
//note we have to tell it the index of which of the 100 elements we want to act on 
closed account (LN3RX9L8)
@Disch

here are my directions:


Declares an array of three Course objects (use a named constant for the size of the array)
Calls a function named init to get the initial values for all the member variables of a Person object
Prints the information for each person

But in the program we are asking three different people their name, last name and age and then at the end it prints all three of those individuals information...
I don't really know how to do it for three
Do you know about for loops?

You don't do it "all 3 at once", you do it one at a time in a loop. Just write a for loop to iterate 3 times.
closed account (LN3RX9L8)
A little bit, would it be like this:
1
2
3
4
for(int i=0; i>size; i++)
{
//have information inside
}
Last edited on
very close. You want i<size. But otherwise, yes that is what you want.

Inside the loop body, you would then use i to index your array:

1
2
3
4
for(int i=0; i>size; i++)
{
   // do stuff with array[i]
}
closed account (LN3RX9L8)
@Disch okay thanks, but I wrote my code and it works almost perfectly except at the end where it should print all of the names, last names, and age, it only prints out the last name, and name the user entered for example:

Please enter the person's first name: Romeo
Please enter Romeo last name: Ruffini
Please enter Romeo Ruffinis age: 19
Romeo Ruffini is 19 years old
Please enter the person's first name: Grant
Please enter Grant last name: Goulden
Please enter Grant Gouldens age: 19
Grant Goulden is 19 years old
Please enter the person's first name: Gregg
Please enter Gregg last name: Zimmerman
Please enter Gregg Zimmermans age: 21
Gregg Zimmerman is 21 years old

//when it should print out all three in the end
You're probably getting input and doing output in the same loop. Do it in separate loops.
closed account (LN3RX9L8)
For each separate object? and sorry I am a bit confused on how to set them to arrays.. Would you like to look at my code? and I changed the names
No need to show code. This is a simple logic problem you should be able to solve on your own.

The body of a loop runs in full for each iteration.

1
2
3
4
5
for(int i = 0; i < 3; ++i)
{
    cout << "First line, i=" << i << endl;
    cout << "Next line,  i=" << i << endl;
}

outputs:

First line, i=0
Next line,  i=0
First line, i=1
Next line,  i=1
First line, i=2
Next line,  i=2


So if you are doing this:
1
2
3
4
5
6
for(int i = 0; i < 3; ++i)
{
    // ... get input for array[i]

    // ... output array[i] to cout
}


This will result in the alternating input/output you are seeing.

If you want all 3 things to be input before any data is output... you will need a separate loop.

The first loop will do only the input. Then, after all the input is done... you do another loop which outputs the data.
closed account (LN3RX9L8)
@Disch yes but I need to make my object into an array and when I do so my whole program fails can you help me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
        
    Person bo, co; 
    
    for(int i=0; i<3; i++)
    {
    bo.setfirstName("Grant");
    bo.getfirstName();
    co.setlastName("Goulden");
    co.getlastName();
    bo.setage(21);
    bo.getage();
    init_function(bo);
    bo.print_func();
    }
    return 0;
}
I don't see any array in that code you posted.
closed account (LN3RX9L8)
I removed it because I don't think I did it correctly I did it like this

const int size=3;
int bo[size], int co[size];
That will create two separate arrays of integers.

I thought you needed to create an array of Courses? Didn't you do that in your original post?
closed account (LN3RX9L8)
No names I changed the name because it was confusing. It is the basically the same
your assignment wrote:
Declares an array of three Course objects


For you to do this, you need to do 2 things.

1) You need to create a 'Course' struct/class.

2) You need to create an array of that Course:

Course array[size];



This:
 
int bo[size], int co[size];


is not an array of Course objects. It's 2 arrays of ints. That does not fulfill your requirements.



EDIT:

Actually now that I re-read your assignment... first it's talking about Course objects, then it's talking about Person objects. Which is it? =x Did you mistype it or something?
Last edited on
closed account (LN3RX9L8)
I can name it anything I want but it was course but its asking for people's name and last name and age. three we need to have three objects of arrays idk if that's correct. Btw thanks for your help
Topic archived. No new replies allowed.