字符串替换C++中的空格中断

Nic*_*tum 0 c++

如果我在执行下面的代码时输入"我们将在学校玩得开心",它似乎在第一个单词后打破并且仅打印"W3".有谁知道我做错了什么?

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
  {
  string s;
  cout << "Invoer: ";
  cin >> s;

  replace( s.begin(), s.end(), 'e', '3' );
  replace( s.begin(), s.end(), 'o', '0' );
  replace( s.begin(), s.end(), 't', '7' );
  replace( s.begin(), s.end(), 'l', '1' );
  transform( s.begin(), s.end(), s.begin(), ::toupper);

  cout << s << endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

And*_*dyG 7

你需要使用getline而不是cin >> s抓住整行,否则它停在空白处:

getline(cin, s);
Run Code Online (Sandbox Code Playgroud)

现场演示