Thread: char array to string

  1. #1
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15

    char array to string

    Okay, I've kind of figured out how to take a char array and convert it to a string, but I'm having one problem. I'm using a char array of size 20, but when I convert an array with less than 20 characters I get the string followed by garbage. Like this:

    char temp[20];
    string word;

    word = temp;//temp is filled with input at this point ex:The

    cout<<word;

    ^^^^^this statement gives me:

    TheKKKKKKKKKKKKKKKKK//where K=garbage

    So my question is how can I just convert the valid contents of the char array to the string instead of the garbage too.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    12
    What compiler are you using? I have tested your code using various length strings up to 20 chars long and there is no problem.
    I use the Borland compiler. There is a free download.
    http://www.borland.com/products/down..._cbuilder.html

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Are you sure temp is null-terminated? What is the exact code you used to fill temp?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15
    This is what I've got, basically I'm reading in one letter at a time until I reach the end of a word or punctuation, converting to a string and then moving on to the next word.


    while (! fin.eof())
    {
    while (fin.get(temp) && temp != '\n')
    {
    if (temp != ' ' && temp != ',' && temp != '-' && temp != '!' && temp !='.')
    {
    character[e] = temp;
    e++;
    }
    else
    {
    word = character;
    cout<<word;
    e=0;
    }
    }
    i++;


    }

  5. #5
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    This is what I've got, basically I'm reading in one letter at a time until I reach the end of a word or punctuation, converting to a string and then moving on to the next word.

    Code:
    while (! fin.eof())
    {
                    while (fin.get(temp) && temp != '\n')
    	{
    	               if (temp != ' ' && temp != ',' && temp != '-' && temp != '!' && temp !='.')
    		{
    			character[e] = temp;
    			e++;
    			}
    		else
    		{
    			word = character;
    			cout<<word;
    			e=0;
    		}
    	}
    	i++;
    
    		
    }
    And maybe to explain what Salem said,...

    strings have to be terminated with a \0 [char(0)] at the end. Since you did not add this \0 or initialized the target string with a lenght and filled it \0, when you output the string, cout will display everything in the string and then the data of the memory "behind" the string until it reaches - by chance - a \0 byte in memory. This is a *very* bad thing to do.

    By the way, the \0 character in the string doesn't count for it's length. So a string uses length +1 bytes of memory. (ya, I know, unless it's a multibyte string....)

  6. #6
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15
    Originally posted by Salem

    Please use [code][/code]Tags
    when posting code
    I'll do that in the future, sorry I was in a hurry. Definitely makes code easier to read.


    Thanks for the help guys, it's appreciated. I didn't know that the string had to be terminated like that, but it makes sense that it should be. It makes my output look a little better anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM