1

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.

1
  • 3
    Why not use std::string and its operator+=? Commented Mar 4, 2011 at 13:07

2 Answers 2

4

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

aug[6] = 0; 
Sign up to request clarification or add additional context in comments.

2 Comments

In fact, what's the point of char[5]? That's a carriage return character.
@suszterpatt i want that enter character at the end of the string
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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.