string array

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!
please, send your code, i will help to find where the problem is?
I would use like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define ARRAY_SIZE 100

char laArray[4][ARRAY_SIZE];

/*  
      Giving default value
*/
strcpy(laArray[0],"item1");
strcpy(laArray[1],"item1");
strcpy(laArray[2],"item2");

/*  
      Changing first item
*/

strcpy(laArray[0],"changed text");



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!

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

#include <iostream>
#include <string>

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
     cerr << "We start with\n" << endl;
     
     for (int i = 0; i < MAXITEMS; ++i)
     {                 
         lastItem = inventory[i];
         cerr << inventory[i] << endl << endl;                  
     }
     
     
     cerr << "Replace Item one in the inventory. " << endl << endl;
     getline(cin,replaceItem);
     inventory[0] = replaceItem;
     cerr << endl << endl;
     cerr << "We now have\n" << endl << endl;
           
     for (int i = 0; i < MAXITEMS; ++i)
     {
         lastItem = inventory[i];
         cerr << inventory[i] << endl << endl;                  
     }
     
        cerr << "Add an Item to the inventory. " << endl << endl;
        getline(cin, addItem);
        inventory[3] = addItem;
        cerr << endl << endl;
        cerr << "We now have\n" << endl << endl;

        for (int i = 0; i < MAXITEMS; ++i)
        {             
          
            lastItem = inventory[i];
            cerr << inventory[i] << endl << endl;            
        }
             cerr << "error message" << endl << endl; 
        
    system("pause");
    return EXIT_SUCCESS;
Last edited on
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 




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
#include <iostream>
#include <string>

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
	cerr << "We start with\n" << endl;

	for (int i = 0; i < MAXITEMS; i++)
	{                 
		lastItem = inventory[i];
		cerr << inventory[i] << endl << endl;                  
	}


	cerr << "Replace Item one in the inventory. " << endl << endl;
	getline(cin,replaceItem);
	inventory[0] = replaceItem;
	cerr << endl << endl;
	cerr << "We now have\n" << endl << endl;

	for (int i = 0; i < MAXITEMS; i++)
	{
		lastItem = inventory[i];
		cerr << inventory[i] << endl << endl;                  
	}

	cerr << "Add an Item to the inventory. " << endl << endl;                            
	getline(cin, addItem);
	inventory[3] = addItem;
	cerr << endl << endl;
	cerr << "We now have\n" << endl << endl;

	for (int i = 0; i < MAXITEMS; i++)
	{             

		lastItem = inventory[i];
		cerr << inventory[i] << endl << endl;            
	}

	system("pause");
	return EXIT_SUCCESS;
}
Last edited on
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.
Topic archived. No new replies allowed.