0

This's got probably an easy answer but it's been puzzling me a few hours already.
The code below declares a string object and fills it character by character. I can print every single character (line 16) but I can't print the string as a whole (line 19).
Could anybody tell me why is that so and how to fix it, please?

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string a;
	
	cout << "String a is: " << a << endl; //Nothing expected, fine
	char tmp= 100;
	for (int i=0; i<10; i++)
	{
		a[i]= tmp;
		tmp++;
	}
	for (int i=0; i<10; i++)
	cout << a[i];
	
	cout << "\na: " << a << endl;
	return 0;
}
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Narue
1

>string a;
This is an empty string.

>a= tmp;
a doesn't exist. a is still an empty string. The subscript operator does not change the size of a collection.

Votes + Comments
Thanks a lot.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.