This document is part of the HTML publication "An Introduction to the Imperative Part of C++"

The original version was produced by Rob Miller at Imperial College London, September 1996.

Version 1.1 (modified by David Clark at Imperial College London, September 1997)

Version 1.2 (modified by Bob White at Imperial College London, September 1998)

Version 1.3, 1.4, 2.0, ..., 2.16 (modified by William Knottenbelt at Imperial College London, September 1999-September 2017)


Introduction to C++ Programming: Exercise Sheet 4

Question 1

Write a program which outputs its own C++ source file to the screen.

(EXAMPLE ANSWER) (BACK TO COURSE CONTENTS)

Question 2

Write a program that (i) starts with the output test statement:

	cout << "Testing: " << 16/2 << " = " << 4*2 << ".\n\n"; 

and then (ii) outputs its own C++ source file to a file called "WithoutComments.cpp" and to the screen, but omitting any of the comments enclosed in "/* ... */" markers (and omitting the markers themselves). The new program in the file "WithoutComments.cpp" should compile and run in exactly the same way as the program from which it was generated.

Hints:
(i) you might find the "putback()" function useful,
(ii) you might want to use the idea of a "flag" to keep track of whether you're inside a comment or not.

(EXAMPLE ANSWER) (BACK TO COURSE CONTENTS)

Question 3

Write a program which counts and displays the number of characters (including blanks) in its own source code file.

(EXAMPLE ANSWER) (BACK TO COURSE CONTENTS)

Question 4

Without using an array (we will learn about arrays in Lecture 6), write a program that prints itself out backwards on the screen.

Hints:
(i) the answer to question 3 might help at the start of your program,
(ii) be careful not to re-use an input stream once an operation has failed on it - use a new stream instead.

Remark: don't worry if your program takes a while before it prints to the screen - opening and closing files takes time.

(EXAMPLE ANSWER) (BACK TO COURSE CONTENTS)

Question 5

What screen output does the following program produce, and why?

	#include <iostream>
	#include <fstream>

	using namespace std;

	int main()
	{
		char character;
		int integer;
		ofstream out_stream;
		ifstream in_stream;
	
		/* Create a file containing two integers */
		out_stream.open("Integers");
		out_stream << 123 << ' ' << 456;
		out_stream.close();
	
		/* Attempt to read a character, then an integer,
		   then a character again, then an integer again,
		   from the file "Integers" just created.         */
		in_stream.open("Integers");
		in_stream >> character >> integer;
		cout << "character: '" << character << "'\n";
		cout << "integer: " << integer << "\n";
		in_stream >> character >> integer;
		cout << "character: '" << character << "'\n";
		cout << "integer: " << integer << "\n";
		in_stream.close();
	
		return 0;
	}

(EXAMPLE ANSWER) (BACK TO COURSE CONTENTS)