1 / 25

CMSC 202

CMSC 202. Computer Science II for Majors. Topics. C++ stream input / output Formatted Output Project 1 description Project 1 posted on web. Stream I/O in C++. Stream is a sequence of bytes C++ introduces objects for I/O cout which replaces ‘printf’ cin which replaces ‘scanf’

gavivi
Download Presentation

CMSC 202

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. CMSC 202 Computer Science II for Majors

  2. Topics • C++ stream input / output • Formatted Output • Project 1 description • Project 1 posted on web

  3. Stream I/O in C++ • Stream is a sequence of bytes • C++ introduces objects for I/O • cout which replaces ‘printf’ • cin which replaces ‘scanf’ • cerr : unbuffered standard error output • clog : buffered standard error output • Include iostream header file

  4. Stream I/O in C++ …cont • cout • Denotes the output stream (usually console) • Uses insertion operator << to output values to stream • cout << “C++ is better than C”;

  5. Stream I/O in C++ …cont • cout Screen << “C++” cout Object Insertion Operator Variable

  6. Stream I/O in C++ …cont • cin • Denotes input stream (usually keyboard) • Uses extraction operator >> to input values from stream • Skips white spaces • cin >> number1;

  7. Stream I/O in C++ …cont • cin Extraction Operator Object Variable >> cin 45 Key board

  8. Example • # include <iostream> • using std :: cout; • using std :: cin; • using std :: endl; • int main() • { • float num1, num2, avg; • cout << “Enter two numbers” ; • cin >> num1; • cin >> num2; • avg = (num1+num2) / 2; • cout << “Average = “ << avg << endl; • return 0; • } • endl is a Manipulator, causes linefeed to be inserted. Just like “/n”

  9. Stream I/O operations • Streams are used for File I/O operations • Common file operations • Open file • Process file • Close file • Include fstream header file

  10. Stream I/O operations … cont • Streams are typed by operations to be performed on them • ifstream denotes a file used for input • ofstream denotes a file used for output • fstream denotes a file used for both • File I/O used << and >> operators – similar to cin and cout

  11. Stream I/O operations … cont • Creating streams • Declare variable for each stream • ifstream Inputfile; • ofstream Outputfile; • Associate stream with a file • Inputfile.open (“file1”); • Outputfile.open (“file2”); • Also possible in single step • ifstream Inputfile (“file1”); • ofstream Outputfile (“file2”);

  12. Stream I/O operations … cont #include <fstream> using std::ifstream; using std::ofstream; int main() { ifstream InputFile ("input.txt"); // input stream ofstream OutputFile ("output.txt");// output stream int value = 0; while(InputFile >> value) // similar to cin >> value { OutputFile << "Value =" << value <<endl; } InputFile.close(); OutputFile.close(); return 0; }

  13. Formatted Output • Setting field width • Member Function width • cin.width(5); // input only 5 characters • cout.width(5); // sets # of character positions // in which value should be output • Manipulator setw • cin >> setw(5); • cout << setw(5) << ... ;

  14. Formatted Output … cont • Setting precision • Manipulators double pi = 3.14159 cout << setprecision(3) <<pi; // 3.142 • Member Function double pi = 3.14159; cout.precision(3); cout << pi; // 3.142

  15. Formatted Output … cont

  16. Formatted Output … cont • Example #include<iostream> #include<iomanip> using namespace std; int main() { float a,b,c; a = 9; b = 99.099; c = 9.901; cout << "Before formatting:" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl << endl; cout << "After formatting:" << endl; cout << fixed; cout << setprecision(2); cout << "a = " << setw(7) << a << endl; cout << "b = " << setw(7) << b << endl; cout << "c = " << setw(7) << c << endl; return 0; }

  17. Formatted Output … cont • Example Before formatting: a = 9 b = 99.099 c = 9.901 After formatting: a = 9.00 b = 99.10 c = 9.90

  18. Formatted Output … cont • Justification • Uses stream manipulators left and right • left : left-justify, padding characters on right • right: right-justify, padding characters on left • cout << left << setw(10) << x ; • cout << right << setw(10) << x ;

  19. Project 1 description • Objective : To use string and vector and to experience OOP as user of objects • To use C++ stream operators • Input : Text file • Command line argument • Assume all words are lowercase

  20. Project 1 description … cont • Output : an analysis of text in file • Print File name • Total number of words in file • Total number of distinct words • For each alphabet, print # of words, # of distinct words and # of occurrences • Most frequently occurring word

  21. Project 1 description … cont • Output … cont 6. Longest Word and # of characters in it • Letter that begins most number of words • Letter that begins most distinct words and # of distinct words with that letter • Letters that do not begin any words

  22. Project 1 description … cont • Sample Input (taken from Project description) a very angry and pesky antelope ate all of my angelhair pasta i really really really dislike a very pesky antelope

  23. Project 1 description … cont • Sample Output (taken from Project description) linux3[33]% Proj1 p1.dat Processing file "p1.dat" The file contains 21 words. The file contains 15 distinct words. Words that begin with 'a': a:2 angry:1 and:1 antelope:2 ate:1 all:1 angelhair:1 Total words: 9, Distinct words: 7 Words that begin with 'd': dislike:1 Total words: 1, Distinct words: 1

  24. Project 1 description … cont • Sample Output …cont Words that begin with 'i': i:1 Total words: 1, Distinct words: 1 Words that begin with 'm': my:1 Total words: 1, Distinct words: 1 Words that begin with 'o': of:1 Total words: 1, Distinct words: 1 Words that begin with 'p': pesky:2 pasta:1 Total words: 3, Distinct words: 2

  25. Project 1 description … cont • Sample Output …cont Words that begin with 'r': really:3 Total words: 3, Distinct words: 1 Words that begin with 'v': very:2 Total words: 2, Distinct words: 1 The most frequent word is "really" which occurred 3 times. The longest word is "angelhair" which contains 9 characters. The letter 'a' had the most words - 9 The letter 'a' had the most distinct words - 7 No words begin with the letters b, c, e, f, g, h, j, k, l, n, q, s, t, u, w, x, y, z

More Related