Thread: Comparing input

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Comparing input

    Hey all, heres my deal. I need to take in user input, and compare it to pre-defined information. The user says something and the computer reacts after checking for appropriate match. Its a chatting program.I want to use a setup like this:

    Code:
    cout<<"Input something: ";
    cin<<input
    
    if (input != " ")
    {
    struct Check1
    {
    <predefined keyword>
    or
    <predefined keyword>
    or
    <predefined response>
    }
    
    if (Check1 == true)
    {
    cout<<predefined response<<"\n";
    }
    
    else:
    
    }
    }
    
    goto Check2

    Now i realize this isn't real code, but this is the idea i want, any ideas how i could do this?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    void DoSomething(char* String)
    {
       if(strcmp(String, "Keyword1") == 0)
       {
          //Do something
       }
       else if(strcmp(String, "Keyword2") == 0)
       {
          //Do something else
       }
       else if(strcmp(String, "Keyword3") == 0)
       {
          //Do something else
       }
       else 
       {
          //Do something else
       }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    will this work if the keyword is in a sentence rather then the only imput?

    Example:

    Hello i am "keyword here" today.

    as opposed to

    keyword

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Magos is telling you that you cannot compare inputted strings with the == operator... you need to use strcmp()

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes, and as far as substrings, well, you are going to have to split them up yourself! The function might have this signature:

    Code:
    /*...returns false at end of string...*/
    bool GetNextWord( char sentence[], int * next_position, char recieve_buff); 
    
    int main() {
    char sentence[] = "This string will get parsed";
    char parsed[] = "parsed";
    char word[100];
    int next = 0;
    
      while( GetNextWord( sentence, &next, word ) ) {
        printf("\n%s", word);
          if( strcmp(word, parsed) == 0) {
           printf(" matches %s.", parsed); 
        getch();
       }
    
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Or you could use std::string:

    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
    
    	string s;
    
    	cout<<"Enter a string: ";
    	cin>>s;
    
    	string::size_type pos = s.find(string("keyword"));
    
    	if ( pos != string::npos )
    	{
    		cout<<"Found keyword at "<<pos<<endl;
    	}
    
    	return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    thnx if i think i like that one.

    edit:// Errors suck:

    PHP Code:
    --------------------Configurationtest Win32 Debug--------------------
    Compiling...
    test.cpp
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(7) : error C2065'string' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(7) : error C2146syntax error missing ';' before identifier 's'
    C:\Documents and Settings\NEKEY\Desktop\test.cpp(7) : error C2065's' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(9) : error C2065'cout' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(9) : error C2297'<<' illegalright operand has type 'char [17]'
    C:\Documents and Settings\NEKEY\Desktop\test.cpp(10) : error C2065'cin' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(10) : warning C4552'>>' operator has no effectexpected operator with side-effect
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(12) : error C2653'string' is not a class or namespace name
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(12) : error C2065'size_type' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(12) : error C2146syntax error missing ';' before identifier 'pos'
    C:\Documents and Settings\NEKEY\Desktop\test.cpp(12) : error C2065'pos' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(12) : error C2228left of '.find' must have class/struct/union type
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(14) : error C2653'string' is not a class or namespace name
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(14) : error C2065'npos' undeclared identifier
    C
    :\Documents and Settings\NEKEY\Desktop\test.cpp(16) : error C2297'<<' illegalright operand has type 'char [18]'
    C:\Documents and Settings\NEKEY\Desktop\test.cpp(16) : error C2065'endl' undeclared identifier
    Error executing cl
    .exe.

    test.obj 15 error(s), 1 warning(s
    Last edited by RoD; 10-20-2002 at 05:58 PM.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    did you forget:

    #include <string>
    #include <iostream>

    ?

    Also, that solution won't work, because it assumes that the keywords are already separated. However, you could use the string::find_first_not_of() function, but you'll have to ask an STL'er about that one...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Ummm... I am not quite sure about this one, but i have heard say about a strtok() function tht tokenizes strings... try the msdn...
    Hope this helps:
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  10. #10
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    did you forget:

    #include <string>
    #include <iostream>
    No. I added .h to the end of them and took it down to ten errors:

    PHP Code:
    --------------------Configurationtest Win32 Debug--------------------
    Compiling...
    test.cpp
    c
    :\documents and settings\nekey\desktop\test.cpp(7) : error C2065'string' undeclared identifier
    c
    :\documents and settings\nekey\desktop\test.cpp(7) : error C2146syntax error missing ';' before identifier 's'
    c:\documents and settings\nekey\desktop\test.cpp(7) : error C2065's' undeclared identifier
    c
    :\documents and settings\nekey\desktop\test.cpp(12) : error C2653'string' is not a class or namespace name
    c
    :\documents and settings\nekey\desktop\test.cpp(12) : error C2065'size_type' undeclared identifier
    c
    :\documents and settings\nekey\desktop\test.cpp(12) : error C2146syntax error missing ';' before identifier 'pos'
    c:\documents and settings\nekey\desktop\test.cpp(12) : error C2065'pos' undeclared identifier
    c
    :\documents and settings\nekey\desktop\test.cpp(12) : error C2228left of '.find' must have class/struct/union type
    c
    :\documents and settings\nekey\desktop\test.cpp(14) : error C2653'string' is not a class or namespace name
    c
    :\documents and settings\nekey\desktop\test.cpp(14) : error C2065'npos' undeclared identifier
    Error executing cl
    .exe.

    test.obj 10 error(s), 0 warning(s

  11. #11
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Hi,
    I don't think that if you are making a chat program your only problem will be tokenizing or comparing the text( I mean doing the coding), because it's not complicated, but I think that the problem is in how to deal with the entered keywords, and respond to them, because in order to make a good chatting program it will be VERY hard.

  12. #12
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    This one is a sub-par version for a buddies english project (he was given permission to submit a program not made by him in conjunction with his paper).

    Hes going to give me a list of questions and responses, i just have to figure out how its going to pick out certain words, example:

    Question: Can u pick up that paper?

    picks out: pick up

    says: I would but i have no arms

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. HELP! Reading from txt file and comparing to input
    By disco_dog in forum C Programming
    Replies: 7
    Last Post: 12-05-2004, 06:30 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM