cmsc 202
Download
Skip this Video
Download Presentation
CMSC 202

Loading in 2 Seconds...

play fullscreen
1 / 25

CMSC 202 - PowerPoint PPT Presentation


  • 118 Views
  • Uploaded on

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’

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CMSC 202' - solstice


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cmsc 202

CMSC 202

Computer Science II for Majors

topics
Topics
  • C++ stream input / output
  • Formatted Output
  • Project 1 description
    • Project 1 posted on web
stream i o in c
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
stream i o in c cont
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”;
stream i o in c cont1
Stream I/O in C++ …cont
  • cout

Screen

<<

“C++”

cout

Object

Insertion Operator

Variable

stream i o in c cont2
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;
stream i o in c cont3
Stream I/O in C++ …cont
  • cin

Extraction Operator

Object

Variable

>>

cin

45

Key board

example
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”
stream i o operations
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
stream i o operations cont
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
stream i o operations cont1
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”);
stream i o operations cont2
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;

}

formatted output
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) << ... ;
formatted output cont
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

formatted output cont2
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;

}

formatted output cont3
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

formatted output cont4
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 ;
project 1 description
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
project 1 description cont
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
project 1 description cont1
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
project 1 description cont2
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

project 1 description cont3
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

project 1 description cont4
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

project 1 description cont5
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

ad