3

I was having some problem when trying to loop thru multi-dimensional array in C programming. The expected output should be in this way:

Enter no. of names: 4
Enter 4 names: Peter Paul John Mary
Enter target name: John
Yes - matched at index location: 2

Enter no. of names: 5
Enter 5 names: Peter Paul John Mary Vincent
Enter target name: Jane
No – no such name: -1 

And here is my code:

int main()
{
char nameptr[SIZE][80];
char t[40];
int i, result, size;

printf("Enter no. of names: ");
scanf("%d", &size);
printf("Enter %d names: ", size);
for (i = 0; i<size; i++)
    scanf("%s", nameptr[i]);
getc(stdin);
printf("\nEnter target name: ");
gets(t);
result = findTarget(t, nameptr, size);
if (result != -1)
    printf("Yes - matched at index location: %d\n", result);
else
    printf("No - no such name: -1\n");
return 0;
}

int findTarget(char *target, char nameptr[SIZE][80], int size)
{
    int row, col;
    for (row = 0; row < SIZE; row++) {
        for (col = 0; col < 80; col++) {
            if (nameptr[row][col] == target) {
                return col;
            }
        }
    }
    return -1;
}

However, when I entered "Peter Paul John Mary" and trying to search , it does not return me with the "Yes - matched at index location: 2". Instead, it returned me with the No – no such name: -1. So I was thinking which part of my code went wrong. Any ideas?

Thanks in advance.

Modified portion

int findTarget(char *target, char nameptr[SIZE][80], int size)
{
int row, col;
for (row = 0; row < size; row++) {
    for (col = 0; col < size; col++) {
        if (strcmp(nameptr[row]+col, target)) {
            return row;
            break;
        }
        else {
            return -1;
        }
    }
}
}

1 Answer 1

4

You don't want to use nameptr[row][col] == target, you want to replace it with strcmp(nameptr[row][col],target) == 0. == compares the pointers (memory addresses), strcmp compares the actual values of the strings, and returns 0 when they match.

Sign up to request clarification or add additional context in comments.

7 Comments

After I changed it to string compare, when I trying to perform a search, the cmd just stopped working and it has no error message at the output there. Any ideas?
Sorry, just took another look at your code. nameptr[row][col] isn't a string; its a single character. Try nameptr[row]+col
Okay but now the problem is the returned index location isn't correct. Also, if I tried to find a name which is not in the array, it still returning me with the Yes, any ideas? Can you help me check with the modified portion?
When you entered the names, did you hit enter after each one, or did you put them all on one line? If the first, then get rid of col entirely; each name will be in its own row, so take out col and its loop, just return row. If the second, then its much more complicated.
Yes, I put all of them in one line. Yeah, I took out already but it still won't work.
|

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.