std::cin input with spaces?

前端 未结 8 1254
再見小時候
再見小時候 2020-11-21 05:41
#include 

std::string input;
std::cin >> input;

The user wants to enter \"Hello World\". But cin fails at the spa

8条回答
  •  时光取名叫无心
    2020-11-21 06:34

    You want to use the .getline function in cin.

    #include 
    using namespace std;
    
    int main () {
      char name[256], title[256];
    
      cout << "Enter your name: ";
      cin.getline (name,256);
    
      cout << "Enter your favourite movie: ";
      cin.getline (title,256);
    
      cout << name << "'s favourite movie is " << title;
    
      return 0;
    }
    

    Took the example from here. Check it out for more info and examples.

提交回复
热议问题