1 / 15

ECE 1305 Introduction to Engineering and Computer Programming

ECE 1305 Introduction to Engineering and Computer Programming. Section 06 Console Input and Output. Library iostream. Input from the keyboard and output to the monitor may be accomplished using functions in the library iostream .

loan
Download Presentation

ECE 1305 Introduction to Engineering and Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 1305Introduction to Engineering and Computer Programming Section 06Console Input and Output

  2. 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;

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Output using cout • The endl command may be concatenated. cout << “First line“ << endl; cout << “Second line“;

  8. 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“;

  9. Output using cout

  10. 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

  11. 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.

  12. 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

  13. 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.

  14. 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.

  15. 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.)

More Related