代码:
#include <iostream>
#define BUFF 100
using namespace std;
int main(int argc, char* argv[])
{
char input[BUFF];
cout << "Input:\n>";
cin >> input;
cout << "\n\nOutput:" << input;
cin >> input;
cin >> input;
}
当您将输入读入 char 数组时,为什么会跳过空格?哦,还有 2 个 cin(在末尾),因为它的行为有点奇怪,如果在有 1 个 cin 时输入空格,它就会退出……也不知道为什么。
如
我输入cup cake
,它输出cup
最佳答案
问题是你想读什么?如果你想读取一行,你应该使用 getline
,如果可能的话使用 std::string
因为它会根据需要增长字符串:
#include <iostream>
#include <string>
int main() {
std::string line;
std::getline( std::cin, line );
}
关于c++ - std::cin 到 char 数组会跳过空格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7240843/