chores
is an array of strings. Each "element" in the array is an individual string. Those elements are accessed via an "index". The index is basically the ID number of the string you want to access.
For example, you could say that:
1 2 3 4
|
chore1 is the same as chores[0]
chore2 is the same as chores[1]
chore3 is the same as chores[2]
... etc
|
The thing to note here is that the index is zero based. IE:
chores[0]
is the first element in the array,
not chores[1]
chores[i]
uses the loop counter 'i' as the index. Since the loop counter changes each time through the loop, it means we're accessing a different string each time the loop runs. So....
1 2 3 4 5
|
for(int i = 0; i < 5; ++i)
{
cout << i+1 << ") ";
getline(cin,chores[i]); // <- which string are we accessing here?
}
|
The first time that loop runs, i == 0, therefore we are getting chores[0]
but the next time it runs, i == 1, so we're getting chores[1] instead
ie: each time the loop runs, we're getting a different string. A different element in the array.
again, I understand what the loop does and how it works for the most part, but the chore[complete-1] throws me off. |
It's the same idea. Instead of having an if/else chain checking to see which number the user entered, we just use that number directly to access the appropriate string.
IE if the use entered "4", then complete == 4, and therefore we print chores[3] (ie: the 4th string in the array)
The -1 is there because the array is zero based, but 'complete' is 1 based. The -1 basically "translates" 1-based to 0-based
Thanks for the help, I hope I'm not asking too many questions. |
You're not. It's nice to speak with people who actually want to learn, rather than people looking for the fastest way to solve their homework problem.
Feel free to ask any other questions you may have. I'm happy to clarify things that are still confusing to you.