2

Hey I am a new C++ learner, I have been creating a program that do something specific... I take input from the user, and converted it into a lowercased sentence, then I use the "in"keyword like we use in python.... For example in python :

main_input = input("Type exit with a bunch of other words: ")
lower_input = main_input.lower()
if "exit" in lower_input:
    exit()
else:
    pass

What this does is that searches for the exit keyword from the input(like "ok you can exit now") and follows accordingly, exits the program. But how to use "in" keyword in c++? I want to do something like this:

#include <iostream>
#include "boost/algorithm/string.hpp"
using namespace std;

int main() {

    while(true){
        std::string main_input;
        std::cout << "Type something: ";
        std::cin >> main_input;
        std::string lower_input = boost::algorithm::to_lower_copy(main_input);
/* the if statement below this, here is the problem...*/
        if("exit" in lower_input){
            std::cout << "Exiting";
            /* after that other statements if necessary to execute*/

        }

    }
  
}

Can anyone tell me how to do that?? Thanks in advance!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
shakhyar.codes
  • 218
  • 4
  • 14
  • 1
    You can use [`find`](http://www.cplusplus.com/reference/string/string/find/), or if you want an exact match - just plain old `==`... – fredrik Aug 21 '20 at 11:01
  • 3
    "_But how to use "in" keyword in c++?_" there is no `in` keyword in C++. – Algirdas Preidžius Aug 21 '20 at 11:23
  • 1
    O.T.: I appreciate the fact that you prefixed every standard stuff with its scope `std::`. However, that makes the `using namespace std;` near top even more annoying... ;-) – Scheff's Cat Aug 21 '20 at 12:00
  • @AlgirdasPreidžius got it! – shakhyar.codes Aug 22 '20 at 08:42
  • @Scheff to be honest I am a python developer, and this is my 4th day learning c++!! I am a beginner, but I found what you told just now when i was messing around with `using namespace`, now I can write directly cout or cin instead of writing std::cout or std::cin!! – shakhyar.codes Aug 22 '20 at 08:45
  • 1
    @ROG_SHAKHYAR If you are learning C++, consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), since you can't learn it just by "messing around", due to various cases of [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). – Algirdas Preidžius Aug 22 '20 at 09:15
  • 1
    To be honest: Prefixing standard stuff with `std::` is completely fine and better than `using namespace std;` ([Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/7478597)). – Scheff's Cat Aug 22 '20 at 13:48

1 Answers1

2

You can use the member function find of the class std::string.

Something like

if ( lower_input.find( "exit" ) != std::string::npos ){

But it would be more accurate to write

if ( lower_input == "exit" ) {

because the user can enter a sentence that contains the symbols "exit" without an intention to break the loop.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335