Searching arrays for strings
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.
eg. an array called office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{ "Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glu e"},{"Pencil"}};
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.
============================================
#include <iostream.h>
#include <ctype.h>
//declare variables
//=================
//misc. variables
int position = 0;
char array[16] = "";
char string[16] = "";
//loop controls
int x = 0;
int y = 0;
//the array
char office[11][16] = {{"Pen"},{"Chalk"},{"Biro"},{"Crayon"},{"Ruler"},{ "Eraser"},{"Stapler"},{"Paper"},{"Whiteout"},{"Glu e"},{"Pencil"}};
//function prototypes
//===================
int LookFor(char, char);
//functions
//=========
void main()
{
cout << "Enter the name of the array you wish to search: ";
cin.getline(array, 16, '\n');
cout << endl << "Enter the string you wish to find: ";
cin.getline(string, 16, '\n');
LookFor(array[16], string[16]); //call function
}
int LookFor(char array, char string)
{
while (y < 11)
{
while (x < 16)
{
if (office[x][y] == string)
{
position = x;
cout << "Found string: " << string << " in" << " Row: " << position << endl;
return 1;
}
else
{
}
x = x++;
}
y = y++;
x = 0;
}
cout << "If no data is listed above, then no match was found." << endl << endl;
return 0;
}