YALE UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE

CPSC 427: Object-Oriented Programming Handout #4
Professor M. J. Fischer October 4, 2016



Problem Set 3

Due before midnight on Wednesday, October 12, 2016.

This short assignment is designed to deepen your understanding of C++ IO and of character representations.

1 Assignment Goals

  1. Learn how ASCII character codes are used to represent characters.
  2. Learn how to obtain the ASCII code of a value of type char.
  3. Learn how to print an int as the ASCII character that it represents.
  4. Learn how to print an int as a decimal number.
  5. Learn how to print an int as a hex number.
  6. Learn how to test if a char is printable.
  7. Learn how to use IO manipulators dec, hex, setw(), and setfill() to control the printed form of numbers.
  8. Learn what in>>val does when val has type int.
  9. Learn how to use in.get(ch) to read a single character.
  10. Learn what cout<<val does when val has type int and when val has type char.
  11. Learn how to recover after a failed attempt to read a decimal integer.

2 Problem

You should write a program that takes the name of a file as a command line argument. The file will consist of a mixture of letters, digits, punctuation, whitespace characters, and control characters. Your program should open the file on an input stream in and declare an int variable x. It should then repeatedly attempt in>>x. If a number is successfully read into x, then x should be printed in decimal on a line by itself.

If the attempt to read x fails, then the next character should be read from the stream using in.get(ch), where ch has type char, and a one-line “Skipping…” message should be printed. Depending on the character read, the message might look like either of the following:

Skipping char: 116 0x74 't'
Skipping char:   0 0x00
In each case, the ASCII code of ch is printed first in decimal, right-justified in a 3-character field without zero-fill, and then again in hex, prefixed by “0x”, followed by a right-justified 0-filled hex number in a 2-character field. If ch is printable as defined by isprint(), then it should also be printed as a character, enclosed in single quotes as shown.

For example, if file data.in contains the text:

Score was 35to21.

the output should be:

 
---------------------------------------------------------------  
        Ima Goetting Closeau  
        CPSC 427/527  
        Tue Oct  4 2016 11:18:06  
---------------------------------------------------------------  
Skipping char:  83 0x53 'S'  
Skipping char:  99 0x63 'c'  
Skipping char: 111 0x6f 'o'  
Skipping char: 114 0x72 'r'  
Skipping char: 101 0x65 'e'  
Skipping char: 119 0x77 'w'  
Skipping char:  97 0x61 'a'  
Skipping char: 115 0x73 's'  
35  
Skipping char: 116 0x74 't'  
Skipping char: 111 0x6f 'o'  
21  
Skipping char:  46 0x2e '.'  
Loop exit  
---------------------------------------------------------------  
Normal termination.

3 Programming Notes

This program is very short and may be put entirely in the run() function in main.cpp.

You must read x using the stream extract operator >>. You may not use stringstream or getline() or other methods to read the line as a string or to read individual digits that comprise a decimal number. You must let the stream do your decimal to binary conversion. Do not call atoi() or strtol() or any other means of manually converting a string to an int.

To obtain the ASCII code of a character stored in a char variable ch, cast ch to an int. Similarly, to print a character whose ASCII code is stored in an int variable x, cast x to a char before printing.

Grading Rubric

Your assignment will be graded according to the scale given in Figure 1 (see below).





# Pts.

Item




1. 1

All relevant standards from PS1 are followed regarding submission, identification of authorship on all files, and so forth.




2. 1

A well-formed Makefile or makefile is submitted that specifies compiler options -O1 -g -Wall -std=c++14.




3. 1

Running make successfully compiles and links the project and results in an executable file readint.




4. 1

Your program gives a usage comment and terminates if the wrong number of command line arguments are given. It gives a descriptive error comment if the specified input file does not open.




5. 4

All instructions given in sections 2 and 3 are carefully followed.




6. 4

Your program correctly extracts all of the integers in the file.




7. 4

Your program prints a correct “Skipping…” message following each failed attempt to read an integer.




8. 2

The “Skipping…” message exactly follows the examples and instructions, including spacing and when to print leading 0’s and when not to.




9. 2

Your program correctly handles end-of-file, regardless of whether the EOF is immediately preceded by whitespace, a digit, or another character.







20

Total points.





Figure 1: Grading rubric.