Antrromet
Antrromet

Reputation: 15424

string not printing properly

I've used the following code for getting a string and using its first character to make another string:

char gramG[100],aug[100],start;
cout<<"\nEnter the grammar:\n";
cin.getline(gramG,100,'.');
start=gramG[0];
aug[0]=start;
aug[1]='\'';
aug[2]='-';
aug[3]='>';
aug[4]=start;
aug[5]=char(13);
cout<<aug;
cout<<aug[0];

In the above code when i'm printing 'aug' it prints as ' ¶'->A ' if A is my start symbol. If i am printing only aug[0] then it is printing correctly A. But when i am printing the string as a whole the aug[0] value is printed as some garbage. Please help.

Upvotes: 1

Views: 592

Answers (2)

Ujjwal Manandhar
Ujjwal Manandhar

Reputation: 2244

you have to terminate the string with null value
for c++ the null value is \0
use this
aug[6] = '\0';
this should work;

Upvotes: 0

Erik
Erik

Reputation: 91320

aug is treated as a 0-terminated character array. 0-terminate it.

aug[6] = 0;

Upvotes: 4

Related Questions