How to check if A==B or C or D or E...

This is a discussion on How to check if A==B or C or D or E... within the C++ Programming forums, part of the General Programming Boards category; Hello, I'd first like to introduce myself to these forums as I will most likely spend lots of time here ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13

    How to check if A==B or C or D or E...

    Hello, I'd first like to introduce myself to these forums as I will most likely spend lots of time here in the future. I'm Bruno Gavranovic, aka bgavran3, I'm 14 years old and I'm pretty familiar with modding, I'm working as a level designer on 2 game modifications but I'd like to move on to more complex things, such as coding, since mapping is quite easy, anyone can do that.

    So, I've started learning C++, I pretty much learnt the basics and now I started working on some a bit more complex project.

    What I want is to get input from the user and if the input is equal to one of the words, the program responds with a text, if its not, it responds with another text.
    So the user, for example types in "hi" and the program checks if the text he typed is equal to words "hey" or "hello" or "hi". If it is, program greets him with "Hello", if the text he typed isn't equal to one of those, program responds with "What?"

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    
    int main()
    {
        char input[15];
        char answer[15];
        struct database {
            char greets;
        }
    
        database beg;
        beg.greets = "hi" && "hey";
    
        cin.getline (input, 15, '\n');
        answer[0]='\0';
        strcat (answer, beg.greets);
        if (strcmp (input, answer) == 0)
        cout<<"Hello";
        else
        cout<<"What?";
        cin.get();
    }
    But the code isn't working for some reason or another, here are list of errors CodeBlocks compiler shows to me:
    Code:
    error: 'database' does not name a type
    error: 'beg' was not declared in this scope
    I know its doable and easier using strings, by comparing string A (hi) with string B (hey), if they're not equal then comparing string A (hi) with string C (hello) and then print out the response based on this but at some point in the program development I will need to have lots of words input needs to be compared with and I want to find a way in which I'll just have to add one more word to the list to make it work.
    I'm using structures at the moment but I'm not sure are they the best way... if not, can someone point me in the right direction?

    And one more question, what is the best way to store unlimited amount of words? What I need is to get the input from the player, display an text, get the input from the player, display an text, get the input...
    And each time I want to be able to check what exactly input is, what the user has typed.

    Thanks
    Last edited by bgavran3; 07-06-2008 at 05:46 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    The error reported by the compiler is here:
    Code:
    struct database {
            char greets;
        }
    
        database beg;
    The two fixes are:
    Code:
    struct database {
            char greets;
        };
    
        database beg;
    Code:
    struct database {
            char greets;
        } beg;
    The former is probably what you wanted, although typically a structure definition is put outside a function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    You may want vectors, strings and loops:
    Code:
    std::vector<std::string> vCompare1, vCompare2;
    vCompare1.push_back("some string");
    //...
    bool bFound = false;
    for (int i = 0; i < vCompare1.size(); i++)
        for (int j = 0; j < vCompare2.size(); j++)
            if (vCompare1.at(i) == vCompare2.at(j))
            {
                bFound = true;
                break;
            }
    Since you are using C++, there's no need to do it the C-way!
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    Actually, it might be better to store the list of words in a std::set since you may have quite a few of them compared to what the user would type as input. Furthermore, you would then just use the std::set's find() member function, and thus abstract away the inner loop (which you could have done with the std::find() generic algorithm too, but more on that later).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    Quote Originally Posted by Elysia View Post
    You may want vectors, strings and loops:
    Code:
    std::vector<std::string> vCompare1, vCompare2;
    vCompare1.push_back("some string");
    //...
    bool bFound = false;
    for (int i = 0; i < vCompare1.size(); i++)
        for (int j = 0; j < vCompare2.size(); j++)
            if (vCompare1.at(i) == vCompare2.at(j))
            {
                bFound = true;
                break;
            }
    Since you are using C++, there's no need to do it the C-way!
    Thanks, but I dont really understand that code, I've skipped like 20 tutorials, I went to the vector one but I still dont understand this part:
    Code:
    bool bFound = false;
    for (int i = 0; i < vCompare1.size(); i++)
        for (int j = 0; j < vCompare2.size(); j++)
            if (vCompare1.at(i) == vCompare2.at(j))
            {
                bFound = true;
                break;
    "Something" is set to false, "i" will increase untill its equal to the size of the first vector?
    After that "j" will increase untill its equal to the size of the second vector? If "i" and "j" are equal then "something" is set to true?
    Can someone explain it to me in detail?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    The two fixes are:
    Code:
    struct database {
            char greets;
        };
    
        database beg;
    i think you missing the keyword 'struct'...

    struct database {
    char greets;
    };

    struct database beg;
    unless using typedef
    typedef struct database {
    char greets;
    }DB;

    DB beg;
    Last edited by csonx_p; 07-07-2008 at 05:56 AM.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    I've skipped like 20 tutorials
    This strikes me as a poor idea if you're trying to learn.

    Regarding the loops, perhaps this will be more clear:
    Code:
    bool bFound = false;
    for (int i = 0; i < vCompare1.size(); i++)
    {
        for (int j = 0; j < vCompare2.size(); j++)
        {
            if (vCompare1.at(i) == vCompare2.at(j))
            {
                bFound = true;
                break;
            }
        }
    }

  8. #8
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    Quote Originally Posted by csonx_p View Post
    i think you missing the keyword 'struct'...



    unless using typedef
    Thanks that worked but now I'm getting those errors:
    Code:
    error: invalid conversion from 'char' to 'const char*'
    error: initalizing argument 2 of 'char* strcat(char*, const char*)'
    And can anyone explain the code from my previous post to me?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    i think you missing the keyword 'struct'...
    Nope. This is C++, so
    Code:
    struct x { ... };
    and
    Code:
    typedef struct x { } x;
    are [aside from compiler internals] the same thing. It is optional to use struct in C++, whilst if you don't do typedef in C, you do need to supply struct before the struct-name.

    --
    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.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    Quote Originally Posted by rags_to_riches View Post
    This strikes me as a poor idea if you're trying to learn.
    No, I meant, in this tutorial section: http://www.cprogramming.com/tutorial/
    Tutorial by tutorial, I've got to the File I/O part, but to understand vectors I had to go all the way down, to the Standard Template Libary, I had to skip more than 20 tutorials to get to the vectors part.

    Oh and thanks for the loop explanation but I dont understand what bFound is and why would I need to loop something untill its equal to the size of the vector?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    Oh and thanks for the loop explanation but I dont understand what bFound is and why would I need to loop something untill its equal to the size of the vector?
    The idea is to loop over all the elements in the vector until a match is found. When a match is found bFound, a flag, is set to true, indicating that a match was found.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by bgavran3 View Post
    beg.greets = "hi" && "hey";
    Trying to understand the use & meaning of this line..? Guess am learning too...

  13. #13
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    Quote Originally Posted by laserlight View Post
    The idea is to loop over all the elements in the vector until a match is found. When a match is found bFound, a flag, is set to true, indicating that a match was found.
    Oh, I think I understand it now, thanks!
    Not the looping tho, but I'm going to give it some more thought, hopefully I will.

    Trying to understand the use & meaning of this line..? Guess am learning too...
    I want to check is the input equal to one of those words, "hi" and "hey"

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    Trying to understand the use & meaning of this line..? Guess am learning too...
    The use of operator&& is wrong in this case. What bgavran3 wants to do is check to see if the input string contains "hi" and "hey", but what that line does is evaluate "hi" && "hey". Since "hi" is a string literal, thus for the boolean evaluation it is converted to a pointer to the first character, which is not a null pointer, so it is evaluated as true. The same goes for "hey". Therefore, "hi" && "hey" is evaluated to true && true, which is evaluated to true. Now, this is assigned to beg.greets, which is a char. When the boolean value true is converted to a char, the value of 1 is used, thus beg.greets is assigned the value of 1. This is obviously not a step in the right direction to what bgavran3 wants to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Quote Originally Posted by bgavran3 View Post
    No, I meant, in this tutorial section: http://www.cprogramming.com/tutorial/
    Tutorial by tutorial, I've got to the File I/O part, but to understand vectors I had to go all the way down, to the Standard Template Libary, I had to skip more than 20 tutorials to get to the vectors part.
    Ah, I see. Given code such as this:
    Code:
    struct database {
            char greets;
        }
    
        database beg;
        beg.greets = "hi" && "hey";
    I would have to say that this
    I pretty much learnt the basics
    is not quite the case. Recognizing, of course, that it was not your initial intention to do so, jumping to vectors before you understand the basics of boolean expressions and loops is not appropriate.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

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