how would I create an array of four strings and initialize the first three items to a value and then change the value of the first item and then add the fourth item and if I try to add another item give an error message?
Thank you all for your help!
Thank you, here is what I have so far?
If nothing else if you could point out what I am not understanding about this, I am just learning this language and I think my school maybe using a book that is not that up to date?
Thank you!
Hello,
Your code is just fine, you found out two things u made error:
1)
if you declare
string inventory[4], if u want to get the first node of the array, you should call inventory[0], not inventory[1]
2)
u should know difference between
i++, and ++i
here is an example:
1 2 3 4
Example 1
B=3;
A=++B;
// A contains 4, B contains 4
1 2 3 4
Example 2
B=3;
A=B++;
// A contains 3, B contains 4
Declaring variables at the beginning of the function is something that is necessary in C, but not good C++ style. I would declare lastItem inside the scope of the loops. (It then becomes a different valiable in each loop):
string lastItem = inventory[i];
@SteakRider:
i++ is not in any way better than ++i. In this case there is no real difference, but the prefix operator (++i) will in other cases result in simpler code. It's a good habit to use ++i.