1

I have a main function which has the code:

int main()
{
    const int SIZE = 80;
    char ca[SIZE];
    char * pc = ca;
    int fPrints = 0;
    int bPrints = 0;
    int lengthChecks = 0;

    ReadString(pc, SIZE);
    char test = 'X';
    int index = 0;
    index = FindIndexOfCharacter(pc, test);
    std::cout << test << " index " << index << std::endl;
    test = 's';
    index = 0;
    index = FindIndexOfCharacter(pc, test);
    std::cout << test << " index " << index << std::endl;

    std::cout << "Press ENTER";
    std::cin.get();
    return 0;
}

A function which reads the input

void ReadString(char * c, int maxLength)
{
    std::cout << "Enter a string less than " << maxLength << " characters." << std::endl;

    std::cin.getline(c, maxLength);
}

This function is supposed to return the index of a character in the array by using the pointer to the array and a test value and return it

int FindIndexOfCharacter(char * c, char testVal) 
{
    for (int i = 0; i < strlen(c); i++)
    {
        if (c[i] == testVal) {
            return (int)i;
        }
        else
        {
            return -1;
        }
    }

}

All I get is -1 for both searches, I am not sure what I am doing wrong. Would appreciate any help!

4
  • 1
    "All I get is -1 for both searches." What searches? You didn't show what kind of input you give to get that result. [ot] Why do you cast an int to an int? And you should probably not leave open the possibility that the compiler calls strlen() on every iteration of the loop. More to the point, you should not use int for sizes; that's why std::size_t exists (not that it matters until you start having strings > 2.1billionish characters long, but still...) Commented Oct 26, 2017 at 21:28
  • @underscore_d Would I be better off using a variable length in the function which equals strlen() and then using it in the loop? Commented Oct 26, 2017 at 21:30
  • 1
    I'd say so. For example, for (std::size_t i = 0, len = strlen(c); i < len; ++i). Of course, first #include <cstddef> to get the declaration of std::size_t. Commented Oct 26, 2017 at 21:31
  • Got it, thank you! Commented Oct 26, 2017 at 21:36

1 Answer 1

3

You're returning too early in FindIndexOfCharacter()! If the very first character you come across doesn't match testVal, FindIndexOfCharacter() will return prematurely.

Try moving return -1; after the for-loop; this way, you'd return -1 only after you've checked every character in c:

int FindIndexOfCharacter(char * c, char testVal) 
{
    for (int i = 0; i < strlen(c); i++)
    {
        if (c[i] == testVal) {
            return (int)i;
        }
    }

    return -1;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That did it! Appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.