Hi,

I would like to write a function that checks the user input to see if it matches with the allowable inputs. The allowable inputs are stored in a separate text file.

User-entered input:
This is the user inpt

Contents of text file:
1. Hello
2. This is the user input
3. Hi
4. How are you

How do I match the user-entered input to item #2 in the text file and also detect that the difference between the user-entered input and item #2 is that the user-entered input is missing the letter 'u' in the word "input"?

Help pls. Thanks.

do you know how to open a file and read each line? hint: ifstream and getline()

Yeah that part I can do. But my search doesn't produce any results as the string entered by the user is different from the one stored in the file.

User-entered input: This is the user inpt
String stored in file: This is the user input

How do match the two inputs and also show where the error/difference is at?

sounds like you need to write your own version of the strcmp() function, where it returns the index at which strings vary rather than just the indicator of which ordering they have.

You'll have to examine the stings char by char till a difference exists or you reach the NULLs.

you might be able to use boost regex library. But don't ask me how to use it because I don't know.

I though about vmane's idea -- but you will need something much more complex than that. Simple comparison character by character might produce false matches

User-entered input: This is the user inpt
String stored in file: This and that

> How do I match the user-entered input to item #2 in the text file
the closest matching string in the text file is the one with the smallest Levenshtein distance (edit distance) from the user-entered input.
http://en.wikipedia.org/wiki/Levenshtein_distance

> and also detect that the difference between the user-entered input and item #2 is that
> the user-entered input is missing the letter 'u' in the word "input"?
use the information in the matrix used for computation of the Levenshtein distance
http://www-igm.univ-mlv.fr/~lecroq/seqcomp/node2.html

#include <algorithm>
#include <string>
#include <vector>
#include <iostream>

std::size_t levenshtein_distance( const std::string& s,
                                  const std::string& t )
{
  if( s.empty() ) return t.size() ;
  if( t.empty() ) return s.size() ;

  using std::size_t ;
  size_t n = s.size() ;
  size_t m = t.size() ;
  using std::vector ;
  vector< vector<size_t> > d( n+1, vector<size_t>(m+1) ) ;

  for( size_t i = 0; i <= n; ++i ) d[i][0] = i ;
  for( size_t j = 0; j <= m; ++j ) d[0][j] = j ;

  for( size_t i = 1 ; i <= n ; ++i )
  {
      for ( size_t j = 1; j <= m; j++)
      {
        int cost = s[ i - 1 ] == t[ j - 1 ] ? 0 : 1 ;
        d[i][j] = std::min( d[i-1][j] + 1,
                       std::min( d[i][j-1] + 1, d[i-1][j-1] + cost ) ) ;
      }
   }
   // TODO: minimum cost path(s) from d[0][0] to d[n][m]
   // gives the required alignment(s) between the two strings
   // a character by character comparison of the aligned
   // strings gives the difference between the two strings
   return d[n][m] ;
}

int main()
{
  std::cout << levenshtein_distance( "kitten", "sitting" ) << '\n' ;
  std::cout << levenshtein_distance( "saturday", "sunday" ) << '\n' ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.