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.

原文地址:https://www.cnblogs.com/pandaroll/p/5548464.html