Exposure C++ Chapter VII Program Input and Output.

37
Exposure C++ Chapter VII Program Input and Output

Transcript of Exposure C++ Chapter VII Program Input and Output.

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

// 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.75ENTER YOUR WEEKLY HOURS ===>> 21GROSSPAY: 183.75

cin is used for input

cin >> Number;

cout is used for output

cout << Number;

Input/Output with cin and cout

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;

// 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

123456

// 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 ===>> AEnter Character 2 ===>> BEnter Character 3 ===>> C

ABC

PROG0703.CPP OUTPUT #2

Enter Character 1 ===>> ABEnter Character 2 ===>> Enter Character 3 ===>> CD

ABC

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

// 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: TodayString2: isString3: Sunday

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

// 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 ===>> GiraffeEnter a Number ===>> 1000

Word: GiraffeNumber: 1000

// 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 ===>> 1000Enter a word: ===>>Word:Number: 1000

// 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 ===>> 1000Enter a word ===>> Giraffe

Word: GiraffeNumber: 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);

// 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

12341234QQ123.456123.456Good 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.

// 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 asPROG0711.CCP

// 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 LineNew LineBABCDFGHIJK Tab Output Tab Output\ backslash'"Double Quotes"

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

Escape Sequences

Justifying Program Output

Right Justified

112

1231234

12345123.00

54321.454323.06

Left Justified

112123123412345123.0054321.454323.06

// PROG0714.CPP// This program demonstrates output formatting with setw.#include <iostream.h>#include <iomanip.h> // required library for using setwvoid 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

1121231234

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

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

1234Q 123.456 Good Morning

PROG0715.CPP OUTPUT #2

1234 Q 123.456 Good Morning

// 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.456789123.456789001.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

// 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.0001.10012.210123.3211234.432

// PROG0718.CPP// This program demonstrates potential difficulties using the setiosflags with showpointvoid main(){ double Number; cout << setprecision(5); cout << setiosflags(ios::showpoint); Number = 1.23456789; cout << setw(18) << Number << endl; Number = 12.3456789; cout << setw(18) << Number << endl; Number = 123.456789; cout << setw(18) << Number << endl; Number = 1234.56789; cout << setw(18) << Number << endl; Number = 12345.6789; cout << setw(18) << Number << endl; Number = 123456.789; cout << setw(18) << Number << endl; Number = 1234567.89; cout << setw(18) << Number << endl; Number = 12345678.9; cout << setw(18) << Number << endl; Number = 123456789; cout << setw(18) << Number << endl; Number = 1234567890; cout << setw(18) << Number << endl;}

PROG0718.CPP OUTPUT

1.2345712.34568123.45679

1234.5678912345.67890123456.789001.23457e+061.23456e+071.23456e+081.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; cout << setw(18) << Number << endl; Number = 123.456789; cout << setw(18) << Number << endl; Number = 1234.56789; cout << setw(18) << Number << endl; Number = 12345.6789; cout << setw(18) << Number << endl; Number = 123456.789; cout << setw(18) << Number << endl; Number = 1234567.89; cout << setw(18) << Number << endl; Number = 12345678.9; cout << setw(18) << Number << endl; Number = 123456789; cout << setw(18) << Number << endl; Number = 1234567890; cout << setw(18) << Number << endl;}

PROG0718.CPP OUTPUT

1.2345712.34568

123.456791234.5678912345.67890123456.789001234567.89000

12345678.90000123456789.00000

1234567890.00000

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;

Output Format Notes

// 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; cout << "Pay $" << setw(12) << Amount << endl; Amount = 1234567.87; cout << "Pay $" << setw(12) << Amount << endl;}

PROG0720.CPP OUTPUT

Pay $*****1234.00Pay $***123456.00Pay $**1234567.87