Selecting only the first few characters in a string C++

前端 未结 5 2079
遇见更好的自我
遇见更好的自我 2021-02-19 23:43

I want to select the first 8 characters of a string using C++. Right now I create a temporary string which is 8 characters long, and fill it with the first 8 characters of anot

5条回答
  •  无人及你
    2021-02-19 23:49

    Or you could use this:

    #include 
    cin.ignore(numeric_limits::max(), '\n');
    

    If the max is 8 it'll stop there. But you would have to set

    const char * word = holder.c_str();
    

    to 8. I believe that you could do that by writing

     const int SIZE = 9;
     char * word = holder.c_str();
    

    Let me know if this works.

    If they hit space at any point it would only read up to the space.

提交回复
热议问题