Function call resulting in a strange looping

This is a discussion on Function call resulting in a strange looping within the C++ Programming forums, part of the General Programming Boards category; Hey, all: I've attached a file here. Basically, it's a program that allows me to test string lengths, copy strings, ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Question Function call resulting in a strange looping

    Hey, all:

    I've attached a file here. Basically, it's a program that allows me to test string lengths, copy strings, and compare them(letter-for-letter).

    If the 2 comparision string are identical, the program should return a 0, otherwise it should return a 1.

    In any case, the situation is that when I enter "R" from my menu, I am taken to my "readString" function where I am prompted to enter a string. This is okay. The problem, however, is that my program doesn't seem capable of escaping the "read" function.

    It does something really weird. It keeps asking me for source string based on the number of characters in the first string I enter.

    e.g. I enter "the" ( asks me to enter source string 3 more times)
    e.g. I enter "them" (asks for a source string 4 more times)

    Can someone please check out my attached code and provide me with some feedback?

    Thanks!!
    Attached Files Attached Files

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int stringLength(char* data)
    {
       while(*data)
       {
          strLength++;
       }
       return strLength;
    }
    This doesn't look right. First, you're not increasing the data pointer at each step so you're looking at the first element all the time.
    Second, you should compare with \0 (String NULL terminator). I don't know if \0 is the same as 0 (the number) but I'd use \0 anyway.

    Code:
    *sourceString++ = *destinationString++;
    This is a matter of taste. I prefer not doing several things in the same statement (copy data and increase the pointers). I'd split them to several lines:
    Code:
    *sourceString = *destinationString;
    sourceString++;
    destinationString++;
    It's a lot easier to see what happens first(the assignment or the addition).

    Code:
    readData(&data[maxCharacters]);
    This will point to the character after the last charcacter in the string, which is the NULL terminator, meaning that there is no space to enter a string. Simply call

    readData(&data[0]);

    or even better

    readData(data);

    instead
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Unhappy still doesn't work...

    Unfortunately, I still have that weird looping going on in my readData function.

    I tried calling readData(&data[0]);
    and readData(data);

    Neither resolves the issue.

    Hmmm.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, you don't have your menu selection inside the loop so you will never be promted to enter another option. If you press 'R' the program will ask you to enter strings over and over again.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    some of it works..not all

    Thanks a bunch for your help. You're very smart.

    My readData now seems to work, but every time I call another of my functions from my menu, my program just exits. Is there something I'm doing wrong in my method calls??

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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