150 likes | 276 Vues
This guide provides a comprehensive overview of console input and output operations using the C++ iostream library. It explains how to use the `#include <iostream>` directive and the `using namespace std;` statement to facilitate input and output. The tutorial covers the `cout` function for displaying messages and variables, including formatted output with precision and escape sequences. It also delves into accepting user input with the `cin` function. Learn to effectively manage console interactions in your C++ programs with these foundational concepts.
E N D
ECE 1305Introduction to Engineering and Computer Programming Section 06Console Input and Output
Library iostream • Input from the keyboard and output to the monitor may be accomplished using functions in the library iostream. • In order to use these functions, the program must include the pre-processor directive #include<iostream> using namespace std;
Output using cout • The cout (console output) function may be used to print information on the computer monitor. • The insertion operator, << , is used to insert the information into the output stream. • The information may be literal cout << “The final answer is: “; The text in quotation marks is printed on the screen.
Output using cout • The information may be a variable. double temp = 98.6; cout << temp; • The value of the variable temp is printed on the screen.
Output using cout • Text and numerical values may be concatenated double temp = 98.6; cout << “It is “ << temp << “ Deg“; • The programmer must include the spaces between items.
Output using cout • The computer must be instructed to start a new line on the screen. cout << “Print this on the first line”; cout << endl; cout << “Print on the second line“; • The endl (end line) command starts a new line.
Output using cout • The endl command may be concatenated. cout << “First line“ << endl; cout << “Second line“;
Output using cout • The same effect is achieved using an escape sequence. • The escape sequences are identified by the backslash. cout << “First line \n“; cout << “Second line“;
Formatting Output • Floating point numbers will typically be printed with 6 decimal points. cout << (1.0/3.0); • will cause the following to be displayed 0.333333
Formatting Output • The number of digits that are displayed may be set with the following sequence of commands. cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2) • will change the way the output appears.
Formatting Output cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2) cout << (1.0/3.0); • will cause the following to be displayed 0.33
Formatting Output • The command cout.precision(number) • may be used to set the number of digits displayed. • This does not change the value stored in memory.
Input using cin • The cin (console input) function may be used to input information from the keyboard. • The insertion operator, >> , is used to insert the information into the input stream. int number; cout << “Enter an integer: “; cin >> number; • The program waits for a number to be entered. When the user presses “enter” the number is stored in the memory location for number.
Input using cin • A programmer may input more than one number with a single cin command. int n, m; cout << “Enter two integers: “; cin >> n,m; • The first number entered is stored in n, the second in m. • (This looks like a good way to make a mistake.)