Search code examples
c++getscstdio

How to use gets() in cpp


These days I am learning strings and arrays in cpp. In my school they taught us to take a string as user input, one have to use gets() but it isn't working on any of the compilers I have. I have already tried using cstdio library, still errors. I know cin.getline() but it is a bit bigger word.


Solution

  • std::cin.get(); is something they teach in beginner classes, hopefully this is the smaller word for getline you were hoping for! :)

    In the future, use a program like Visual Studio which has IntelliSense and can list options for you - or just look at the documentation.

    The best method would probably be something along these lines:

    std::cout << "Input a string: ";
    std::string strInput;
    std::cin >> strInput;
    std::cout << std::endl << "Your string: " << strInput << std::endl;
    

    Good luck!