Thread: function breakdown

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

    Exclamation function breakdown

    Good evening:
    I have finally "fixed" (really, many people here helped, ) my code, and now it compiles error-free which is nice; Upon runtime, though, it is apparent that the primary goal of this snippet of code is not accomplished, that is, "return the substring ("good") and allow it to act as a trigger for a function, a Boolean result which spits out a cout." Very relieved no errors at this point, but little designed results. Just some hints, etc should anyone care to assist.

    Thank-you,
    reRanger



    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    #include <string.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    char str[]="good"; 
    char grabber[256];
    
    
    cout<< "Hello, how are you today?\n"<< endl;                     
    cin.getline(grabber,256);
    
    char * pch;
    pch=strstr (str, "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
    
        }
    "Nay! But you love the present life!"

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Look at your call to strstr again. What are you comparing to what?
    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.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    First of all you have to change..

    Code:
    pch = strstr(str, "good");
    by..

    Code:
    pch = strstr(grabber, "good");
    because str will always contain "good".. you are looking for it in grabber (the user's input..).

    then you got the great problem..

    strstr() will only search until a space is found in the string so..
    i'm doing good (doesn't work)
    i'mdoinggood (works!)

    so my advice would be..

    1) code your own strstr() function to fix this. (effective)
    2) code a string preprocessing function to erase spaces. (not so effective but should work in most cases).

    ROCKaMIC

    EDIT: sorry.. you can forget the advice, i did it because i recoded everything in C to save compilation time and i used scanf().. nevermind then :P
    Last edited by ROCKaMIC; 11-24-2004 at 10:13 AM.

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

    Thumbs up Ty

    ROCKaMIC: Thank-you for your advice
    "Nay! But you love the present life!"

  5. #5
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44
    ROCkaMIC: please check your private messages.
    reRanger

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Indeed, the problem was not with strstr(), but with the fact that you were only reading in until a space (which from your edit I presume you figured out).
    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. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM