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.

  • 相关阅读:
    Mac item 远程连接服务器
    搭建私人Git Server
    数据结构第三章小结
    第二章实践小结
    poj3617 Best Cow Line
    最长上升子序列问题
    Uva11450 Wedding shopping
    poj3050 hopscotch
    poj2718 Smallest Difference
    poj3669 Meteor Shower
  • 原文地址:https://www.cnblogs.com/pandaroll/p/5548464.html
Copyright © 2011-2022 走看看