Download presentation
Presentation is loading. Please wait.
0
Introduction to C++ computers and programming
CMPT 129 © Janice Regan, CMPT
1
C++ Basic Input and Output (I/O)
To print to your computer’s screen (console) or the read data typed into your keyboard you will need to use objects from the C++ iostream library. To access the objects in the iostream library you must use a #include pre-processor directive to include their declarations: #include <iostream> This directive tells C++ to use appropriate library so we can use the I/O objects cin, cout, cerr, endl needed for simple input and output © Janice Regan, CMPT
2
C++ Spaces inside “ “ cout << fixed << setprecision(2)
<< "The balance is $" << balance << endl; cout << “the interest is “ << percent << “ % ” ; cout << setprecision(5) << percent/100 << endl; PRINTS The balance is $78.50 The interest is 12.45% Thee is no space between $ and the final quote in code, there is a space between % and the final “ in the code. Therefore, in the output $ is immediately followed by the value (no space) but % is followed by a space. © Janice Regan, CMPT
3
C++ endl When your code executes the instruction
Consider the output window (the window in which the output from your program, and the input from your keyboard is displayed.) When your code executes the instruction cout << endl; The cursor in the output window is moved to the beginning of the next line © Janice Regan, CMPT
4
C++: Moving to a new output line
C++ style: endl C++ can also use C style: "\n" escape sequence for the char "newline“ cout << "Hello World" << endl; cout << "Hello World\n"; In C++ both the statements print string literal "Hello World" to display, then move to next line © Janice Regan, CMPT
5
C++ Output Window Output
What can you print to your computer’s output window using the insertion operator <<? string name=“John”; const double scale_factor = 2.2; cout << scale_factor // Prints 2.2, the value of constant scale_factor cout << name; // Prints John, the name stored in variable name cout << “This is a string literal”; // Prints This is a string literal Each cout above prints the value of one variable or constant © Janice Regan, CMPT
6
C++ Output window Output
You can print the values of more than one variable or constant in a single cout statement cout << “the number of games was ” << numGames << endl << “our team won “ << numWin << “ games” << endl; Given that the variables have the values numGames = 23 and numWin = 15 This single cout statement prints the number of games was 23 our team won 15 games to the output window © Janice Regan, CMPT
7
C++ 3 Equivalent Outputs
cout << “the number of games was ” << numGames << endl << “ out team won “ << numWin << “ games” << endl; // One cout statement printing many values in succession cout << “the number of games was ” << numGames << endl; cout << “ out team won “ << numWin << “ games” << endl; // Two cout statements, each printing half the values cout << “the number of games was ” ; cout << numGames; cout << endl; cout << “ out team won “ ; cout << numWin ; cout << “ games” ; cout << endl; // each value printed using a single cout statement © Janice Regan, CMPT
8
Number of digits after decimal
There are two ways in C++ to specify how many digits to print after the decimal point Method 1 uses setprecision() and Uses <iostream> library only Uses “magic formula” to set number of digits after the decimal point The other method uses manipulators Uses <iomanip> and <iostream> libraries Syntax is simpler to remember © Janice Regan, CMPT
9
Explain the “magic formula”
cout.setf(ios::fixed); //Tells C++ to display output in fixed format xxx.yyy cout.setf(ios::showpoint); //Tells C++ to always print the decimal point //Tells C++ to print trailing 0’s cout.precision(3); //Tells C++ to print 3 digits after the decimal point Sets your program so all floating point numbers printed after these statements will print in fixed format with 3 digits after the decimal point Adding another cout.precision(N) statement later in your code will cause all floating point numbers after that statement to print with N digits after the decimal point … © Janice Regan, CMPT
10
Extend the “magic formula”
cout.unsetf(ios::fixed); //Tells C++ to stop displaying in fixed format xxx.yyy cout.setf(ios::scientific); //Tell C++ to start displaying in scientific notation xxx.yyy You can switch from fixed point notation to scientific notation (or from scientific notation to fixed point notation) First you must unset the flag telling C++ to print using floating point. Then you must set the flag to tell C++ to print using scientific notation. (Or unset scientific and set fixed) Results will not be predictable if you set both flags simultaneously © Janice Regan, CMPT
11
What else can you specify?
The programmer can explicitly specify how C++ should format the numbers output by their programs using the <iomanip> library In particular you can specify The number of digits printed The number of digits printed after the decimal point width of field (how many spaces to leave for a value) Fixed point or scientific notation E2 © Janice Regan, CMPT
12
C++ Manipulators fixed scientific setw() setprecision() left right
Used to control how output is formatted Require user to include <iomanip> library fixed scientific setw() setprecision() left right © Janice Regan, CMPT
13
Manipulators: fixed + setprecision()
fixed, prints number as fixed point, xx.yyy setprecision(2) indicates 2 digits after the decimal point Continues to use precision 2 and fixed until told to change in another cout command cout << "$" << fixed << setprecision(2) << 10.3 << " "<< "$" << << endl; Prints $ $20.51 © Janice Regan, CMPT
14
Manipulators: scientific + setprecision()
scientific and setprecision() manipulators: setprecision(4) indicates 4 digits after the decimal point cout << “population is " << scientific << setprecision(4) << mypop << endl; If mypop has value cout statement prints: population is e+005 scientific prints number in scientific notation, xxx.yy Ezz © Janice Regan, CMPT
15
Changing Format: Example
cout << fixed << setprecision(2) << "the interest is " << << "% or $”<< dollars << endl; cout << scientific << setprecision(4) << "The amount is " << << endl; Format changes after scientific is used. The code above print the following when dollars has value 33.12: the interest is 12.34% or $33.12 The amount is e+002 © Janice Regan, CMPT
16
Manipulator: setw() setw() Note: setw() affects only NEXT value output
Sets the width in characters of the output field By default output will be right justified in the output field If the output has the same number of characters as the number or spaces available within the field it will fill the field If the output has fewer characters as the number or spaces available within the field it will by default be right justified within the field Note: setw() affects only NEXT value output Must include setw() manipulator before each item output © Janice Regan, CMPT
17
Manipulator Example: setw()
setw() manipulator: cout << “xxxxxxxxxxxxxxxxxxx”; cout << endl<<"Start" << setw(5) << 10 << setw(4) << 20 << setw(6) << 30; Prints: xxxxxxxxxxxxxxxxxxxx Start © Janice Regan, CMPT
18
Manipulator Example: left, right
left and right manipulators: cout << fixed << left << setw(20) << interest << " %"; cout << endl << setw(20) << balance << endl; cout << setw(20) << right << ; Prints: xxxxxxxxxxxxxxxxxxxxxx % © Janice Regan, CMPT
19
Input Using cin Differences: cin >> num;
">>" (extraction operator) points opposite Think of it as "pointing toward where the data goes" Object name "cin" used instead of "cout" No literals allowed for cin Must input "to a variable" cin >> num; Waits on-screen for keyboard entry Value entered at keyboard is "assigned" to num © Janice Regan, CMPT
20
Prompting for Input: When using console output and keyboard input always "prompt" user for input cout << "Enter number of dragons: "; cin >> numOfDragons; No endl in cout means that the prompt "waits" on same line for keyboard input as follows: Enter number of dragons: Waits here for input © Janice Regan, CMPT
21
Prompting for Input: When using console output and keyboard input always "prompt" user for input cout << "Enter number of dragons: “ << endl; cin >> numOfDragons; endl in cout means that the prompt "waits" on next line for keyboard input as follows: Enter number of dragons: Waits here for input © Janice Regan, CMPT
22
Variable types and input
Be careful to give the correct type of data when responding to the prompt in a program. Items from the keyboard will be converted but this may still not give the results you expect Because of overflow Because of conversion © Janice Regan, CMPT
23
Input the correct type of data
Think of all the information you type in as a continuous stream of characters If you are reading an integer and you read a decimal point you will stop reading at the decimal point because the decimal point is not a part of an integer The next time you read you will begin with the decimal point left over from the last input © Janice Regan, CMPT
24
Incorrect input examples
int one; double two; Int three; cout << "enter an integer"; cin >> one; cout << "enter a double "; cin >> two; cin >> three; cout << one << “ “ << two << “ " << three; enter an integer4 enter a double 2.4 enter an integer7 enter an integer5.6 enter a double enter an integer6 enter an integer12 enter a double 44 enter an integer88 © Janice Regan, CMPT
25
Incorrect input examples
int one; int two; int three; cout << "enter an integer "; cin >> one; cout << "enter integer 2"; cin >> two; cout<<"enter integer 3"; cin>>three; cout << "XX" << endl << one << " "<< two <<three; enter an integer 4.44 enter integer 2enter integer 3XX enter an integer hello © Janice Regan, CMPT
26
C++ output by example Consider the code sampleIO.cpp posted with this set of notes. Lets discuss the examples given in the code © Janice Regan, CMPT
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.