Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exposure C++ Chapter VII Program Input and Output.

Similar presentations


Presentation on theme: "Exposure C++ Chapter VII Program Input and Output."— Presentation transcript:

1 Exposure C++ Chapter VII Program Input and Output

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

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

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

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

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

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

8 // 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; }

9 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

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

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

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

13 // 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; }

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

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

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

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

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

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

20 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);

21 // 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= ;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 QQ Good MorningGoodMorning 1234 Q Good Morning Good Morning

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

23 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 = ; 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

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

25 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

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

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

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

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

30 // 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 = ; 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 Good Morning PROG0715.CPP OUTPUT #2 1234 Q Good Morning

31 // 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 = ; cout << setprecision(8); cout << setiosflags(ios::fixed) << Number << endl; cout << setiosflags(ios::showpoint) << Number << endl; cout << setiosflags(ios::scientific) << Number << endl; } PROG0716.CPP OUTPUT E+02

32 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

33 // 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= , N5= ; 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

34 // PROG0718.CPP // This program demonstrates potential difficulties using the setiosflags with showpoint void main() { double Number; cout << setprecision(5); cout << setiosflags(ios::showpoint); Number = ; cout << setw(18) << Number << endl; Number = ; Number = ; Number = ; Number = ; Number = ; Number = ; Number = ; Number = ; Number = ; } PROG0718.CPP OUTPUT e+06 e+07 e+08 e+09

35 // 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 = ; cout << setw(18) << Number << endl; Number = ; Number = ; Number = ; Number = ; Number = ; Number = ; Number = ; Number = ; Number = ; } PROG0718.CPP OUTPUT

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

37 // 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 = ; Amount = ; } PROG0720.CPP OUTPUT Pay $***** Pay $*** Pay $**


Download ppt "Exposure C++ Chapter VII Program Input and Output."

Similar presentations


Ads by Google