Jul 14, 2013 at 1:23am UTC
jjnkln
Last edited on Jul 14, 2013 at 4:28am UTC
Jul 14, 2013 at 2:44am UTC
First of all you must to include header <string> instead of <cstring>.
The search can be done for example the following way
1 2 3 4 5
string *p = array;
while ( p != array + LINES && str != p->substr( 0, str.size() ) ) ++p;
if ( p != array + LINES ) cout << "The target record is " << *p << endl;
Last edited on Jul 14, 2013 at 2:48am UTC
Jul 14, 2013 at 3:02am UTC
ajipvkaj eu fzzzt
Last edited on Jul 14, 2013 at 5:26am UTC
Jul 14, 2013 at 3:19am UTC
Vlad thank you your code worked perfectly. Yet, how do i make it accept a typo so like Vlak would still display the contents of Vlad?
Last edited on Jul 14, 2013 at 3:23am UTC
Jul 14, 2013 at 3:27am UTC
You were shown a code with a typo. Moreover it is incorrect code because it does not search strings that start with the target string. For example let assume that you entered string "Lo". There is no such a record in the array that starts with "Lo". However the proposed code points out the first record.
As for the typo I mentioned then you are using variable n that was not initialized
if (array[n ].find( str ) != string::npos)
Last edited on Jul 14, 2013 at 3:31am UTC
Jul 14, 2013 at 3:30am UTC
The problem is that it is difficult to distinguish whether a typo was done or it is a valid string.
For example let assume that "Joa" was entered If you have records "Joe..." and "Joan..." how to understand what record the user want to find?
Last edited on Jul 14, 2013 at 3:36am UTC
Jul 14, 2013 at 3:42am UTC
There is an idea to use a "distance" between strings and find the record with the least distance.
Last edited on Jul 14, 2013 at 3:43am UTC
Jul 14, 2013 at 4:14am UTC
do you know how to do that?? i have no idea how to write this for a typo
Jul 14, 2013 at 10:23am UTC
As I said you can search a record that is the nearest to the target record. However in this case the result can contain several records.