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;
}

Change a[i]= tmp; with a += tmp; .

commented: grazie mille +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.

commented: Thanks a lot. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.