The Logic

This is a discussion on The Logic within the C++ Programming forums, part of the General Programming Boards category; Good eve: a function searches a string for a substring and returns the sub triggering an event, simply: Code: #include ...

  1. #1
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44

    The Logic

    Good eve:
    a function searches a string for a substring and returns the sub triggering an event, simply:

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    #include <string.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    char resp1[]="good"; 
    char name[256];
    
    
    cout<< "Hello, how are you today?\n"<< endl;                     
    cin.getline (name,256);
    
    char * pch;
    pch=strstr (name, "good"); //Searching for “good” in  ^ Char str
    
    
    if(pch !=NULL) 
    
    cout<< "I am glad to hear you are doing well today."<< endl;                      
    else
    cout<< "Is anything wrong at all? Would you like to discuss it? \n"<< endl;                      
    
    Sleep(50000);
    
    return 0;  //END
    
        }
    I would appreciate some hints (only hints, please) to nudge me in the proper direction to: search said string for not only one word(ex:"dog") but two words (ex:"dog", "cat") I have tried a few methods such as placing additional, uniquely-named var arrays and setting "pch" accordingly (NOTE: the above code ^ does not reflect these stated adjustments-- it failed I believe due to that fact that NULL was returned (?)
    Any help or encouragement would be greatly encouraged-- you guys are just great
    Cheers, reRanger
    "Nay! But you love the present life!"

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, using this method, you will have to perform the search multiple times (once for each word). From there, you can use logical operators to specify conditions if both, at least one, exactly one, or none of the searches evaluated to true (look into bools). A significantly more advanced technique (in C++, though not in some other languages such as Perl) would be to use regular expressions for which you would need a specialized class/library to work with (I believe Boost -- www.boost.org -- has a reg exp library if you are really interested). Anyway, I don't recommend the second solution right now, I just wanted to let you know it is out there (and if you ever program in Perl, you will use regular expressions).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 12:05 PM
  2. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21