Hey,
I'm writing a function that searches for an id in an array of a structure, for some reason it returns -1 everytime, so I was just hoping another set of eyes could help me clarify this.

int searchAr(student studentar [], int size, int searchid)
{
    int i;
    for(i = 0; i<(size-1); i++)
    {
          if (studentar[i].studentID == searchid)
             {
             return i;
             }
             else
             {
             return -1;
             }
    }
}

> There's only one place in your function where it explicitly returns -1, (in the else code block) ...

> Did you set the studentID variable after you declared a struct-variable from it ?
If no, that's the problem :)

Look carefully at the code of your searchAr function: the for-loop is executing one time only :) ...

int searchAr(student studentar [], int size, int searchid)
{
    int i;
    for(i = 0; i<(size-1); i++)
    {
          /* This for-loop is executing only one time */
          if (studentar[i].studentID == searchid)
             {
             return i;
             }
             else
             {
             return -1;
             }
    }
}

Actually this code is wrong ...

int searchAr(student studentar [], int size, int searchid)
{
    for(int i = 0; i<size; i++)
          if (studentar[i].studentID == searchid) return i;
    return -1;
}

This is already a lot better :) ...

Edit:: for(i = 0; i<(size-1); i++) why is the condition i<(size-1) , I would make i<size of it ...

I've played around with it more and it seems like this would make more sense but now it returns nothing.

int searchAr(student studentar [], int size, int searchid)
{
    int i;
    for(i = 0; i<size; i++)
    {
          if (studentar[i].studentID == searchid)
             {
             return i;
             }
    }
    return -1;
}

I've played around with it more and it seems like this would make more sense but now it returns nothing.

int searchAr(student studentar [], int size, int searchid)
{
    int i;
    for(i = 0; i<size; i++)
    {
          if (studentar[i].studentID == searchid)
             {
             return i;
             }
    }
    return -1;
}

>>but now it returns nothing: that's not true, the function is always returning a value :) ...

What I was doing was using a cout to see if it was passing back the i or -1 now it doesn't print anything when the cout is used.

I've built a small program around your (fixed) search function and the situation you described:

#include <iostream>

using namespace std;

struct student
{
	int studentID;
};

int searchAr(student studentar [], int size, int searchid)
{
    for(int i = 0; i<size; i++)
          if (studentar[i].studentID == searchid) return i;
    return -1;
}

int main()
{
	student stud[5];
	stud[4].studentID = 2;
	cout << "Student with ID \'2\' found at place: " << searchAr(stud,5,2) << " (in the array of students)" << endl;
	return 0;
}

I have to say that it's working in the way it was intended or am I wrong ?

Ya its fixed now I had a problem in my main I didn't notice. Thanks for the help!

This question has already been answered. Start a new discussion instead.