Reputation: 69
I have been trying to iterate over a struct array, but for some reason it only goes through once. I am still trying to get the hang of c and its use of pointers. For the function, I have to use the **books array and iterate over it. For instance, I need to return how many books that cost less than $50 dollars. I don't know why it is iterating through only once.. When I use a print statement to check the "number_of_books", I get five which is the correct size of the array. Help would be appreciated!
// calling the function in main as : int b = get_cost(books, 50)
int get_cost(book **books, int cost){
int get_book_total = 0;
for(int i = 0; i < number_of_books; i++){
if(books[i]->cost < cost){
get_book_total++;
}
return get_book_total;
}
}
Upvotes: 0
Views: 137
Reputation: 423
Your return statement is in your for loop, so your function returns after the first iteration.
Upvotes: 1