how to search for a word match in a string array

so here's my code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string firstarray[] = {"one", "two", "three"};
string secondarray[] = {"first number", "second number", "third number"};
int x=0;
char iData[256];
loop:
system("cls");
cout<<"Enter word to be searched for: ";
cin.getline(iData,256);
goto processData;

processData:
if (firstarray[x]==iData)
{
cout<<secondarray[x]<<endl;
system("pause");
goto loop;
}
else
{
x++;
goto processData;
}

return 0;
}

...

it is some kind of array-based dictionary program... it's working.
my only problem is that when the user enters a word that isn't in the first array, the program crashes. I have no idea on what to do about it... my mind is totally blank. please help!

if you're going to post a solution, please just post the revised version of my code.

thanks!

P.S. another topic i really need help on is on how to check if the program is testing the last value in an array... thanks again!!!
First you need to some reading regarding loops to get rid of these goto abominations. Keywords to search for: for, while, do-while.

Second, read strings as following:
1
2
string iData;
getline(cin,iData);


Your problem might very well vanish into thin air after applying these fixes.
Loop is implemented by GOTO!!! :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<string>
using namespace std;

main(){
       string firstarray[] = {"one", "two", "three"};
       string secondarray[] = {"first number", "second number", "third number"};
       string dataSearch;
       char choice;
       do{
            system("cls");
            bool found=false;
            cout<<"Enter word to be searched for: ";
            cin>>dataSearch;
            for(int x=0; x<sizeof(firstarray)/sizeof(string);x++){
                    if(dataSearch==firstarray[x]){
                          cout<<secondarray[x];
                          found=true;
                          break;                        
                    }
            }
            if(found==false)
                cout<<"word not found..."<<endl;
            cout<<endl<<"Do you want to continue searching[y/n]: ";
            cin>>choice;
       }while(tolower(choice)!='n');
       return 0;  
}
Topic archived. No new replies allowed.