• C++读取一行字符串输入


    这个知识点必须掌握,因为是必备技能!不然连输入都搞不定,何谈后面的?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	//对于char* / char[]
    	char s[1001];
    	cout<<"Please input char[]:"<<endl;
    	cin.getline(s, 1000);//iostream下的函数, 第二个参数表示允许的最大输入长度
    	cout<<"Output:"<<endl<<s<<endl<<strlen(s)<<endl; 
    
    	//对于string
    	string ss;
    	cout<<"Please input string:"<<endl;
    	getline(cin, ss); //这个getline函数在<string>头文件下
    	cout<<"Output:"<<endl<<ss<<endl<<ss.length()<<endl;
    
    	return 0;
    }
    /**
    输入和输出样例:
    Please input char[]:
    He llo
    Output:
    He llo
    6
    Please input string:
    Wor ld
    Output:
    Wor ld
    6
    */

  • 相关阅读:
    预处理命令
    函数
    结构体
    字符数组
    数组
    文件
    用 typedef 定义类型
    枚举类型
    联合
    位运算
  • 原文地址:https://www.cnblogs.com/lvlang/p/10586365.html
Copyright © 2020-2023  润新知