Thread: Substring hunt and gather

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

    Substring hunt and gather

    A preview of some code I would like a response upon if possible: The mission: locate a word (substring) in user-input string and then have the ability to, in a sense, I guess, turn it into a function, triggering an event. Yes, it is simple, but I've yet to reach the point of it fully working as I picture it should. I determined that I could just use an event, in this case the return of null (or actually, a "!=null") to stage the next portion of the code. I am not looking for a complete fix (and please understand, I am at school working in a comp. lab where my Dev++ compiler will not run, so I am coding blind, and the code is set rather sloppy still-- no proper formatting) Just let me know what you think or where I could use some corrections. Is this substring function even doable as I have planned?
    Cheers, reRanger

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    #include <string.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    Char str[ ]= “good”; //Variable
    String input1[ ]
    
    std::cout<< “Hello, how are you today?\n”<< endl;                     
    getline(cin, input1, ‘\n’);
    
    Char * pch;
    pch=strstr (str, “good”); //Searching for “good” in  ^ Char str
    
    if
    (char str[ ] !=null) // or perhaps “zero” instead of “null”? or just “str” instead of whole tag…
    
    std::cout<< “I am glad to hear you are doing well today.\n”<< endl;                      
    else
    std::cout<< “Is anything wrong at all? Would you like to discuss it? \n”<< endl;                      
    
    
    return 0;  //END
    
        }
    "Nay! But you love the present life!"

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Ok, first, I'm pretty sure Char needs to be char. Second, you are gathering input into a string called input1 (I'm guessing you don't need the [] when declaring, you aren't making an array of strings yet).

    When you check for "good", you are checking an array of characters that hasn't changed since you declared it. Try using strstr() to check input1, where the text that the user entered goes.

    I haven't done any programming with strings yet, you may need alot more help from someone who knows about it.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

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

    Thumbs up Ty

    Danke, neandrake...
    reRanger
    "Nay! But you love the present life!"

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Also, when you have
    Code:
    using namespace std;
    You don't need to do this:
    Code:
    std::cout <<
    You can just do this
    Code:
    cout <<
    That's the basic purpose of the using namespace.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44
    sweet, thanx
    reRanger
    "Nay! But you love the present life!"

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    bitte schon ;-)
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed