how to recognize two words entered by the user for a string

Hello,
In my little experiment i am trying to get the compiler to recognize 1 word with 2 parts from a list of names.
for example: if the user wanted to choose "candy bar", from a list of items which include: "candy bar", "cat", "dog"
my current code can recognize words without a space like cat and dog. But how can i recognize candy bar?
I tried using getline but its of no use.
Any help is appretiated. Thanks.
getline(cin, str)

or cin >> str

or

stringstream mystr(str)

E: Just thought about it again. So you have a library of words and you are trying to get as many matches to any input the user enters. So say the user enters:

rain, bow

And your library contains the words:

string lib [] = {"cat", "dog", "rainbow", "rain", "bow"};, it should match rain, bow, and rainbow


string temp;

string word1;
string word2;

for(...)
{
if lib[j] == word1 || lib[j] == word2
...
}

temp = word1 + " " + word2;

for(...)
{
if lib[j] == temp
...
}

Something like that?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <string>

int main()
{
    const unsigned nItems = 5 ;
    std::string items[nItems] =
    {
        "candy bar",
        "cat",
        "dog",
        "rail gun",
        "quit"
    };

    bool notDone = true ;

    while ( notDone )
    {
        std::cout << "Type your selection:\n" ;

        for ( unsigned i=0; i<nItems; ++i )
            std::cout << items[i] << '\n' ;

        std::string input ;
        std::getline(std::cin, input) ;

        if ( input == "quit" )
            notDone = false ;

        bool foundInput = false ;
        for ( unsigned i=0; i<nItems; ++i )
            if ( input == items[i] )
                foundInput = true ;

        std::cout << '"' << input << "\" is " ;
        if ( foundInput )
            std::cout << "a match!\n" ;
        else
            std::cout << "not a match!\n" ;
    }
}

Topic archived. No new replies allowed.