strstr: code & errors

This is a discussion on strstr: code & errors within the C++ Programming forums, part of the General Programming Boards category; (compiled on Dev-C++) Code: #include <stdio.h> #include <fstream> #include <iostream> #include <string.h> #include <windows.h> using namespace std; int main() { ...

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

    strstr: code & errors

    (compiled on Dev-C++)

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    #include <string.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    char str[]="good"; //Variable
    char input1;
    
    {
    cout<< "Hello, how are you today?\n"<< endl;                     
    getline(cin);
    
    char * pch;
    pch=strstr (input1, 'good'); //Searching for “good” in  ^ Char str
    
    
    if(char str[] !=null) // or perhaps “zero” instead of “null”? or just “str” instead of whole tag…
    
    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
    
        }


    errors:

    18 no matching function for call to `getline(std::istream&)'

    21 [Warning] multi-character character constant

    21 call of overloaded `strstr(char&, int)' is ambiguous

    56 error candidates are: char* strstr(const char*, const char*) <near match>

    125 char* std::strstr(char*, const char*) <near match>

    24 syntax error before `!=' token

    TY reRanger
    "Nay! But you love the present life!"

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Why oh why didn't you just reply to your other thread?

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    char str[]="good"; //Variable
    char input1;  // So the user can only input 1 character? 
    
    {//Why is this here? 
    cout<< "Hello, how are you today?\n"<< endl;                     
    getline(cin); /*So you are going to get it from the cin and put it where? */ 
    
    char * pch;
    pch=strstr (input1, 'good'); //Searching for “good” in  ^ Char str
    
    //expression should be: if (pch != NULL )
    if(char str[] !=null) // or perhaps “zero” instead of “null”? or just “str” instead of whole tag…
    
    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;

  4. #4
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44
    Thank-you, Thantos, for your quick and scholarly reply. I appreciate it and will look into your suggestions immediately.
    reRanger
    "Nay! But you love the present life!"

  5. #5
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44
    Thantos: I do not understand:
    Code:
       char str[]="good"; //Variable
    char input1;  // So the user can only input 1 character?
    "input1" is just the given working name of this variable; I meant it to be the place where the trigger word ('good') would be stored. Please explain

    reRanger
    "Nay! But you love the present life!"

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    the way you have input1 declared it can only store 1 character. If you need to store more then one character you need to have an array of characters.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    reRanger, some of your code uses functions meant for the C++ string class in the header <string>. Other parts of the code use C-style strings, which are just null terminated character arrays. Pick one and stick to it so we can help you figure out how to fix your code.

    I'd recommend the C++ string class, but if you have an instructor that is teaching you C-style strings then you might have to do it that way. Let us know which you'd prefer using.

    PS. Change “ to " not '

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

    Exclamation C++ only

    HI:
    I was not even aware that I was mixing C and C++ Okay: I prefer C++ only and always. Although my instructor teaches mostly in C, I work in C++. (This is not a homework assignment, its self work, a snippet of a large project; if I depended on learning everything I needed to know here, I would never reach my goals I fear) Anyway, can you expalin to me my errors? A few others on here have been kind enough to mention their ideas and I have implemnted them (unfortunately, the comp lab I run at the school hates my Dev-C++ compiler, so I am coding blind until I get home.

    Thanks in advance, reRanger
    "Nay! But you love the present life!"

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    C style strings are valid in C++ and are used often, but since you are doing this on your own I think it is good that you learn the C++ string class. Here is a simple program that uses that class.
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string input;
        std::getline(std::cin, input);
    
        if (input == "hello")
            std::cout << "You said hello." << std::endl;
        else
            std::cout << "You didn't say hello." << std::endl;
    
        if (input.find("ell") != input.end())
            std::cout << "The word ell was in your input." << std::endl;
        else
            std::cout << "The word ell was NOT in your input." << std::endl;
    }
    It is very difficult to code without a compiler in front of you. It is recommended that you first figure out the logical flow of the program (no computer or compiler needed). Then write the code for small parts of the program at a time. After each little bit you write, compile it and fix any compiler errors. If you can, test the incomplete code as you go, so that you know that what you have already written is working.

    If you are trying to code without a compiler in front of you, you end up with a large program with tons of errors in it and it is harder to fix your problems. So I'd recommend finding out how to work your computer lab's compiler or just write out the logical flow of your program and wait until you get home to code it.

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

    Ty

    jLou:
    Thank-you so much; and I am glad, no, impressed that you "think it is good that you learn the C++ string class. " I mean, most people do not even give a !*@$ and are generally and genuinely out for themselves only. I am new to coding but I already have a great passion for it. My problem is,though, I try to absorb so much that I miss things, confuse, and generally complicate relatively simple matters But, hey, I appreciate your help, and I'm sure I will need more in the future.
    reRanger
    "Nay! But you love the present life!"

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

    var

    std::string input;std::getline(std::cin, input); are considered variables in above code?
    reRanger
    "Nay! But you love the present life!"

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    string is a type (like int or char).

    input is the name of a variable that has a type string.

    getline is a function that gets a line from an input stream and puts it into a string.

    cin is the console input stream.

    So the first line declares a string variable named input. The second line reads from the console a line of text and stores that line of text in the variable named input.

    You can pretty much ignore the std:: part. All the names of functions, types, variables, etc, used in the standard library have a namespace so there are no naming conflicts. You don't really need to understand that now. You can put using namesapce std; like you did in your original code instead.

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

    Wink Tyty

    jlou thank-you. I feeel like I get some of this a bit better now. I cannot wait to get home to my compiler. But before compiling, I think a rewrite of sorts is in order, just to start fresh and clean. Going to do that now...
    reRanger
    "Nay! But you love the present life!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  4. RAM/Swap Memory Code Errors
    By ghe1 in forum Linux Programming
    Replies: 2
    Last Post: 04-01-2002, 06:37 AM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

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