zoukankan      html  css  js  c++  java
  • C++每次读取一行字符串输入(学习笔记) (转)

    1. 面向行的输入:getline()
    getline()函数读取整行,它使用通过回车键输入的换行符来确定输入结尾。
    cin.getline(Arr, 20) // Arr为用来输入行的数组的名称;
                           //20包括19个字符和1个空字符  


    2. 面向行的输入:get()
    与getline()的工作方式类似,接受参数相同,但get()并不再读取并丢弃换行符,而是将其留在输入队列中。可如下使用:
    cin.get(Arr1, ArSize); // 读第一行
    cin.get();             // 读掉换行符
    cin.get(Arr2, ArSize2); // 读第二行

    ////////////////////////////////////////或者

    cin.get(Arr1, ArSize).get();
    cin.get(Arr2, ArSize2);


    3. 字符串输入:getline(cin, srt)
    getline(cin, str); // str为string类型  getline()需要#include<string>


    4. 输入时遇错误类型时
    int int nTemp;
    cout << "Please enter handicap(int): " << endl;
        while (!(cin >> nTemp))
        {
            cin.clear();//重置输入,如果省略这条,程序将拒绝继续读取输入
            while (cin.get() != ' ')//本循环使用 cin.get() 来读取尾行之前的所有输入,从而删除这一行
                continue;
            cout << "Please input a integer: ";
        }

     

  • 相关阅读:
    Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C
    Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1
    引入CSS文件的方式,以及link与@import的区别
    JavaScript
    css
    html
    CentOS 7编译安装Python3.7.X
    python3配置爬虫开发环境
    Threading模块
    队列Queue的get方法
  • 原文地址:https://www.cnblogs.com/acbingo/p/4674480.html
Copyright © 2011-2022 走看看