c++学习随笔 setw( n )

About the use of setw( int n ), First the setw() is included in the header comment #include, Setw means “set the width”,it’s usually right-aliged in the field of n spaces , you can type like this :

1
cout << setw(25) << "Hello World " << endl;

1
Hello World

If you want to set the characters being left-aligned, you can use setiosflags(ios::left). Also, you may use setfill() to fill the blanks with “*” “@” and so on, let me give an example:

1
2
cout << setw(25) << setiosflags(ios::left) << "Hello World " << endl;
cout << setw(25) << setiosflags(ios::left) << setfill('*') << "Hello World " << endl;

you will see

1
2
Hello World
Hello World**************

Decomposing makes everything easier…(分解使一切变得更容易!)

About a guessing gamme
First Figure out how to generate a random number within a given range of values.

1
srand() rand()

Second,Create a main function that processes one guess from the player, and provides hints
Third,Add what we need to allow for multiple guesses until the player guesses the number.