Exposure C++ Chapter VII Program Input and Output.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Exposure C++ Chapter IV Introduction to C++ Programs.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Due Dates Quiz 1, 2 : Friday September 11 th Quiz 3 : Friday September 18 th Quiz 4 : Monday September 28 th Lab 1, 2 and 3 : Friday Sep 11 th Project.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Exposure C++ Chapter V Variables and Constants The Limitation of Constants // PROG0501.CPP // This program demonstrates using four constants. #include.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Exposure C++ Chapter VI Data Type Operations C++ Integer Operations Symbols +Addition -Subtraction *Multiplication /Integer division %Modulus or Remainder.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Chapter 3: Input/Output
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Topic 2 Input/Output.
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 3: Input/Output
Introduction to cout / cin
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Fundamental Programming
Chapter 2 part #3 C++ Input / Output
getline() function with companion ignore()
Presentation transcript:

Exposure C++ Chapter VII Program Input and Output

Program Input With cin C++ provides an input tool that looks, and behaves, in the opposite manner as cout, called cin. This C++ keyword is pronounced c-in. You will find that the special operators used for cout << are also used for cin. With program input, the “less-than chevrons” are turned around to become “greater-than chevrons.”

Insertion and Extraction Operators cout puts ( inserts ) characters into the output stream with the insertion operator <<   cin removes ( extracts ) characters from the input stream with the extraction operator >>

// This program demonstrates numerical keyboard input. // PROG0701.CPP // This program demonstrates numerical keyboard input. #include <iostream.h> void main() { double Payrate; // hourly wage int HoursWorked; // hours worked per week double GrossPay; // hours worked times hourly wage cout << "ENTER YOUR HOURLY PAYRATE ===>> "; cin >> Payrate; cout << "ENTER YOUR WEEKLY HOURS ===>> "; cin >> HoursWorked; GrossPay = Payrate * HoursWorked; cout << endl; cout << "GROSSPAY: " << GrossPay << endl; } PROG0701.CPP OUTPUT ENTER YOUR HOURLY PAYRATE ===>> 8.75 ENTER YOUR WEEKLY HOURS ===>> 21 GROSSPAY: 183.75

Input/Output with cin and cout cin is used for input cin >> Number; cout is used for output cout << Number;

Using a "Prompt" with Program Input cin stops program execution and waits for input. There is no indication other than the execution halt that anything needs to be done. Every input statement should be preceded by a logical prompt. Prompt Example: cout << "Enter a positive integer --> "; cin >> Number;

#include <iostream.h> // PROG0702.CPP // This program demonstrates multiple numeric keyboard input on one line. #include <iostream.h>   void main() { int Nbr1,Nbr2; cout << "ENTER INTEGER1 <Space> INTEGER2 ===>> "; cin >> Nbr1 >> Nbr2; cout << endl; cout << Nbr1 << endl; cout << Nbr2 << endl; } PROG0702.CPP OUTPUT ENTER INTEGER1 <Space> INTEGER2 ===>> 123 456 123 456

// PROG0703.CPP // This program demonstrates character keyboard input. // The second input statement is ignored when more than one // character is entered.   #include <iostream.h> void main() { char C1,C2,C3; cout << "Enter Character 1 ===>> "; cin >> C1; cout << "Enter Character 2 ===>> "; cin >> C2; cout << "Enter Character 3 ===>> "; cin >> C3; cout << endl; cout << C1 << C2 << C3 << endl; }

PROG0703.CPP OUTPUTS PROG0703.CPP OUTPUT #1 Enter Character 1 ===>> A Enter Character 2 ===>> B Enter Character 3 ===>> C ABC PROG0703.CPP OUTPUT #2 Enter Character 1 ===>> AB Enter Character 2 ===>> Enter Character 3 ===>> CD ABC

// This program demonstrates multiple character keyboard // PROG0704.CPP // This program demonstrates multiple character keyboard // input on one line. // This program works with or without inserted "white" // space. #include <iostream.h> void main() { char C1,C2,C3; cout << "Enter 3 Characters ===>> "; cin >> C1 >> C2 >> C3; cout << endl; cout << C1 << C2 << C3 << endl; } PROG0704 OUTPUT #1 Enter 3 Characters ===>> ABC PROG0704 OUTPUT #2 Enter 3 Characters ===>> A B C

cin and White Space The cin function ignores any white space characters in the input stream. Only visible characters are “extracted.” Letters, numbers and symbols can be entered. Space-bar, Enter key, cursor-keys, Function-keys, etc. are characters known as white space, and will not be extracted by cin from the input stream.

// This program demonstrates string keyboard input. // PROG0705.CPP // This program demonstrates string keyboard input. // StringVar only stores characters until the first // "white-space" character is found in the input stream. #include <iostream.h> #include "APSTRING.H" void main() { apstring StringVar; cout << "ENTER A STRING ===>> "; cin >> StringVar; cout << endl; cout << StringVar << endl; } PROG0705.CPP OUTPUT #1 ENTER A STRING ===>> QWERTY QWERTY PROG0705.CPP OUTPUT #2 ENTER A STRING ===>> TODAY IS SUNDAY TODAY

// PROG0706.CPP // This program demonstrates multiple string input on one line. // Using cin >> with string input causes "white space" problems. #include <iostream.h> #include "APSTRING.H" void main() { apstring String1,String2,String3; cout << "ENTER STRING 1 <sp> STRING 2 <sp> STRING 3 ===>> "; cin >> String1 >> String2 >> String3; cout << endl; cout << "String1: " << String1 << endl; cout << "String2: " << String2 << endl; cout << "String3: " << String3 << endl; }

PROG0706.CPP OUTPUT ENTER STRING1 <SP> STRING2 <SP> STRING3 ===>> Today is Sunday String1: Today String2: is String3: Sunday

// This program demonstrates entering strings with "white space". // PROG0707.CPP // This program demonstrates entering strings with "white space". // The APSTRING getline function is introduced to input all // characters, including "white space" characters. #include <iostream.h> #include "APSTRING.H" void main() { apstring Sentence; cout << "Enter a short sentence ===>> "; getline(cin,Sentence); cout << endl; cout << Sentence << endl; } PROG0707.CPP OUTPUT Enter a short sentence ===>> The quick brown fox lives. The quick brown fox lives.

// This program demonstrates entering a string with getline // PROG0708.CPP // This program demonstrates entering a string with getline // followed by entering a number with cin. // This sequence causes no problems. #include "APSTRING.H" void main() { apstring Word; int Number; cout << "Enter a Word ===>> "; getline(cin,Word); cout << "Enter a Number ===>> "; cin >> Number; cout << endl; cout << "Word: " << Word << endl; cout << "Number: " << Number << endl; } PROG0708.CPP OUTPUT Enter a word ===>> Giraffe Enter a Number ===>> 1000 Word: Giraffe Number: 1000

// This program demonstrates entering a number with cin // PROG0709.CPP // This program demonstrates entering a number with cin // followed by entering a string with getline. // This program has a stream buffer problem. #include <iostream.h> #include "APSTRING.H" void main() { apstring Word; int Number; cout << "Enter a Number ===>> "; cin >> Number; cout << "Enter a Word ===>> "; getline(cin,Word); cout << endl; cout << "Word: " << Word << endl; cout << "Number: " << Number << endl; } PROG0709.CPP OUTPUT Enter a number ===>> 1000 Enter a word: ===>> Word: Number: 1000

// This program solves the problem of leaving "white" space in // PROG0710.CPP // This program solves the problem of leaving "white" space in // the input stream by cin. A Dummy getline statement is // used to clear the buffer. #include <iostream.h> #include "APSTRING.H" void main() { apstring Word,Dummy; int Number; cout << "Enter a Number ===>> "; cin >> Number; getline(cin,Dummy); cout << "Enter a Word ===>> "; getline(cin,Word); cout << endl; cout << "Word: " << Word << endl; cout << "Number: " << Number << endl; } PROG0710.CPP OUTPUT Enter a number ===>> 1000 Enter a word ===>> Giraffe Word: Giraffe Number: 1000

String Input Notes Only “visible” string characters can be entered with cin. All white space is ignored by cin. String input, including white space, is done with getline. getline(cin,StringVariable); Using a getline statement after a cin statement causes a special situation. White space - like <Enter> - is left in the input stream. You need to “flush” the input stream with a special dummy string variable.

You need to “flush” the input stream with a special dummy string variable. int Number; apstring StringVariable, DummyString; cout << "Enter a number ===>> "; cin >> Number; getline(cin,DummyString); cout << "Enter a string ===>> "; getline(cin,StringVariable);

// This program demonstrates the importance of using // PROG711.CPP // This program demonstrates the importance of using // proper program style in writing source code #include <iostream.h> #include "APSTRING.H" void main(){int IntVar=1234;char CharVar='Q'; double DoubleVar=123.456;apstring StringVar="Good Morning"; cout<<IntVar<<IntVar<<endl;cout<<CharVar<<CharVar<<endl;cout << DoubleVar<<DoubleVar<<endl;cout<<StringVar<<StringVar<<endl; cout<<endl;cout<<'\t'<<IntVar<<'\t'<<'\t'<< IntVar<<endl;cout << '\t' <<CharVar<<'\t'<<'\t'<<CharVar<< endl;cout <<'\t'<<DoubleVar<< '\t'<<'\t'<<DoubleVar<<endl;cout<<'\t'<<StringVar<<'\t'<< StringVar<< endl;} PROG0711.CPP OUTPUT 12341234 QQ 123.456123.456 Good MorningGoodMorning 1234 Q 123.456 Good Morning Good Morning

Program Output Formatting Proper output formatting is very desirable. Look at any magazine, book or flyer. You will see columns line up, text justified, numbers neatly arranged according to decimal points, and in general you will see output that is pleasing. C++ has some useful library functions and other features that will assist the programmer in controlling output. In the last chapter, you have already been introduced to setprecision, which controls the number of digits displayed beyond the decimal point. We shall look at that function again and combine it with several new functions to control real number output.

This is the same output as // PROG0712.CPP // This program demonstrates output formatting with // the tab (/t) character. #include <iostream.h> #include "APSTRING.H" void main() { int IntVar = 1234; char CharVar = 'Q'; double DoubleVar = 123.456; apstring StringVar = "Good Morning"; cout << IntVar << IntVar << endl; cout << CharVar << CharVar << endl; cout << DoubleVar << DoubleVar << endl; cout << StringVar << StringVar << endl << endl; cout << '\t' << IntVar << '\t' << '\t' << IntVar << endl; cout << '\t' << CharVar << '\t'<< '\t' << CharVar << endl; cout << '\t' << DoubleVar << '\t'<< '\t'<< DoubleVar << endl; cout << '\t' << StringVar << '\t' << StringVar << endl; } This is the same output as PROG0711.CCP

// This program demonstrates the use of escape sequences. // PROG0713.CPP // This program demonstrates the use of escape sequences. // Be aware that \n should can cause problems together with cout. // You should use endl with cout. #include <iostream.h> #include "APSTRING.H" void main() { cout << "New Line" << '\n'; cout << "New Line\n"; cout << 'A' << '\b' << 'B' << endl; cout << "ABCDE\bFGHIJK" << endl; cout << '\t' << "Tab Output" << endl; cout << "\tTab Output" << endl; cout << "\\ backslash" << endl; cout << '\'' << endl; cout << "\"Double Quotes\"" << endl; } PROG0713.CPP OUTPUT New Line B ABCDFGHIJK Tab Output \ backslash ' "Double Quotes"

Escape Sequences Use an escape sequence as a single character or in a string to get specialized output. cout << "\tTab Output" << endl; cout << '\t' << "Tab Output" << end; \t Tab next output \n New line (Do not use with cout; use endl) \b Backspace \\ Backslash \' Apostrophe \" Quotes

Justifying Program Output Left Justified 1 12 123 1234 12345 123.00 54321.45 4323.06 Right Justified 1 12 123 1234 12345 123.00 54321.45 4323.06

// PROG0714.CPP // This program demonstrates output formatting with setw. #include <iostream.h> #include <iomanip.h> // required library for using setw void main() { int Nr1, Nr2, Nr3, Nr4; Nr1 = 1; Nr2 = 12; Nr3 = 123; Nr4 = 1234; cout << Nr1 << endl; cout << Nr2 << endl; cout << Nr3 << endl; cout << Nr4 << endl << endl; cout << setw(8) << Nr1 << endl; cout << setw(8) << Nr2 << endl; cout << setw(8) << Nr3 << endl; cout << setw(8) << Nr4 << endl << endl; cout << setw(8) << Nr1 << setw(6) << Nr2 << endl; cout << setw(8) << Nr3 << setw(6) << Nr4 << endl; } PROG0714.CPP OUTPUT 1 12 123 1234 1 12 123 1234

Using setw The setw function is used to “right justify” program output. Program output occupies the total number of spaces that are specified in the setw function. The setw function is ignored if the program output exceeds the setw size. The setw function does not work with char. setw is part of the <iomanip.h> function library. The examples on the next slide use the letter b for each preceding blank space.

Program Statement cout << setw(5) << 123 << endl; cout << setw(6) << 56 << endl; cout << setw(3) << 1234 << endl; cout << setw(8) << 987.45 << endl; Output Result bb123 bbbb56 1234 bb987.45

// PROG0715.cpp // This program demonstrates output formatting with the setw function. // It also demonstrates setw problems with char. // Run the program a second time with comments in front of Line1. // Remove the comments from Line 2. This will fix the char problem. #include <iostream.h> #include <iomanip.h> #include "APSTRING.H" void main() { int IntVar = 1234; char CharVar = 'Q'; // Line 1 // apstring CharVar = "Q"; // Line 2 double DoubleVar = 123.456; apstring StringVar = "Good Morning"; cout << setw(15) << IntVar << endl; cout << setw(15) << CharVar << endl; cout << setw(15) << DoubleVar << endl; cout << setw(15) << StringVar << endl; } PROG0715.CPP OUTPUT #1 1234 Q 123.456 Good Morning PROG0715.CPP OUTPUT #2 1234 Q 123.456 Good Morning

// This program demonstrates output formatting with // PROG0716.CPP // This program demonstrates output formatting with // the setiosflags fixed, showpoint and scientific. #include <iostream.h> #include <iomanip.h> void main() { double Number; Number = 123.456789; cout << setprecision(8); cout << setiosflags(ios::fixed) << Number << endl; cout << setiosflags(ios::showpoint) << Number << endl; cout << setiosflags(ios::scientific) << Number << endl; } PROG0716.CPP OUTPUT 123.456789 123.45678900 1.23456789E+02

Using setiosflags setiosflags produces output formatting for various purposes. setiosflags(ios::fixed) fixed point output setiosflags(ios::showpoint) display trailing zeroes setiosflags(ios::scientific) scientific notation

// This program demonstrates output formatting with // PROG0717.CPP // This program demonstrates output formatting with // real numbers such that the floating points are lined up. #include <iostream.h> #include <iomanip.h> void main() { cout << setprecision(3); cout << setiosflags(ios::showpoint); double N1=1, N2=1.1, N3=12.21, N4=123.321, N5=1234.4321; cout << setw(15) << N1 << endl; cout << setw(15) << N2 << endl; cout << setw(15) << N3 << endl; cout << setw(15) << N4 << endl; cout << setw(15) << N5 << endl; } PROG0717.CPP OUTPUT 1.000 1.100 12.210 123.321 1234.432

// PROG0718.CPP // This program demonstrates potential difficulties using the setiosflags with showpoint void main() { double Number; cout << setprecision(5); cout << setiosflags(ios::showpoint); Number = 1.23456789; cout << setw(18) << Number << endl; Number = 12.3456789; Number = 123.456789; Number = 1234.56789; Number = 12345.6789; Number = 123456.789; Number = 1234567.89; Number = 12345678.9; Number = 123456789; Number = 1234567890; } PROG0718.CPP OUTPUT 1.23457 12.34568 123.45679 1234.56789 12345.67890 123456.78900 1.23457e+06 1.23456e+07 1.23456e+08 1.23457e+09

// PROG0719.CPP // This program demonstrates how fixed works together with showpoint to achieve the desired // decimal number output. void main() { double Number; cout << setprecision(5); cout << setiosflags(ios::showpoint); cout << setiosflags(ios::fixed); Number = 1.23456789; cout << setw(18) << Number << endl; Number = 12.3456789; Number = 123.456789; Number = 1234.56789; Number = 12345.6789; Number = 123456.789; Number = 1234567.89; Number = 12345678.9; Number = 123456789; Number = 1234567890; } PROG0718.CPP OUTPUT 1.23457 12.34568 123.45679 1234.56789 12345.67890 123456.78900 1234567.89000 12345678.90000 123456789.00000 1234567890.00000

Output Format Notes Frequently output format requires right justification, zeroes following the floating point, and no scientific notation for large numbers. Such an output is achieved by using the following three function calls before any output format: cout << setiosflags(ios::showpoint); cout << setiosflags(ios::fixed); cout << setprecision(3); // or other precision number These three functions only need to be called once in a program, unlike setw which must be called every time before any program output, like: cout << setw(10) << Number1 << endl; cout << setw(10) << Number2 << endl;

// This program demonstrates how leading output can be filled in // PROG0720.CPP // This program demonstrates how leading output can be filled in // with special fill characters. #include <iostream.h> #include <iomanip.h> void main() { double Amount; cout << setprecision(2); cout << setiosflags(ios::showpoint); cout << setiosflags(ios::fixed); cout << setfill('*'); Amount = 1234; cout << "Pay $" << setw(12) << Amount << endl; Amount = 123456; Amount = 1234567.87; } PROG0720.CPP OUTPUT Pay $*****1234.00 Pay $***123456.00 Pay $**1234567.87