zoukankan      html  css  js  c++  java
  • cin cout getline string

    1.C++ code, When we want to read a number whatever the type is int or double , just use 

    cin >> Num_variable;

    Then, after read number, if we want to read a line of words, we have to use

    cin.ignore();

    and next line

    getline(cin, String);

    after we read a Num_variable, a ' ' char is still saved in the input buffer, the "cin.ignore()" can clear the input buffer

    so, the String can be set to really what you want to read, otherwise if we don't use cin.ignore(), code will read blank line("blank ") to String.

    e.g.,

       cin >> NUM;

       cin.ignore();

       getline(cin, String0);

       getline(cin, String1);

       getline(cin, String2);

       ·······

    2. C++ code, When we want to print a double type of number, we have to set the precision of this number.

    we have to #include <iomanip>

    before cout, we can use 

    cout << setiosflags(ios::fixed);

    and next line

    cout << setprecision(1) << e << endl;

    code will print 1 number after the number_point.

  • 相关阅读:
    elf和内存分布
    平衡二叉树
    sdio驱动
    wifi
    阻塞赋值与非阻塞赋值
    线性失真与非线性失真
    数字前端,后端介绍
    总线
    并行全比较排序算法&并对角标排序
    verilog memory
  • 原文地址:https://www.cnblogs.com/pandaroll/p/5548464.html
Copyright © 2011-2022 走看看