Reputation: 901
#define NUMLEG 7
int Search_type(char leg_type[6][10], int travel_int[6][15], int trip_num);
c = Search_type(type,leg_type, travel_int, trip_num);
if (c == -1)
printf("Destination not found.\n");
else
printf("Destination Found \nDestination Name:%s", travel_name[c]);
int Search_type( char type, char leg_type[6][10], int travel_int[6][15], int trip_num)
{
int i, n;
char target;
getchar();
printf("Please enter travel type you wish to search by:");
scanf("%c", &target);
for (i =0; i <trip_num; i++)
{
for ( n = 0; n < travel_int[i][NUMLEG]; n++)
{
if( leg_type[i][n] == target)
{
return i;
}
}
return -1;
}
}
This is a rip out of a function I'm currently working on from a project and for some strange reason I can't seem to get it to search properly, I know that its not the rest of the code but this area since all the other function works properly. So I decided to break this out of the code and test it.
I can't seem to get it to search after 1 row:
travel_int[i][NUMLEG] contains the number of legs leg_type is where i store the char of each leg
I want it to search through the leg_type till it finds the specified character. Without pointers.
EDIT
For example Trip#:1 Destination Name:Austin Leg(#1):A Leg(#2):T
Trip#:2 Destination Name:Florida Leg(#1):S Leg(#2):F
so
trip number = 2
travel_int stores 2 trips and each trip has 2 legs
leg type stores in the 2 leg types at the trip number
When I put in A or T it prints out Austin like it should but when I input S or F it return destination not found =/. Am I doing the search wrong, or is it the print statement that is wrong. Or is it where I'm place my return -1;. (I would put up the code but its a bit long..)
Upvotes: 1
Views: 3492
Reputation: 6695
In your nested loops you are returning -1 without doing complete search in the entire arrays.
Place return -1 outside all the for() loops.
for (i =0; i <trip_num; i++)
{
for ( n = 0; n < travel_int[i][NUMLEG]; n++)
{
if( leg_type[i][n] == target)
{
return i;
break;
}
}
} return -1
This would definitely make your program run correctly.And this is the only reason why you get the correct results when you search for trip #01.
Upvotes: 1