Transcript Slides

CSC-110
Program Design & Development
Prompting the User for Input
• When you want data from the user, it is helpful to
tell them what you want them to enter (a prompt).
– If you just use cin, all it will show is a blinking
cursor on the screen.
cin >> quiz1;
– What does that tell the user?
• So by providing text (output) prior to asking for
input, the user will know what you are looking
for him/her to enter.
Prompting the User for Input
• Example:
cout << "Enter grade for Quiz 1 ---> ";
cin >> quiz1;
• Notes:
– There is no endl or '\n' at the end of the prompt.
• This allows the user to enter his/her data directly next to
the prompt.
– There is no endl or '\n' at the end of the cin.
• The line (containing the prompt and the user input) will
terminate when the user presses “Enter” after entering
his/her data.
• Never put endl or '\n' at the end of a cin statement.
Formatting Output
• Formatting member functions come with the
iostream library.
– This is the older style of C++.
• Formatting manipulator functions come from the
iomanip library.
– This is the newer style of C++, which will be used in this
course.
– Must include the iomanip library header file:
#include <iomanip>
Formatting is a Multi-Step Process
First, the iomanip library must be included.
Then in the code, the following lines must be used prior to the
output:
cout << fixed;
cout << showpoint;
cout << setprecision(X);
fixed – floating-point numbers are not written in scientific notation
showpoint – a decimal and trailing zeros are always shown for
floating-point numbers
setprecision(X) – specifies X number of digits output after the
decimal point for floating-point numbers; will automatically round
output
Formatting is a Multi-Step Process
The three lines setting the formatting may be written together:
cout << fixed << showpoint << setprecision(X);
After fixed and showpoint are set once, you may change the
number of decimal places by using setprecision(X) by itself.
Formatting is a Multi-Step Process
Example:
float num = 145.348273;
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
fixed << showpoint << setprecision(2);
num;
endl;
setprecision(4);
num << endl;
13.135629 << endl;
setprecision(1) << num << endl;
setprecision(5) << "The number is " << num << endl;
"The number is " << setprecision(0) << num << endl;
"The number is " << noshowpoint << num << endl;
Result:
145.35
145.3483
13.1356
145.3
The number is 145.34827
The number is 145.
The number is 145
Leaving Space for Output
cout << setw(X);
setw(X) – where X is how much space you want reserved for the
next item of output
– by default, output is right justified in the space.
Example:
cout << setw(7) << 3.14 << 2.43 << setw(4) << 19;
cout << setw(3) << 45214 << endl << setw(4) << 2;
cout << setw(10) << 3 << endl;
Result:
bbb3.142.43bb1945214
bbb2bbbbbbbbb3
Leaving Space for Output
left – manipulator for specifying left-justified output in a
width field
Syntax: cout << left;
right – manipulator for specifying right-justified output in a
width field (default)
Syntax: cout << right;
Centering Output
• C++ does not have any built-in way to center output, so you must
manually create it.
• The output screen for C++ is 25 rows by 80 columns.
• Horizontal centering:
– In order to horizontally center the output, you must know how long the output
will be and add extra spaces to the left to move it over to the center.
– Make sure the output is set for right-justify
– And set the width to: 40 + (length_of_output / 2)
– Example:
cout << right; //Not necessarily needed--default setting
cout << setw(40 + (15 / 2)) << "Welcome to C++!\n";
Centering Output
• Vertical centering:
– To vertically center the output,
display (12 – (number_of_lines_of_output / 2)) blank lines prior to the
output.
– Example:
cout << "\n\n\n\n\n\n\n\n\n\n\n\n" << "This is centered!\n";
//The output is only one line long, so there are 12 blank
//lines needed.
Centering Output
• Combine both methods to output to the center of the whole screen:
– Example:
cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
//12 lines
cout << right;
cout << setw(40 + (28 / 2))
//Output is 28 characters long
<< "This is completely centered.\n";
Programming Assignment #1
Write a program that asks the user to enter the
two sides of a rectangle. The program will then
display the perimeter and area of the rectangle.
Both output values will be labeled and
formatted with two (2) decimal places.
Pseudocode due Tuesday, 26 September
Final program due Tuesday, 3 October