Thread: string to c-string junk ?

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    2

    string to c-string junk ?

    Hello, I am in my first year studying computer science so I'm pretty new to coding, I am trying to read user input into a string and then copy that to a c-string, but my c-string keeps showing junk at the end. I have spent over an hour trying to figure this out.
    Attached Files Attached Files
    Last edited by cm4424; 02-05-2014 at 01:21 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just use the c_str() function of std::string. But why do you need a C-string in the first place?
    Also post your code on the board, if possible, using code tags.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char *String1Ptr;
    	char *String2Ptr;
    	char *String3Ptr;
    	string temp;
    	cout << "Please enter the first sentence.";
    	getline(cin, temp);
    	int tempLength=(temp.length());
    	String1Ptr = new char[tempLength];
    	for(int i = 0; i < tempLength; i++)
    		{
    			String1Ptr[i] = temp[i];
    		};
    All you have is an array of characters, not a properly terminated C-style string. A proper C-style string is one where there is a terminating null character. The "junk" you see is because you lack this null character... when printing a char array the output will continue character-by-character until you happen upon a random null somewhere in memory. You aren't allocating enough memory (+1 for that missing null) and you then need to set that last character to '\0' for it to be considered a true string in C.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    2
    Elysia--this is for a project to familiarize us with c-strings, sorry about not posting my code on the board, this is my first post.
    hk_mp5kpdw--thank you for the explanation.
    and thank you both for taking the time to help out a beginner.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is this a specific school course? So I can recommend people to avoid it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-27-2013, 04:36 AM
  2. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  3. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  4. Replies: 1
    Last Post: 10-31-2005, 11:36 AM
  5. Removing junk from end of string
    By dirkduck in forum C++ Programming
    Replies: 7
    Last Post: 05-20-2002, 01:15 PM

Tags for this Thread