Thread: problem with getline() file I/O

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    8

    problem with strings

    [sorry the title is misleading/incorrect. I edited my post, but couldnt change the title]
    Hi all, I'm having a problem with a function that is supposed to reverse a string. When i Run it, I'm getting a box that says
    the instruction at "some hex address" referenced memory at "some other hex address." The memory could not be "written"
    Thanks for any help you can give me

    Here is my code
    Code:
    string szReverse(string szString)
    {
    	string szNewString;
    	int iLength = szString.length();
    	for (int i = 0; i < iLength; i++)
    	{
    		szNewString[i] = szString[iLength - i - 1];
    	}
    	
    	return szNewString;
    }
    Last edited by hyperion; 12-07-2003 at 08:01 PM.

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    that is most likely because you are trying to read and write the same array element. If you have an odd number of elements i will be the same as string.length-i-1 when they meet in the middle. You won't ever write the first half into the second half either, because you'll have already written over the first half of the elements with new data. Use a second string store your reversed string.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >Use a second string store your reversed string.
    He did already. I think it's probably because when you instantiate "szNewString", it's empty (it's length() = 0). Try, first, to set its length equal to "szString", and then copy it.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Standard containers such as vector and string are not like their builtin equivalents (arrays if you were wondering). You can declare and use a container before it has any elements, they grow dynamically. You cannot use the subscript operator of a standard container if the element does not already exist. In your case, you can either resize szNewString manually:
    Code:
    string szReverse(string szString)
    {
      string szNewString;
      int iLength = szString.length();
    
      szNewString.resize(iLength);
      for (int i = 0; i < iLength; i++)
      {
        szNewString[i] = szString[iLength - i - 1];
      }
    
      return szNewString;
    }
    Or build it dynamically instead of treating it as you would an array:
    Code:
    string szReverse(string szString)
    {
      string szNewString;
      int iLength = szString.length();
    
      for (int i = 0; i < iLength; i++)
      {
        szNewString += szString[iLength - i - 1];
        // or
        // szNewString.push_back ( szString[iLength - i - 1] );
      }
    
      return szNewString;
    }
    See also Uses and Abuses of Vector for a similar discussion.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Minor nit, a prefix of "sz" implies "null terminated char *" in hungarian notation. This convention is, of course, pure evil, but can be confusing. str[3] = str[3] is perfectly acceptable, though it does nothing interesting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. File I/O problem
    By dacbo in forum C Programming
    Replies: 4
    Last Post: 01-17-2006, 08:22 AM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  4. getline() and file I/O
    By 7stud in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2003, 01:49 PM
  5. File I/O problem
    By LandMonster in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 10:36 AM