Adding a string to an array's index

I'm currently trying to initialize a string array, and then trying to put the "*" inside the 0th index of the array. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

using namespace std;

  int main(){
	string a[10];
	a[0] + "#";
	cout << a[0];
	return 0;
}


Can I somehow do this with a for loop, so that it can add a * to every index?
To place a "*" at nth index you use the assignment operator: a[n] = "*". What you have used is the + operator which has been overloaded in the string class for concatenation.

Can I somehow do this with a for loop, so that it can add a * to every index?

Yes. You just iterate over the array and perform the same operation to each index.
1
2
3
for(int i = 0; i < 10; ++i){
    a[i] = "*";
}


Look at the following for further guidance:

http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/reference/string/string/?kw=string
http://www.cplusplus.com/reference/array/array/?kw=array
Last edited on
But let's say that I don't have a "*" at every index. In the first iteration of a loop I would do a[i]= "*", but what if I want to add to that index? So a[i] already has a "*", so how would I add another "*" to that index?
Topic archived. No new replies allowed.