Problems with copying strings

This is a discussion on Problems with copying strings within the C++ Programming forums, part of the General Programming Boards category; Ok, so I am working at a program that operates with sets. Each set has a name and an array ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    43

    Problems with copying strings

    Ok, so I am working at a program that operates with sets. Each set has a name and an array for its elements. The user inputs for example "Alpha: 1 2 3 4 5", where Alpha is the name. I have to parse the string so that I get what's before the : symbol. It works perfectly with name declared as static (such as char name[10]), but when I am allocating memory for exactly as many characters as there are before the : , then I get either errors, either "Press any key to continue"... I used strncpy and also a function implemented by myself, but to no use.

    Does anyone have another idea for implementing this? Thank you.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Would you ask a mechanic to fix your broken car without showing up with your car?

    Where is your code?
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Code:
     class CMultime
    {
    	int* m_Elemente;
    	char* m_Nume;
    public:
    	void preiaNume(char* sursa)
    	{
    		int caractere = 0;
    		int lungime = strlen(sursa);
    		for(int i = 0; i < lungime; i++)
    		{
    			if(sursa[i] != ':')
    			{
    				caractere++;
    			}
    			else
    			{
    				break;
    			}
    		}
    		if(caractere)
    		{
    			m_Nume = new char[ caractere ];
    			for(int i = 0; i < caractere; i++)
    				*(m_Nume + i) = *(sursa + i);
    		}
    		else
    		{
    			cout << "Nu ai respectat formatul sau nu ai introdus numele multimii." << endl;
    		}
    	}
    This would be it. After using a function to display m_Nume, I get the string before : and some ASCII weird stuff.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Veneficvs View Post
    Code:
     
    			m_Nume = new char[ caractere ];
    			for(int i = 0; i < caractere; i++)
    				*(m_Nume + i) = *(sursa + i);
    This would be it. After using a function to display m_Nume, I get the string before : and some ASCII weird stuff.
    You need to null terminate this string.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by MK27 View Post
    You need to null terminate this string.
    Thank you! This is the modified version.
    Code:
    for(i = 0; i < caractere; i++)
    				*(m_Nume + i) = *(sursa + i);
    			*(m_Nume + i) = NULL;
    Now, the main has
    Code:
     void main()
    {
    	CMultime prima;
    	char sir[10];
    	cin.getline(sir, 10);
    	prima.preiaNume(sir);
    	prima.afis();
    }
    . It compiles great, it even gets me the string, but I hear a sound and the program does not stop. But thank you very much.. I forgot about that NULL ending.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I suggest that you change m_Elemente to be a std::vector<int>, and change m_Nume to be a std::string. The parameter named sursa looks like it should be a const char* instead, though if you go with my suggestion then it might as well be a const std::string&.

    By the way, it would be good if you would translate your code to English.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Sorry. From now on I will translate it. I found the problem myself. It is because when I say *(m_Nume + i) = NULL, it puts the NULL into the next element.. which does not belong to the string because of the dimension. Therefore, I make new char [caractere + 1] instead. Thank you for all your replies.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Veneficvs
    I found the problem myself. It is because when I say *(m_Nume + i) = NULL, it puts the NULL into the next element.. which does not belong to the string because of the dimension. Therefore, I make new char [caractere + 1] instead.
    Good to hear that, but unless you have special reasons for doing otherwise, you should still prefer using std::string in this case. If not, it looks like you are going to have to manually manage the memory for two things, and you're probably going to get it wrong.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding a character in an array of strings
    By ewoods in forum C Programming
    Replies: 3
    Last Post: 08-12-2009, 03:37 PM
  2. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  4. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 02:13 PM
  5. Help w/ comparings two strings case sensitive
    By ikkin in forum C Programming
    Replies: 7
    Last Post: 11-13-2003, 07:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21