如何输入多个带空格的字符串? getline gets scanf cin.getline四种输入方式

版权声明:欢迎转载博客(转载时请附上原文链接^_^) https://blog.csdn.net/OneLine_/article/details/88936031

因为天梯赛的三道字符串自闭了

想好好补一下字符串输入……

以下是四种不同的 可以输入带空格的 字符串输入方式

注:getline 与 cin.getline 的使用方式略有不同 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;
char s[10000];
char a[10000];
char m[10000];
string str;

int main()
{
    gets(a);
    scanf("%[^\n]%*c", m);//读入一行数据 到换行符时截止
    cin.getline(s, 1000, '\n'); //输入截止符为 换行符
    getline(cin, str, 'z'); //遇到字符 z 停止输入
    puts("");
    cout << a << endl;
    cout << m << endl;
    cout << s << endl;
    cout << str << endl;
    return 0;
}

 输出结果如图: (最后一次输入是以 z 结尾 所以遇到 z 之后不再输入字符)

如有错误,请评论指出,谢谢~ 

猜你喜欢

转载自blog.csdn.net/OneLine_/article/details/88936031