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<<开发者_JAVA技巧"\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.
aug is treated as a 0-terminated character array. 0-terminate it.
aug[6] = 0;
you have to terminate the string with null value
for c++ the null value is \0
use this
aug[6] = '\0';
this should work;
精彩评论