Array questions

I am having trouble with understanding how to loopthrough an array and change a string value and add another string value to it. The maximum amount of strings is 4 and if you enter more than 4 strings it should give error message.
Thank you for your help!
Do you have an array of strings?

ie.
 
std::string str[];


If so, why not use a vector?
 
std::vector<std::string> v_str;

I am only about half way through the book at school and vectors are not covered for about another 200 pages. It would seem that is they wanted to use that they would have put it before now in the book?
But, thank you for the help.
Which book are you using? I don't see the reason why string and vectors are that far apart. Both are contiguous data structure so they are pretty much closely related to each other. Either way, it's what the author prefer I guess.

Going back to your problem. Can you provide any code or algorithm outlining the code? I am assuming a lot of situations with what you said on the very first post so I can not specify a specific solution.
Thank you very much vince!
I am using Thomson's Learn C++ by Making Games

Here is the code that I have come up with so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char* argv[])
{
    //declare variables
    const int MAXITEMS = 4;
    string lastItem;
    string inventory[MAXITEMS] = {"Sword", "Armor", "Shield"};
    string replaceItem;
    string addItem;
     // print what is in inventory to start with
     cout << "We start with\n\n" << inventory[0] << "\n" << endl 
          << inventory[1] << "\n" << endl << inventory[2] << "\n" << endl << endl;
     
     cout << "Replace Item one in the inventory. " << endl << endl;
     getline(cin,replaceItem);
     inventory[0] = replaceItem;
     cout << endl << endl;
     cout << "We now have\n" << endl << endl;
           
     for (int i = 0; i < MAXITEMS; ++i)
     {
         lastItem = inventory[i];
         cout << inventory[i] << endl << endl;
                  
     }
     
            
        cout << "Add an Item to the inventory. " << endl << endl;
        getline(cin, addItem);
        inventory[3] = addItem;
        cout << endl << endl;
        cout << "We now have\n" << endl << endl;
    
    for (int i = 0; i < MAXITEMS; ++i)
    {
        lastItem = inventory[i];
        cout << inventory[i] << endl << endl;
    }
    
        
    cout << "Add an Item to the inventory. " << endl << endl;
        getline(cin, inventory[4]);
        cout << endl << endl;
    
    for (int i = 0; i < MAXITEMS; ++i)
    {
        lastItem = inventory[i];
        cout << inventory[i] << endl << endl;
        
    }
   
      
    system("pause");
    return EXIT_SUCCESS;
    
}
:
Can you tell me how I would code this if you enter more than 4 items give an error message?
thank you for your time and help!
Can't you use just one loop somehow in this to do what I am needing to do?
Topic archived. No new replies allowed.