C++输入输出
通过cin(istream)读取输入 1 2 char buf[10]{}; std::cin >> buf; // 如果输入超过10个字符会缓冲区溢出 可以使用setw限制输入,以下代码会读取前9个字符,最后一个字符用于\0。 需要注意的是cin通过»读取遇到空白字符会结束读取。 1 2 3 #include <iomanip> char buf[10]{}; std::cin >> std::setw(10) >> buf; 提取和空白符 1 2 3 4 5 6 7 8 int main() { char ch{}; while (std::cin >> ch) std::cout << ch; return 0; } 当用户输入以下内容时 Hello my name is Alex 提取运算符会跳过空格和换行符。因此,输出是 HellomynameisAlex 通过get函数读取 1 2 3 4 5 6 7 8 int main() { char ch{}; while (std::cin.get(ch)) std::cout << ch; return 0; } 当用户输入以下内容时 Hello my name is Alex 不会跳过空格和换行符。输出是 Hello my name is Alex ...