Input and Output (IO) CSCE 110 Drawn from James Tam's material.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
J. Michael Moore Text Files CSCE 110 From James Tam’s material.
Getting Started With Pascal Programming
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables in Pascal.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
J. Michael Moore Modules: Getting information out CPSC 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Getting Started With Pascal Programming
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Introduction To Files In Pascal You will learn how to read from and write to text files in Pascal.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 11, Lecture 2 (Wed)
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Introduction to Pascal The Basics of Program writing.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Pascal Programming Today Chapter 11 1 Chapter 11.
Assignment statement and Arithmetic operation 1 The major part of data processing.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
Input and Output Output Prompt: –A message displayed to the user indicating what they are to enter. Should be clear and exact on what you want. write:
1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Pascal Programming Today Chapter 2 1 Chapter 2. Pascal Programming Today Chapter 2 2 »Output statements write data to output devices (e.g. VDU). »Two.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
C Formatted Input/Output
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Computer Science and an introduction to Pascal
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
INPUT & OUTPUT scanf & printf.
Presentation transcript:

Input and Output (IO) CSCE 110 Drawn from James Tam's material

Output Displaying information on screen Done via the write and writeln statements –Write: displays the output and nothing else (the cursor remains on the line) –Writeln: displays the output followed by a newline (the cursor moves to the next line)

Output Format (literal string of characters): write ('a message'); OR writeln ('a message');

Output Example program outputExample1 (output); begin write('line1'); writeln('line2'); write('line3'); end. Style convention Or is it? You can find a copy of this program in the shared course UNIX directory (/pub/courses/csce110/)

Output the Contents Of Variables And Constants Format: write( or ); or writeln ( or );

Output Variables And Constants Example program outputExample2 (output); const ACONSTANT = 888; begin var num : integer; num := 7; writeln(ACONSTANT); writeln(num); end. You can find an copy of this program and the compiled version in the course directory: /pub/courses/csce110/

Mixed Output It's possible to display literal strings of characters and the contents of variables and constants with a single write or writeln statement. Format: write('message',, 'message'…); OR writeln('message',, 'message'…);

Mixed Output Example program outputExample3 (output); const ACONSTANT = 888; begin var num : integer; num := 7; writeln('ACONSTANT: ', ACONSTANT); writeln('num=', num); end. You can find an copy of this program and the compiled version in the course directory: /pub/courses/csce110/

Automatic Output Formatting Automatic formatting of output –Field width: The computer will insert enough spaces to ensure that the information can be displayed. –Decimal places: For real numbers the data will be displayed in exponential/floating point form. 1 These values can be set to any non-negative integer (zero or greater).

Manual Output Formatting Format: write or writeln ( : : ); Examples: var num : real; num := 12.34; writeln(num); writeln(num:5:2); 1 These values can be set to any non-negative integer (zero or greater).

Formatting Output If the field width doesn't match the actual size of the field –Field width too small – extra spaces will be added for integer variables but not for other types of data. –Examples: var num : integer; num := ; writeln(num:3); writeln('123456':3);

Formatting Output If the field width doesn't match the actual size of the field –Field width too large – the data will be right justified (extra spaces will be put in front of the data). –Examples: var num : integer; num := 123; writeln(num:6); writeln('123':6);

Formatting Output If the number of decimal places doesn't match the actual number of decimal places. –Set the number of decimal places less than the actual number of decimal places – the number will be rounded up. –Example One: var num : real; num := ; writeln (num:6:2);

Formatting Output If the number of decimal places doesn't match the actual number of decimal places. –Set the number of decimal places greater than the actual number of decimal places – the number will be padded with zeros. –Example Two: var num : real; num := ; writeln(num:12:6);

Input The computer program getting information from the user Done via the read and readln statements Format: read ( ); OR readln ( );

Input Example program inputExampleOne (input, output); begin var num : integer; write('Enter an integer: '); readln (num); end. Normally Style…

Read & Readln Reads each value entered and matches it to the corresponding variable. –e.g., read (num) –If num is an integer then the read statement will try to read an integer value from the user's keyboard input.

Read Vs. Readln Read –If the user inputs additional values before hitting enter, the additional values will remain in the buffer. Readln –Any additional values entered before (and including) the enter key will be discarded.

Read: Effect On The Keyboard Buffer program getInput (input, output); begin var num : integer; write('Enter an integer: '); read(num); end. Pascal program Keyboard: user types in 27 and hits enter

Read: Effect On The Keyboard Buffer program getInput (input, output); begin var num : integer; write('Enter an integer: '); read(num); end. Pascal program 27 1 Keyboard controller: determines which keys were pressed and stores the values in the keyboard buffer 1 When the user presses the enter key it is stored as the EOL (end-of-line) marker. The EOL marker signals to the Pascal program that the information has been typed in and it will be processed. YY N Note: after the read statement has executed the pointer remains at the EOL marker. RAM 27 num

Readln: Effect On The Keyboard Buffer program getInput (input, output); begin var num : integer; write('Enter an integer: '); readln(num); end. Pascal program Keyboard: user types in 27 and hits enter

Readln: Effect On The Keyboard Buffer program getInput (input, output); begin var num : integer; write('Enter an integer: '); readln(num); end. Pascal program 27 1 Keyboard controller: determines which keys were pressed and stores the values in the keyboard buffer 1 When the user presses the enter key it is stored as the EOL (end-of-line) marker. The EOL marker signals to the Pascal program that the information has been typed in and it will be processed. YY N Note: Unlike read, the readln will move the pointer past the EOL marker (input buffer is emptied and ready for new input). RAM 27 num

Readln: Effect On The Keyboard Buffer program getInput (input, output); begin var num : integer; write('Enter an integer: '); readln(num); end. Pascal program 27 1 Keyboard controller: determines which keys were pressed and stores the values in the keyboard buffer 1 When the user presses the enter key it is stored as the EOL (end-of-line) marker. The EOL marker signals to the Pascal program that the information has been typed in and it will be processed. Note: Unlike read, the readln will move the pointer past the EOL marker (input buffer is emptied and ready for new input). RAM 27 num N

Read Vs. Readln If no input is read in by the program after a 'read' or 'readln' statement then both approaches appear identical (the effect of the pointer staying or moving past the EOL marker has no visible effect). Caution! If the 'read' or 'readln' statement is followed by another read or readln then the effect of the extra input remaining in the keyboard buffer can have unexpected consequences!

Read Example program read1 (input, output); begin var num : integer; var ch : char; write('Enter a number: '); read(num); write('Enter a character: '); read(ch); writeln('You entered num: ', num, ' ch: ', ch); end. You can find an copy of this program and the compiled version in the course directory: /pub/courses/csce110/

Readln Example program read2 (input, output); begin var num : integer; var ch : char; write('Enter a number: '); readln(num); write('Enter a character: '); readln(ch); writeln('You entered num: ', num, ' ch: ', ch); end. You can find an copy of this program and the compiled version in the course directory: /pub/courses/csce110/

General Rule Of Thumb When getting input from the user unless there's a compelling reason you should use 'readln' rather than 'read'. (This is an important point: forget at your own peril!)

What happens? Best The prompt that requests user input should take the form of a write rather than a writeln: var num : integer; write('Enter your age: '); readln(age); var num : integer; writeln('Enter your age: '); readln(age); General Rule Of Thumb

Another Use For Readln As an input prompt e.g., writeln('To continue press enter'); readln; writeln('The rest of the program continues..'); When this statement is reached the program will pause and wait for input from the user.