I have only just found this board, and I've been impressed with what I have experienced. I hope people don't mind my questions too much.

My problem is that I want to search an array for a string. The aim is to have the user type in an array and string and have the program search the array specified for the string specified, then return what row it is in.

The user enters "office" (as the array to search) and "Chalk" as the string to find. The progam then displays that it was found in row 1 etc.
============================================
The following is what I have, I'm aware that it is not really close to what it should be.

any help/pointer would be appreciated.
============================================

and as such the y variable becomes unneccessary for this purpose
and the array parameter in lookfor is never used so it is eliminated from the function...
also a for loop could be better utilized here but i will leave it with the while to prevent more confusion...

is it possible to have office use the array variable? I've tried, but it gives me a "error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast".

As you probably guessed I'm kind of new to C++

08-29-2001

guest

If you have the line:

#include <string.h>

at the top of the file you don't need to have the line

int strcmp( const char *string1, const char *string2 );

in your program as it is there automatically by virtue of the process set in motion with the include line above. Therefore delete the above line. If that doesn't abort the error then you may need to change the process of filling the array of strings. but I think the first suggestion will work.

You can return the value of x from LookFor() if the string is found and return the value -1 if it isn't. Remember that x represents the index of the string in the array. Index 0 is the first element of the array and index 1 is the second element of the array. Therefore, you may need to do some adjusting depending on how you wish to use the information.

08-29-2001

no-one

guest is right on the first part...
#include <string.h>
instead of prototyping the function sorry for the confusion.

1. Array is a char not a char*
2. Array is supposed to be a char and your not using the adress operator to pass the adress of the char.
3. or your adressing a specific character in the array such as with Array[0] or such...

whats the code this happens on?

>is it possible to have office use the array variable?

that depend on what you want to do with it...
how do you want it used?

08-29-2001

guest

the variable called array is superfluous if you wish to search the array of strings called office for a user entered string. Here's a modified version of the code that should work.

why not just use strstr(). This finds a string within a string.... its in string.h or cstring

08-31-2001

zen

Quote:

What I want, as shown in the following code, is for the user to be able to enter BOTH the array to seach AND the string to search for.

You could create an array of structs -

struct x
{
char array[x][y];
char arrayname[z];
}arrays[noOfArrays];

Then when the user enters the array name to search you can loop through the array of x's until you find a match with arrayname and then search for the string in the actual array.

08-31-2001

zen

..or for another alternative you could look up STL map.

08-31-2001

no-one

so what your saying is look for "string" in this "array"?

so that it will find the array(office for example) to search for the arguement string?

08-31-2001

Zaarin

Yes that's right no-one. I want the array name to be a variable, so that the user can enter which array to search.

08-31-2001

no-one

ok got ya... i'll be back with a good way ASAP... i unfortunatly
have a spliting headache now and i think my eyes are gonna pop out so i can't write good code when im like this i can't even see straight. so i'll probably have it tommorow... sorry.

09-03-2001

no-one

sorry this took so long ive been real busy...

the absolute best way you can do this is with an
STL map nothing you will find will accomplish this better so...

but i must apologize again because i don't know enough about the STL map to explain it to you but if you make a post about using the STL map im sure the better versed C++ programmers could help you understand it better than i could.