CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2017
    Posts
    3

    Question Compare 2 std::string in c++ virtual studio 2015

    Hey, I'm trying to compare a std::string with a string out of an array. Somehow it doesn't work.

    The User types in a word in the main method and it gets delivered to my search method as keyword.

    in the search function i have my stringarray with words. If my keyword matches with one of the words in the array, the method shall return true; else false.

    Code:
    bool serach(string keyword){
                    bool found = false;
    		int index;
    
    	//Archive//
    		string words[] = { "msg", "chrome","steam","spotify","discord" };
    	
    
    	//-----// Searching for Input //-----//
    		for (int i = 0; i < sizeof(words); i++) {
    
    			if (words[i] == keyword) {
    
    				cout << "found";
    				getchar();
    				return true;
    			}
    			if (!found == false && i == sizeof(words)) {
    				return false;
    			}
    		}
    Last edited by 2kaud; February 23rd, 2017 at 09:46 AM.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Compare 2 std::string in c++ virtual studio 2015

    Code:
    if (!found == false && i == sizeof(words))
    Within the for loop, i will never be equal to sizeof(words) as the for loop comparison test will fail and the loop will exit. Why have this test at all? If the loop exits normally then the word hasn't been found so just return false after the loop.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Compare 2 std::string in c++ virtual studio 2015

    You can actually shorten this code by using an STL algorithm function. Consider
    Code:
    bool search(string keyword) {
    	const string words[] = { "msg", "chrome","steam","spotify","discord" };
    
    	return find(begin(words), end(words), keyword) != end(words);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Feb 2017
    Posts
    3

    Re: Compare 2 std::string in c++ virtual studio 2015

    Thank you very much. It worked ^^ Could you please explain to me what the != end(works) exactly does to the find function?

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Compare 2 std::string in c++ virtual studio 2015

    If find() doesn't find the requested string, it returns the passed 2nd argument to find() - in this case end(words). So if the string is found, then the returned value is not end(words) and so the comparison test for not equal is true and so true is returned. If the comparison test for not equal is false, the string hasn't been found and so false is returned. See http://www.cplusplus.com/reference/algorithm/find/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Feb 2017
    Posts
    3

    Re: Compare 2 std::string in c++ virtual studio 2015

    Thanks a lot

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured