C++ Getline after Cin

前端 未结 3 1918
忘掉有多难
忘掉有多难 2021-01-21 22:28

I am trying to write a program which gets user\'s input in a specific way. First, I input a word which contains no space; Then, I input another word which may contains space; An

3条回答
  •  庸人自扰
    2021-01-21 23:15

    So it looks like your program is just grabbing the space that you put in the program, you can get rid of it several ways!

    1. You can stream the input yourself using cin.get() character by character you add them into a string until you get a space then keep going but don't add the spaces until you get something that isn't a space, then use your getline or you can continue your custom streaming this time looking for a newline!

    2. You can just edit the resulting string to remove the extra spaces super easily, look at the substr() method!

提交回复
热议问题