Appending to the front of a string in a class...

This is a discussion on Appending to the front of a string in a class... within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include "ooint.cpp" main() { ooint ooint1 = "100"; cout << ooint1 << " "; ooint1.intcat("1"); cout << ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Appending to the front of a string in a class...

    Code:
    #include <iostream.h>
    #include "ooint.cpp"
    
    main()
    {
    	ooint ooint1 = "100";
    	cout << ooint1 << " ";
    	ooint1.intcat("1");
    	cout << ooint1 << " ";
    	ooint1.intfcat("3");
    	cout << ooint1 << " " << "\n\n";
    
    	return 0;
    }
    There's my program... and in the implimentation file:

    Code:
    void ooint::intfcat(const char * Int1)
    {
    	//char Int2[(strlen(Int1))+(strlen(CString))];
    	char Int2[1024];
    	for (int i=0; i<=strlen(Int1); i++)
    		Int2[i] = Int1[i];
    	strcat(Int2,CString);
    	strcpy(CString,Int2);
    }
    It gives me a weird error: (see attatchment on next post)

    If I click Ignore, it does what I want as output:

    100 1001 31001
    Last edited by Trauts; 11-22-2002 at 11:30 PM.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Here's the error box:
    Attached Images Attached Images  

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >>>i<=strlen(Int1);

    Not sure but for one, this should be:

    i < strlen(Int1);

    ...to avoid access violations...



    ITSA
    Socket Library!

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Now I get this:

    OOINTEX caused an invalid page fault in
    module OOINTEX.EXE at 017f:00402c4c.
    Registers:
    EAX=3166fde4 CS=017f EIP=00402c4c EFLGS=00010212
    EBX=00560000 SS=0187 ESP=0066f930 EBP=0066fd88
    ECX=00790d38 DS=0187 ESI=817ef74c FS=3d9f
    EDX=0066f984 ES=0187 EDI=0066fd88 GS=0000
    Bytes at CS:EIP:
    8b 48 04 51 e8 3b 4f 00 00 83 c4 08 5f 5e 5b 81
    Stack dump:
    0066f984 0066fdec 817ef74c 00560000 cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc

    And then it gives me the other error box.

    Would you be interested in having the whole source?

    BTW: Now if I click "Ignore" it doesn't work at all, so I changed it back.

    [EDIT] for (int i=0; i<=strlen(Int1); i++)
    Int2[i] = Int1[i];

    That bit of code is because the Int1 is a constant and it wouldn't let me use strcpy. But if I change it so that it isn't a constant anymore, then the user would have to pass two arguments to the class: ooint1.intfcat("3","3");
    Last edited by Trauts; 11-23-2002 at 08:10 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    this might work:
    Code:
    class ooint
    {
      public:
        ooint(const char *);
        char CString[80];
        void intcat(const char *);
        void intfcat(const char *);
    };
    
    ooint::ooint(const char * Int) 
    {
      strcpy(CString, Int);
    }
    
    void ooint::intcat(const char * Int1)
    {
       strcat(CString, Int1);
    }
    
    void ooint::intfcat(const char * Int1)
    {
      char Int2[1024];
      strcpy(Int2, Int1);
      strcat(Int2,CString);
      strcpy(CString,Int2);
    }
    
    int main()
    {
       ooint ooint1 ("100");
       cout << ooint1 << " ";
       ooint1.intcat("1");
       cout << ooint1 << " ";
       ooint1.intfcat("3");
       cout << ooint1 << " " << "\n\n";
    
       return 0;
    }
    though I don't recommend using CString as the name of a variable as it is too easily confused with the CString class from VC++, and if you are using that compiler, may be causing some of the problem.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I've attatched the files...

    The reason I have it named CString is that the class I'm modifying had it named that.

    None of the operator overloading will apply anymore after I finish this, I need to change that still.

    The intcat works fine, but the intfcat doesn't.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I think the problem is it is trying to overwrite the const char *, but I don't know how to fix it without passing 2 identicle strings to the function, and I don't want that.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Elad!

    Code:
    void ooint::intfcat(const char * Int1)
    {
      char Int2[1024];
      strcpy(Int2, Int1);
      strcat(Int2,CString);
      strcpy(CString,Int2);
    }

    That works just fine... as long as it isn't in the class:
    Code:
    #include <string.h> // header for strings
    
    char CString[80] = "101";
    
    void intfcat(const char * Int1);
    
    main()
    {
    	intfcat("2");
    	cout << CString << endl;
    
    	return 0;
    }
    
    void intfcat(const char * Int1)
    {
      char Int2[1024];
      strcpy(Int2, Int1);
      strcat(Int2,CString);
      strcpy(CString,Int2);
    }
    It seems to work fine if I edit out the strcat(Int2,CString);
    meaning it doesn't give the error, but it doesn't work, either.
    Last edited by Trauts; 11-25-2002 at 08:39 AM.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Code:
    void ooint::intfcat(const char * Int1)
    {
      char Int2[1024];
      strcpy(Int2, Int1);
      strrev(CString);
      strrev(Int2);
      strcat(CString,Int2);
      strrev(CString);
    }
    That gives the same error as

    Code:
    void ooint::intfcat(const char * Int1)
    {
      char Int2[1024];
      strcpy(Int2, Int1);
      strcat(Int2,CString);
      strcpy(CString,Int2);
    }
    [EDIT]I figured out why it has the same error... its overflowing!

    If I try to use an "oostring" instead of a string... oostring is a string class:

    Code:
    void ooint::intfcat(const char * Int1)
    {
    	oostring Int2;
    	oostring Int3;
    	Int2 = Int1;
    	Int3 = CString;
    	Int2 += Int3;
    	for (int i=0; Int2[i] != '\0'; i++)
    		CString[i] = Int2[i];
    	CString[i] = '\0';
    }
    Then I get:

    Code:
    100 1001 index out of range: 5 string: 31001
    Assertion failed: 0 <= k && k < strlen(CString), file c:\program files\microsoft
     visual studio\myprojects\oostring.cpp, line 103

    Please, can someone help me figure out how to get it to resize the object? It works if it is done outside of a class...

    I don't want to use a vector unless I have to.
    Last edited by Trauts; 11-26-2002 at 04:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM

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