0% found this document useful (0 votes)
67 views10 pages

The Mailing List Application: Example

This document provides the source code for a C++ program that manages a mailing list. The program allows the user to enter, edit, maintain, and print names and addresses from the mailing list. When run, it presents a menu of options to guide the user through adding, changing, and viewing contacts. The code includes comments explaining how it implements concepts from the C++ textbook and suggesting possible improvements.

Uploaded by

Aaron Brown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views10 pages

The Mailing List Application: Example

This document provides the source code for a C++ program that manages a mailing list. The program allows the user to enter, edit, maintain, and print names and addresses from the mailing list. When run, it presents a menu of options to guide the user through adding, changing, and viewing contacts. The code includes comments explaining how it implements concepts from the C++ textbook and suggesting possible improvements.

Uploaded by

Aaron Brown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

737

EXAMPLE
C+ + By
F
The Mailing List
Applic ation
This appendix shows a complete program that contains most the
commands and functions you learned in this book. This program
manages a mailing list for your personal or business needs.
When you run the program, you are presented with a menu
of choices that guides you through the programs operation.
Comments throughout the program offer improvements you
might want to make. As your knowledge and practice of C++
improve, you might want to expand this mailing list application
into a complete database of contacts and relatives.
Here is the listing of the complete program:
/ / Fi l ename: MAI LI NG. CPP
/ / * Mai l i ng Li st Appl i cat i on *
/ / - - - - - - - - - - - - - - - - - - - - - - - -
/ /
/ / Thi s pr ogr amenabl es t he user t o ent er , edi t , mai nt ai n, and
/ / pr i nt a mai l i ng l i st of names and addr esses.
/ /
/ / Al l commands and concept s i ncl uded i n t hi s pr ogr amar e
/ / expl ai ned t hr oughout t he t ext of C++ By Exampl e.
Appendix F o The M ailing List Application
738
/ /
/ /
/ /
/ / These ar e i t ems you mi ght want t o add or change:
/ / 1. Fi nd your compi l er s cl ear scr een f unct i on t o
/ / i mpr ove upon t he scr een- cl ear i ng f unct i on.
/ / 2. Add an ent r y f or t he code member t o t r ack di f f er ent
/ / t ypes of names and addr esses ( i . e. , busi ness codes,
/ / per sonal codes, et c. )
/ / 3. Sear ch f or a par t i al name ( i . e. , t ypi ng Sm f i nds
/ / Smi t h and Smi t t y and Smyt he i n t he f i l e) .
/ / 4. When sear chi ng f or name mat ches, i gnor e case ( i . e. ,
/ / t ypi ng smi t h f i nds Smi t h i n t he f i l e) .
/ / 5. Pr i nt mai l i ng l abel s on your pr i nt er .
/ / 6. Al l ow f or sor t i ng a l i st i ng of names and addr ess by name
/ / or ZI P code.
/ / Header f i l es used by t he pr ogr am:
#i ncl ude <coni o. h>
#i ncl ude <ct ype. h>
#i ncl ude <f st r eam. h>
#i ncl ude <i ost r eam. h>
#i ncl ude <st r i ng. h>
const char FI LENAME[ ] = ADDRESS. DAT;
/ / Pr ot ot ype al l of t hi s pr ogr am s f unct i ons.
char get _answer ( voi d) ;
voi d di sp_menu ( voi d) ;
voi d cl ear _sc ( voi d) ;
voi d change_na ( voi d) ;
voi d pr i nt _na ( voi d) ;
voi d er r _msg ( char er r _msg[ ] ) ;
voi d pause_sc ( voi d) ;
const i nt NAME_SI ZE = 25;
const i nt ADDRESS_SI ZE = 25;
const i nt CI TY_SI ZE = 12;
739
EXAMPLE
C+ + By
const i nt STATE_SI ZE = 3;
const i nt ZI PCODE_SI ZE = 6;
const i nt CODE_SI ZE = 7;
/ / Cl ass of a name and addr ess
cl ass Mai l
{
pr i vat e:
char name[ NAME_SI ZE] ; / / Name st or ed her e, shoul d
/ / be Last , Fi r st or der
char addr ess[ ADDRESS_SI ZE] ;
char ci t y[ CI TY_SI ZE] ;
char st at e[ STATE_SI ZE] ; / / Save r oomf or nul l zer o.
char zi pcode[ ZI PCODE_SI ZE] ;
char code[ CODE_SI ZE] ; / / For addi t i onal expansi on. You
/ / mi ght want t o use t hi s member
/ / f or cust omer codes, vendor codes,
/ / or hol i day car d codes.
publ i c:
voi d pr _dat a( Mai l *i t em)
{
/ / Pr i nt s t he name and addr ess sent t o i t .
cout << \ nName : << ( *i t em) . name << \ n;
cout << Addr ess: << ( *i t em) . addr ess << \ n;
cout << Ci t y : << ( *i t em) . ci t y << \ t St at e:
<< ( *i t em) . st at e << Zi pcode: << ( *i t em) . zi pcode
<< \ n;
}
voi d get _new_i t em( Mai l *i t em)
{
Mai l t emp_i t em; / / Hol ds t empor ar y changed i nput .
cout << \ nEnt er new name and addr ess i nf or mat i on bel ow\ n( Pr ess t he ;
cout << Ent er key wi t hout t ypi ng dat a t o r et ai n ol d "
i nf or mat i on) \ n\ n;
cout << What i s t he new name? ;
ci n. get l i ne( t emp_i t em. name, NAME_SI ZE) ;
i f ( st r l en( t emp_i t em. name) ) / / Onl y save new dat a i f user
{ st r cpy( ( *i t em) . name, t emp_i t em. name) ; } / / t ypes somet hi ng.
cout << What i s t he addr ess? ;
ci n. get l i ne( t emp_i t em. addr ess, ADDRESS_SI ZE) ;
Appendix F o The M ailing List Application
740
i f ( st r l en( t emp_i t em. addr ess) )
{ st r cpy( ( *i t em) . addr ess, t emp_i t em. addr ess) ; }
cout << What i s t he ci t y? ;
ci n. get l i ne( t emp_i t em. ci t y, CI TY_SI ZE) ;
i f ( st r l en( t emp_i t em. ci t y) )
{ st r cpy( ( *i t em) . ci t y, t emp_i t em. ci t y) ; }
cout << What i s t he st at e? ( 2 l et t er abbr evi at i on onl y) ;
ci n. get l i ne( t emp_i t em. st at e, STATE_SI ZE) ;
i f ( st r l en( t emp_i t em. st at e) )
{ st r cpy( ( *i t em) . st at e, t emp_i t em. st at e) ; }
cout << What i s t he ZI P code? ;
ci n. get l i ne( t emp_i t em. zi pcode, ZI PCODE_SI ZE) ;
i f ( st r l en( t emp_i t em. zi pcode) )
{ st r cpy( ( *i t em) . zi pcode, t emp_i t em. zi pcode) ; }
( *i t em) . code[ 0] = 0; / / Nul l out t he code member
/ / ( unused her e) .
}
voi d add_t o_f i l e( Mai l *i t em) ;
voi d change_na( voi d) ;
voi d ent er _na( Mai l *i t em) ;
voi d get zi p( Mai l *i t em) ;
};
voi d Mai l : : change_na( voi d)
{
/ / Thi s sear ch f unct i on can be i mpr oved by usi ng t he
/ / code member t o assi gn a uni que code t o each per son on t he
/ / l i st . Names ar e di f f i cul t t o sear ch f or si nce t her e ar e
/ / so many var i at i ons ( such as Mc and Mac and St . and Sai nt ) .
Mai l i t em;
f st r eamf i l e;
i nt ans;
i nt s; / / Hol ds si ze of st r uct ur e.
i nt change_yes = 0; / / Wi l l become TRUE i f user f i nds
char t est _name[ 25] ; / / a name t o change.
cout << \ nWhat i s t he name of t he per son you want t o change? ;
ci n. get l i ne( t est _name, NAME_SI ZE) ;
s = si zeof ( Mai l ) ; / / To ensur e f r ead( ) r eads pr oper l y.
741
EXAMPLE
C+ + By
f i l e. open( FI LENAME, i os: : i n | i os: : out ) ;
i f ( ! f i l e)
{
er r _msg( *** Read er r or - Ensur e f i l e exi st s bef or e "
" r eadi ng i t ***) ;
r et ur n;
}
do
{
f i l e. r ead( ( unsi gned char *) &i t em, si zeof ( Mai l ) ) ;
i f ( f i l e. gcount ( ) ! = s)
{
i f ( f i l e. eof ( ) )
{ br eak; }
}
i f ( st r cmp( i t em. name, t est _name) == 0)
{
i t em. pr _dat a( &i t em) ; / / Pr i nt name and addr ess.
cout << \ nI s t hi s t he name and addr ess t o <<
change? ( Y/ N) ;
ans = get _answer ( ) ;
i f ( t oupper ( ans) == N )
{ br eak; } / / Get anot her name.
get _new_i t em( &i t em) ; / / Enabl e user t o t ype new
/ / i nf or mat i on.
f i l e. seekg( ( l ong) - s, i os: : cur ) ; / / Back up a st r uct ur e.
f i l e. wr i t e( ( const unsi gned char *) ( &i t em) ,
si zeof ( Mai l ) ) ; / / Rewr i t e i nf or mat i on.
change_yes = 1; / / Changed f l ag.
br eak; / / Fi ni shed
}
}
whi l e ( ! f i l e. eof ( ) ) ;
i f ( ! change_yes)
{ er r _msg( *** End of f i l e encount er ed bef or e f i ndi ng t he name ***) ; }
}
voi d Mai l : : get zi p( Mai l *i t em) / / Ensur e t hat ZI P code
/ / i s al l di gi t s.
{
i nt ct r ;
Appendix F o The M ailing List Application
742
i nt bad_zi p;
do
{
bad_zi p = 0;
cout << What i s t he ZI P code? ;
ci n. get l i ne( ( *i t em) . zi pcode, ZI PCODE_SI ZE) ;
f or ( ct r = 0; ct r < 5; ct r ++)
{
i f ( i sdi gi t ( ( *i t em) . zi pcode[ ct r ] ) )
{ cont i nue; }
el se
{
er r _msg( *** The ZI P code must consi st of di gi t s onl y ***) ;
bad_zi p = 1;
br eak;
}
}
}
whi l e ( bad_zi p) ;
}
voi d Mai l : : add_t o_f i l e( Mai l *i t em)
{
of st r eamf i l e;
f i l e. open( FI LENAME, i os: : app) ; / / Open f i l e i n append mode.
i f ( ! f i l e)
{
er r _msg( *** Di sk er r or - pl ease check di sk dr i ve ***) ;
r et ur n;
}
f i l e. wr i t e( ( const unsi gned char *) ( i t em) , si zeof ( Mai l ) ) ;
/ / Add st r uct ur e t o f i l e.
}
voi d Mai l : : ent er _na( Mai l *i t em)
{
char ans;
743
EXAMPLE
C+ + By
do
{
cout << \ n\ n\ n\ n\ nWhat i s t he name? ;
ci n. get l i ne( ( *i t em) . name, NAME_SI ZE) ;
cout << What i s t he addr ess? ;
ci n. get l i ne( ( *i t em) . addr ess, ADDRESS_SI ZE) ;
cout << What i s t he ci t y? ;
ci n. get l i ne( ( *i t em) . ci t y, CI TY_SI ZE) ;
cout << What i s t he st at e? ( 2 l et t er abbr evi at i on onl y) ;
ci n. get l i ne( ( *i t em) . st at e, STATE_SI ZE) ;
get zi p( i t em) ; / / Ensur e t hat ZI P code i s al l di gi t s.
st r cpy( ( *i t em) . code, ) ; / / Nul l out t he code member .
add_t o_f i l e( i t em) ; / / Wr i t e new i nf or mat i on t o di sk f i l e.
cout << \ n\ nDo you want t o ent er anot her name <<
and addr ess? ( Y/ N) ;
ans = get _answer ( ) ;
}
whi l e ( t oupper ( ans) == Y ) ;
}
/ / ************************************************************
/ / Def i ned const ant s
/ / MAX i s t ot al number of names al l owed i n memor y f or
/ / r eadi ng mai l i ng l i st .
const i nt MAX = 250;
const char BELL = \ x07 ;
/ / ************************************************************
i nt mai n( voi d)
{
char ans;
Mai l i t em;
do
{
di sp_menu( ) ; / / Di spl ay t he menu f or t he user .
ans = get _answer ( ) ;
swi t ch ( ans)
{
case 1 :
Appendix F o The M ailing List Application
744
i t em. ent er _na( &i t em) ;
br eak;
case 2 :
i t em. change_na( ) ;
br eak;
case 3 :
pr i nt _na( ) ;
br eak;
case 4 :
br eak;
def aul t :
er r _msg( *** You have t o ent er 1 t hr ough 4 ***) ;
br eak;
}
}
whi l e ( ans ! = 4 ) ;
r et ur n 0;
}
/ / ************************************************************
voi d di sp_menu( voi d) / / Di spl ay t he mai n menu of pr ogr am.
{
cl ear _sc( ) ; / / Cl ear t he scr een.
cout << \ t \ t *** Mai l i ng Li st Manager ***\ n;
cout << \ t \ t - - - - - - - - - - - - - - - - - - - - \ n\ n\ n\ n;
cout << Do you want t o: \ n\ n\ n;
cout << \ t 1. Add names and addr esses t o t he l i st \ n\ n\ n;
cout << \ t 2. Change names and addr esses i n t he l i st \ n\ n\ n;
cout << \ t 3. Pr i nt names and addr esses i n t he l i st \ n\ n\ n;
cout << \ t 4. Exi t t hi s pr ogr am\ n\ n\ n;
cout << What i s your choi ce? ;
}
/ / ************************************************************
voi d cl ear _sc( ) / / Cl ear t he scr een by sendi ng 25 bl ank
/ / l i nes t o i t .
{
i nt ct r ; / / Count er f or t he 25 bl ank l i nes.
f or ( ct r = 0; ct r < 25; ct r ++)
745
EXAMPLE
C+ + By
{ cout << \ n; }
}
/ / ************************************************************
voi d pr i nt _na( voi d)
{
Mai l i t em;
i f st r eamf i l e;
i nt s;
i nt l i nect r = 0;
s = si zeof ( Mai l ) ; / / To ensur e f r ead( ) r eads pr oper l y.
f i l e. open( FI LENAME) ;
i f ( ! f i l e)
{
er r _msg( *** Er r or - Ensur e f i l e exi st s bef or e"
" r eadi ng i t ***) ;
r et ur n;
}
do
{
f i l e. r ead( ( si gned char *) &i t em, s) ;
i f ( f i l e. gcount ( ) ! = s)
{
i f ( f i l e. eof ( ) ) / / I f EOF, qui t r eadi ng.
{ br eak; }
}
i f ( l i nect r > 20) / / Scr een i s f ul l .
{
pause_sc( ) ;
l i nect r = 0;
}
i t em. pr _dat a( &i t em) ; / / Pr i nt t he name and addr ess.
l i nect r += 4;
}
whi l e ( ! f i l e. eof ( ) ) ;
cout << \ n- End of l i st - ;
pause_sc( ) ; / / Gi ve user a chance t o see names
/ / r emai ni ng on- scr een.
}
Appendix F o The M ailing List Application
746
/ / ************************************************************
voi d er r _msg( char er r _msg[ ] )
{
cout << \ n\ n << er r _msg << BELL << \ n;
}
/ / ************************************************************
voi d pause_sc( )
{
cout << \ nPr ess t he Ent er key t o cont i nue. . . ;
whi l e ( get ch( ) ! = \ r )
{ ; } / / Wai t f or Ent er key.
}
/ / ************************************************************
char get _answer ( voi d)
{
char ans;
ans = get ch( ) ;
whi l e ( kbhi t ( ) )
{ get ch( ) ; }
put ch( ans) ;
r et ur n ans;
}

You might also like