Вы находитесь на странице: 1из 7

ASSIGNMENT TWO 15 marks

Due Friday 4/6/2010 at 5pm


available since 27/04, last updated 19/04

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;
}

to see if the program behaves as expected.


3. Write the C++ function displayMenu( ) prototyped by
void displayMenu();
such that its execution just displays the following menu items
Book Price (student Id and name) ver 1.0
MAIN MENU
0. Exit 1. Enter book 2. Find book
3. List all 4. Delete book 5. Update book
Your choice ->
Write a driver program test3.cpp whose execution will first clear th
e screen, then display the above main menu, then prompt the user to enter a menu
selection, and then display the confirmation on the user's selection before exi
ting the program. For the confirmation, if the user has entered "1" (without the
double quotes) for the menu selection, for example, the program could display s
omething like "Selected: Enter book".
4. Write the C++ function listRecords( ) prototyped by
void listRecords(string allTitles[], double allPrices[], int totalRe
c);
This function will display totalRec pairs of book title and book pri
ce, to be extracted from the paired arrays allTitles and allPrices (as the first
totalRec elements there). You are welcome to make use of the following structur
e if you don't wish to write your own completely.
void listRecords(string allTitles[], double allPrices[], int totalRe
c)
{
if(totalRec <= 0)
cout << "* No book titles are available.\n";
else
{
cout << "\
*** ALL AVAILABLE BOOK TITLES ***\n\n\
TITLE PRICE ($)\n\
-----------------------------------------\n";
// your code here:
// use a "for" loop to display all the book titles in the allTit
les array
// and their corresponding prices in the allPrices array
}
}
Write a driver program test4.cpp such that in the main function ther
e, an array allTitles of string elements is declared. This array should contain
"Book 1" and "Book 2" as the first two elements, and the array should be of the
size MAXSIZE, a constant set to 300. Likewise declare an array allPrices of doub
le data type, containing 78.5 and 66 as its first two elements. The size of allP
rices is again MAXSIZE.
This driver program will simply display all the book titles and thei
r corresponding book prices (contained in the paired arrays allTitles and allPri
ces). It helps to declare an int variable totalRec (initialised to 2 in this cas
e because there are just 2 books initially in each of the arrays) to keep track
of how many legitimate elements are currently contained in the arrays allTitles
and allPrices.
5. Write the C++ function findTitlePrice( ) prototyped by
bool findTitlePrice(string allTitles[], double allPrices[],
int totalRec, string title, double & price);
This function will search the array allTitles of totalRec elements t
o see if it contains the given title. If no match is found, then the function sh
ould return the boolean value false. Otherwise, it should locate the correspondi
ng price from the allPrices array and pass it back to the function's caller via
the function parameter price, and the function should return the boolean value t
rue in this case.
We note that allTitles and allPrices are paired arrays. Hence if a b
ook title is stored at for instance position pos of the array allTitles, i.e. if
the book title is represented by allTitles[pos], then the corresponding book pr
ice would be stored at position pos of the array allPrices, i.e. the correspondi
ng book price would be stored as allPrices[pos]. You are welcome to make use of
the following structure if you don't wish to write your own completely.
bool findTitlePrice(string allTitles[], double allPrices[],
int totalRec, string title, double & price)
{
for(int i=0; i < totalRec; i++)
{
// your code here:
// for each element of array allTitles, check if it matches
// the given title, i.e. if title and allTitles[i] are the same;
// if yes, fetch the corresponding price from
// the array allPrices and exit the function (returned value: tr
ue)
}
// your code here:
// what value should you return here?
}
Write a driver program test5.cpp such that in the main function ther
e, an array allTitles of string elements is declared. This array should contain
"Book 1" and "Book 2" as the first two elements, and the array should be of the
size MAXSIZE, a constant set to 300. Likewise declare an array allPrices of doub
le data type, containing 78.5 and 66 as its first two elements. The size of allP
rices is again MAXSIZE.
This driver program will prompt the user to enter a book title (the
user may enter for instance "Book 3" here), then use findTitlePrice function to
determine if the entered book title is found in the array allTitles. Display the
book title and the corresponding price if a match is found, otherwise display a
n appropriate not-found message. It helps to declare an int variable totalRec (i
nitialised to 2 in this case because there are just 2 books in the arrays) to ke
ep track of how many legitimate elements are currently contained in the arrays a
llTitles and allPrices. Also when reading a book title, it may be handy to make
use of getline(cin, title).
6. Write the C++ function displayRecord( ) prototyped by
void displayRecord(string title, string allTitles[],
double allPrices[], int totalRec)
This function will search the first totalRec elements of the array a
llTitles to see if any book title there matches the given title (through the use
of above findTitlePrice function). If a match is found, i.e. function findTitle
Price returns the value true, then display both the book title and the correspon
ding price. Otherwise, display an appropriate not-found message.
Write a driver program test6.cpp which is initially the same as test
5.cpp. Modify it so that it displays the title and price for the found/matched b
ook using the above function displayRecord.
7. (2 marks) Write the C++ function readRecord( ) prototyped by
bool readRecord(string allTitles[], double allPrices[],
string & title, int &totalRec);
This function will prompt and input for a book title and its corresp
onding price (you may want to again make use of the function getline(cin, title)
). If the input is successful and the book is not already in the arrays allTitl
es and allPrices, then store the title in allTitles (at the next available posit
ion totalRec) and likewise store the price in allPrices. The entered new book ti
tle will be passed back to the caller via the parameter title and the array size
totalRec will also be passed back after the update. The function will then retu
rn the boolean value true. The function will not modify anything if the input is
not successful or if the book title is already in the array allTitles, and the
function will return the boolean value false in this case. You are welcome to ma
ke use of the following structure if you don't wish to write your own completely
.
bool readRecord(string allTitles[], double allPrices[],
string & title, int &totalRec)
{
if(totalRec < MAXSIZE)
{
cout << "\nEnter a Book Record:\nBook Title ->";
skipBlanks();
if(getline(cin,title))
{
// your code here:
// check if title is already in the array allTitles,
// if not, input the corresponding book price and store
// both the title and the price in the array allTitles and
// allPrices respectively, and then return the value true
} // otherwise: invalid book title
} // otherwise: capacity exceeded due to MAXSIZE
return false; // aborted
}
Write a driver program test7.cpp which is initially the same as test
5.cpp. Modify it so that it will try (once) to read in a book title and its corr
esponding price, and have them stored in the arrays allTitles and allPrices resp
ectively. If this is successful, then display the newly read book title and its
price (fetched directly from the arrays); if not, display the message "Aborted:
book entry failed.".
8. (2 marks) Write the driver program bookPrice.cpp, incorperating all
the above features and points. In particular, this program will
o display the menu
o allow a user to enter a new book title and its price (menu sel
ection 1)
o allow a user to locate the book via its title and display the
book title and price if a match is found (menu selection 2)
o allow a user to list all book titles paired with their corresp
onding prices (menu selection 3)
o continue to display menu for the next menu selection until "Ex
it" (menu selection 0) is chosen
o contain those two book titles and the corresponding prices (as
mentioned in the above point 5) before the menu is first displayed.
9. Draw the structure diagram that reflects your complete program bookP
rice.cpp you did in the previous point.
10. Overall quality on achieving the above requirements.
Unless otherwise stated, the above program requirements each account for 1
mark. If any of the C++ programs test2.cpp to test7.cpp does not get submitted,
then the corresponding functionality will be examined throught the main program
bookPrice.cpp.
PART-II: (3 marks)
Advanced Features
The purpose of this part is to extend and polish that basic program descri
bed in Part I. The functionality of this extended C++ program, xBookPrice.cpp, s
hould contain the following additional features.
11. (1 mark) Corresponding to the main menu
MAIN MENU
0. Exit 1. Enter book 2. Find book
3. List all 4. Delete book 5. Update book
the new menu item Delete book will get a book title from the user an
d delete the corresponding title and price entries. Also, whenever a book title
is entered, its leading and trailing blanks will be removed automatically.
12. (1 mark) The new menu item Update book will first get a book title a
nd the book price from the user. If the book title has already got a record in t
he program (i.e. read by the program earlier on, but not subsequentally deleted)
, then the newly entered price will replace the previous price corresponding to
the book title. This is essentially an update of the book price. Otherwise, the
program simply stores the book title and the book price similar to the functiona
lity of Enter book specified earlier on.
13. (1 mark) The program should be robust and should never crash. It sho
uld issue appropriate messages on whether a request has been successful or what
has actually been done as a result. For instance, if the user aborts his data en
try while trying to enter a student record, the program should acknowledge that.
In short, the program should be informative of its behaviour and should achieve
a sensible user interfacing.
Some Screenshots
Below is a list of screen shots of running a possible basic implementation
. This is just one illustration, and the interface design of your program may we
ll be completely different.
Book Price (Z Jiang) ver 1.0 1/1/2010
MAIN MENU
0. Exit 1. Enter book 2. Find book
3. List all 4. Delete book 5. Update book
Your choice ->1
Enter a Book Record:
Book Title ->Phantom of the opera
Book Price ->123.45

Press any key to continue . . .


Book Price (Z Jiang) ver 1.0 1/1/2010
MAIN MENU
0. Exit 1. Enter book 2. Find book
3. List all 4. Delete book 5. Update book
Your choice ->3
*** ALL AVAILABLE BOOK TITLES ***
TITLE PRICE ($)
-----------------------------------------
Book 1 78.50
Book 2 66.00
Phantom of the opera 123.45

Press any key to continue . . .


Note on Submission
o This assignment must be submitted electronically via vUWS before the
due date. No email submissions will be accepted.
o It is the students' responsibility to make sure that they keep a cop
y of the submission receipt for the whole semester. Once an electronic submissio
n is successfully made via the submission page, clicking the receipts button the
re will display the receipt. Just copy/paste it to a plain text file and save it
as the submission receipt. Students are also advised to enter their email addre
ss in the EMAIL field so that the receipt will also be emailed to the provided a
ddress. However please beware that you can not completely depend on this to retr
ieve the receipt, and the submission process can not report on an incorrect emai
l address you may have entered.
o Submitted files may be zipped together as a single zip file (but not
as a zipx file), if a student wishes to do so. However, no other file compressi
on or file archiving formats will be accepted for the submission.
o All C++ programs must be compilable under Dev-C++ ver 4.9.9.2, or mo
re precisely, under g++ 3.4.2 (mingw-special).
o The electronic submission should contain the paper work in plain tex
t or in Microsoft Word format (must be readable on the School's lab computers),
and the C++ source code (the .cpp file(s)) as well as other relevant files if an
y. Please do not include the compiled executables as the markers will generate t
hem for you independently anyway.
o Each submission must be accompanied by a declaration of the ownershi
p of the submitted work as described in the unit outline and learning guide. Ple
ase note that an examiner or lecturer/tutor has the right not to mark this assig
nment if a pertinent declaration is not present in your submission.
o Late submissions will attract a daily incremented late penalty of 10
% per day.
o Please note that if your C++ program does not compile, you automatic
ally lose 50% of the marks in the C++ coding part.
o Electronic submission on the due date after 5pm before 12 midnight w
ill still be accepted without penalty. However, any submission failure in that p
eriod due to either the student faults or the fault or malfunction of the School
's or UWS' servers will not be accepted as the legitimate reasons for a late sub
mission. Beware that School's servers often need to be shut down for maintenance
from late Fridays or just before public holidays.

Вам также может понравиться