cin.ignore(intExp, chExp);
Here intExp is an integer expression yielding an integer value, and chExp is a char
expression yielding achar value. In fact, the value of the expression intExp specifies the
maximum number of characters to be ignored in a line.
cin.ignore(100, ' \n');
cin.ignore(75, ' A');
cin.get(ch);
The stream functionputback lets you put the last character extracted from the input
stream by the get function back into the input stream. The stream function peek looks
into the input stream and tells you what the next character is without removing it from
the input stream. By using these functions, after determining that the next input is a
number, you can read it as a number. You do not have to read the digits of the number as
characters and then convert these characters to that number.
istreamVar.putback(ch);
ch = istreamVar.peek();
cin.putback(ch); //puts the previous character extracted by the get function, which is 'b' ,
// back into the input stream.
cin.get(ch);
ch = cin.peek(); //checks the next character in the input stream, which is 'c' , and stores it in ch
cin.get(ch);
cin.clear(); //Restore input stream;
cin.ignore(200,' \n'); //Clear the buffer;
Output and Formatting Output
cout << expression or manipulator << expression or manipulator...;
#include <iomanip>
cout << setprecision(2); // formats the output of decimal numbers to two decimal places, until a similar
//subsequent statement changes the precision.
cout << fixed;
cout.unsetf(ios::fixed);
After the manipulator fixed is disabled, the output of the floating-point numbers return to their default settings. The manipulator scientific is used to output floating-point numbers in scientific format.
cout << fixed << showpoint;
The manipulator setw is used to output the value of an expression in a specific number of columns. The value of the expression can be either a string or a number. The expression setw(n) outputs the value of the next expression inn columns. The output is right-justified.
#include <iomanip>
cout << setw(5) << x << endl;
In C++, you can use the manipulator flush to clear the buffer even if the buffer is not full. In contrast to the manipulator endl, the manipulator flush does not move the insertion point to the beginning of the next line.
cout << "Enter an integer: " << flush;
cout << setfill('#');
cout << setw(5) << x << setw(7) << setfill('#')<< y << setw(8) << "Warm" << endl;
cout << left;
cout.unsetf(ios::left);
cout << right;
Input/Output and the string Type
string name; //variable declaration
cin >> name; //input statement; 空格终止
string myString;
getline(cin, myString);
#include <fstream>
//Add additional header files you use
using namespace std;
int main(){
//Declare file stream variables such as the following
ifstream inData;
ofstream outData;
.
.
.
//Open the files
inData.open("a:\\prog.dat"); //open the input file
//outData.open("a:\\firstProg.out", ios::app);
outData.open("a:\\prog.out"); //open the output file
//Code for data manipulation
//Close files
inData.close();
outData.close();
return 0;
}
bool Data Type and Logical (Boolean) Expressions
bool legalAge;
int age;
legalAge = (age >= 21);
switch (expression){
case value1:
statements1
break ;
case value2:
statements2
break ;
.
.
.
case valuen:
statementsn
break ;
default:
statements
}
#include <cassert>
assert(denominator);
quotient = numerator / denominator;
//initialize the loop control variable(s)
while (expression) //expression tests the LCV
{
.
.
.
//update the loop control variable(s)
.
.
.
}
Case 3: Flag-ControlledwhileLoops
found = false ; //initialize the loop control variable
while (!found) //test the loop control variable
{
.
.
.
if (expression)
found = true; //update the loop control variable
.
.
.
}
Case 4: EOF-Controlled whileLoops
infile.get(ch);
while (!infile.eof())
{
cout << ch;
infile.get(ch);
}
enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER,VOLLEYBALL };
sports popularSport, mySport;
//Assignment
popularSport = FOOTBALL;
mySport = popularSport;
//Operations on Enumeration Types
popularSport = static_cast<sports>(popularSport + 1);
popularSport = static_cast<sports>(popularSport - 1);
//ENUMERATION TYPES AND LOOPS
for (mySport = BASKETBALL; mySport <= SOCCER;mySport = static_cast<sports>(mySport + 1))
typedefStatement
typedef existingTypeName newTypeName;
Namespaces
namespace namespace_name{
members
}
namespace_name::identifier
浙公网安备 33010602011771号