How to put value in items of an array

Hello,

Can you guys help me? Like for example I want to put a value in the items of an array.

I want to put a value in each item in this array:

string items[] = {"item1","item2","item3"};

Thanks in advance ..
If you mean by standard input (i.e. cin), then you simply use the name of the array and the subscript (inside the array operator, []) you want to put the value in. So something like:

items[0] = "Banana";

would put "Banana" in the 0th element of the array.

A more efficient method would be to use a for loop that iterates from 0 to less than the number of elements in the array. See the following program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
  string items[] = {"item1","item2","item3"};
  
  for(int n=0; n<3; n++) // For loop goes from 0 to 2, as there is no 3rd element
    cin >> items[i]; // in each iteration, let user cin the nth element
   
 // we can print out the array in the exact same way to see that it worked 
  for(int k=0; k<3; k++)
  cout << items[k] << "  "; 
}

Last edited on
No, I mean that items 1-3 will have a value of a number like 1 or 5, is there even a way to code it like that or is it even possible? Or the only way the items would have a value is to code

int item1,item2,item3 = "value";

???

Even though, Thanks for the reply anyway ..
closed account (48T7M4Gy)
I want to put a value in each item in this array:


string items[] = {"item1","item2","item3"};

So which part of that didn't do what you want?

Or is it you want to store 3 integer items in an array? In which case the following would apply:

int number[]= { 1, 2, 3 };

and (almost) miraculously number[0] = 1, etc ...
Last edited on
Well, thats not really the syntax or the answer I'm looking for ..

Here is my code:

string items[] = {"item1","item2","item3"};

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

items[i] = 500;

}

Here is what I want to know ..

Is 500 the value of the three items in the array? Or am i gonna make the items in the
for loop "int items[i] = 500;" like this?
closed account (48T7M4Gy)
Well, thats not really the syntax or the answer I'm looking for ..

From this end the following approach is mutually more beneficial than a 20 questions exercise.

I suggest you run it as a program, show us the program including any output and error messages (with line numbers) that you get. Then tell us why your output isn't as you expect.

If you've already specified items[0] as string "item1" 500 will not be the value in the items array, in fact you'll have a type error on your hands.

But please be a good chap and do what I suggested above. :)

Last edited on
Hello!
and array is initialized when you use it's index
string items[] = {"item1","item2","item3"}; would simply mean that
1
2
3
items[0]="item1"
items[1]="item2"
items[2]="item3"

but if you want to initialize more than 2 array to the same value you can do this
1
2
string items[];
items[0]=items[1]=items[2]="hello";


another way is to use a for loop and take the input from the user
1
2
3
4
5
6
7
string items[10],str;
for(int i=0;i<=10;i++)
{
  cout<<"enter the string";
  cin>>str;
  items[i]=str;
}

you could use a similar for loop to output the array.
Topic archived. No new replies allowed.