Академический Документы
Профессиональный Документы
Культура Документы
You have by now already completed assignment 1 which has prepared you for
writing simple programs individually involving selections, nested loops, functio
n calling, and basic IO manipulations. This assignment 2 aims to assemble all th
ose basic skills you acquired through the exercises in assignment 1, along with
the additional knowledge on the use of arrays to be learnt during the later half
of the semester, to create a concerted piece of computer program.
PART-I: (12 marks)
Basic Program Requirements
You are required to design a program bookPrice.cpp that will be used to ke
ep track of the book price for a small bookstore. This program will be menu driv
en, and the user should be able to interactively enter the title and price for a
ny book, display the price for a given book, and do the listing of all the books
and their corresponding prices. For simplicity, we assume that no two different
books will ever have the same title.
For the basic part of this program, that is, for the fulfilment of the bas
ic functionalities of this program, students are asked to follow exactly the fol
lowing steps to achieve it. For the later advanced features, however, students w
ill have more freedom in their program design. For simplicity, we always assume
that the program will be run under Microsoft Windows.
1. Recall that system("pause"); will hold the execution until a key is
pressed. Likewise system("cls"); will clear the current output screen. Write a C
++ function pause( ) prototyped by
void pause( );
such that running it will hold the program execution until a key is
pressed. Similarly write a C++ function clrscr( ) prototyped by
void clrscr( );
such that running it will just clear the whole output screen.
2. In order to easily read a line of text, like a book title, it is mor
e convenient to first remove any potential leftover blanks or newline characters
, still left in the input buffer, by calling the C++ function skipBlanks( ) prot
otyped by
void skipBlanks();
This function is actually given to you explicitly below, although yo
u are most welcome to write you own. Extend (or rewrite your own if you wish) th
e following driver program test2.cpp such that it first reads in a book title an
d then displays the title, and it then reads in a book price and also displays i
t afterwards.
#include<iostream>
using namespace std;
void clrscr()
{
// your code here, as done in the previous step
}
void pause()
{
// your code here, as done in the previous step
}
void skipBlanks() {
char ch;
// skip the following white spaces in the input buffer
while((ch=cin.peek())==' ' || ch=='\t'
|| ch=='\n' || ch=='\r') cin.get();
}
int main()
{
string title, id;
double price;
clrscr();
cout << "Enter your student ID: ";
cin >> id;
cout << "Enter a book title: ";
// what happens if the next statement is deleted?
skipBlanks(); // this may have to precede immediately the "getline
"
getline(cin, title);
cout << "[" << title << "]\n";
// your code here
pause();
return 0;
}