Thread: how to make a if a char equals something

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Unhappy how to make a if a char equals something

    Sorry for the confusing title but here's what I want to do:

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    
    {
        char search[50];
        cout<<"Welcome to Google! What are you looking for?\n\n";
        cin>>search;
        if (search == test ) {
                   cout<<"Good";
                   cin.ignore();
        }
        if (search != test) {
        cin.ignore();
        cout<<"\nSorry! " <<search <<" wasn't found!\n\n";
        system("pause");
    }
    }
    Why doesn't this work?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    How doesn't it work?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    when I compile it in DEV-C++ it gives me an error

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What is the error?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    it says that "`test' undeclared (first use this function)" on the tenth line. and then " (Each undeclared identifier is reported only once for each function it appears in.) "

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    `test' undeclared
    What do you think that means?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    I just don't get it anymore can't you just show me what do to?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What part of "`test' undeclared" do you not get?

    Did you declare a variable called test?

    Hint: you should.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Uh, nevermind, I have another idea

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, I think cyberfish is both right and wrong - you probably do not want to declare a variable called test, you probably want to have a constant string that you compare with your input. Use "" to surround constant strings, just like when you do output.

    And by the way, when you do "if (x == y) ..." followed by "if (x != y)", you should really replace the second if with "else" - since the first statement and the second are always true in the opposite case [1].

    [1] Of course, if you are actually modifying x or y inside the first if, then that's not strictly true. Do not worry about this bit right now, I'm just preventing someone else from side-tracking the subject.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah good call. I actually didn't read the code. Just trying to get him to do some troubleshooting himself, since he showed no effort at all.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    right under main (){
    put:
    char test;

  13. #13
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Quote Originally Posted by walkonwater View Post
    right under main (){
    put:
    char test;
    That'll get rid of the error, but not the problem.
    Any help I give may be classified as:
    The Blind leading the Blind...
    Currently working through:
    "C++ Primer Plus"

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    C Strings - C Tutorial - Cprogramming.com Here you can find information about strings, how to compare one string to another and so on.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Shakti View Post
    C Strings - C Tutorial - Cprogramming.com Here you can find information about strings, how to compare one string to another and so on.
    Oh no no no no.
    This is C++, so C-style strings we should avoid. Here is a better tutorial, methinks:
    Cprogramming.com - C++ Standard Library - String Class
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM