[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSci202] / lab06
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Fri May 6 12:58:07 PDT 2011


    CSCI202 Lab06 -- Stream

      Goal learn to read input, count things, and produce formatted output

      Deliverable --Three Programs to count characters, words, and lines

      1. A program count_chars which opens cin, reads and counts each character, and sends the count to cout.
      2. A program count_words which opens cin, reads and counts each word, and sends the count to cout.
      3. A program count_lines which opens cin, reads and counts each line, and sends the count to cout.

      Resource -- the book and the web

        The main problem is knowing what to seach for...
      1. Chapter 2, Chpter 9, and Appendix E pages 971 thru to 973 covers most of the functions you need.
      2. On the web try [ http://www.cplusplus.com/reference/iostream/ ] and [ http://www.cplusplus.com/reference/iostream/istream/ ] (C Plus Plus dot Com).

      Note -- End of file

      In UNIX you signal the end of the input (cin) by ending the line and holding down the <Ctrl> key and taping the letter D. In MS try CTRL/Z.

      Note -- These programs should be filters

      The should read from cin and output to cout. But this does not mean that they can not be used with any file of text. You can use them on any text file like this
       		./executable <textfile


        Write a program to count the characters in a file

        In this program every character counts as one -- even spaces, tabs, and end-of-line. You need to find the correct way to get one char from an fstream to make the algorithm work.

        Algorithm to count characters input (

        1. declare and set a counter to zero.
        2. declare a variable to hold a char
        3. WHILE the program can get a char from the input stream DO (
          1. add 1 to the counter
        4. END WHILE
        5. Output a report to the user with the value of the counter

        Hint -- KEEP IT SIMPLE

        Develop a program to count words in a file

        As far as this program is concerned, a word is a string with no spaces, tabs, or end of line characters. So "1+2" and "mouchoir" are both words.

        Copy the character counter program into a new file and modify it to read and count C++ strings rather than chars. String input reads one "word" at a time.

        Strong hint: do NOT use the old C char*/cstring library.

        Second Hint -- Start by copying looking at the algorithm for the previous problem...

        Develop a program to count lines in a file

        Each line -- even a blank line, counts as a line. Again by selecting the right input function you can make this program by copying the previous one and making some small changes.

      Grading -- How much did you get done in 2 hours

      Done means: it (1) works and (2) is clean code.


  1. accessor::=`A Function that accesses information in an object with out changing the object in any visible way". In C++ this is called a "const function". In the UML it is called a query.
  2. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  3. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  4. constructor::="A Function in a class that creates new objects in the class".
  5. Data_Structure::=A small data base.
  6. destructor::=`A Function that is called when an object is destroyed".
  7. Function::programming=A selfcontained and named piece of program that knows how to do something.
  8. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  9. mutator::="A Function that changes an object".
  10. object::="A little bit of knowledge -- some data and some know how". An object is instance of a class.
  11. objects::=plural of object.
  12. OOP::="Object-Oriented Programming", Current paradigm for programming.
  13. Semantics::=Rules determining the meaning of correct statements in a language.
  14. SP::="Structured Programming", a previous paradigm for programming.
  15. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  16. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  17. Q::software="A program I wrote to make software easier to develop",
  18. TBA::="To Be Announced", something I should do.
  19. TBD::="To Be Done", something you have to do.
  20. UML::="Unified Modeling Language".
  21. void::C++Keyword="Indicates a function that has no return".