Home Forums C Programming inventory mngmt in C Re: Re: inventory mngmt in C

#3487
GWILouisaxwzkla
Participant

Not sure exactly what you need , but here is something similar with an array of structs and a library inventory:


/****************************************************************
* File Name : c:programshelpshell.cpp
* Date : May,14,2007
* Comments : new project
* Compiler/Assembler :
*
*
*
*
*
* Program Shell Generated At: 1:40:06 p.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


#include
#include
//#include
//#include
//#include
//#include

using namespace std;

struct PERSON
{

string firstName;
string lastName;
string address;
string phoneNumber;
};


//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@

void addPerson ( PERSON & thisPerson );
void getPerson ( PERSON & thisPerson );
bool findPerson ( string & firstName );
bool findPerson ( string & firstName , string & lastName );

void printBook ( void );
bool checkDuplicate ( PERSON & personToCheck );
bool insertAfter ( PERSON & personToAdd , int index );


//##################################################################################


//>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<
const int MAX_PEOPLE = 3;
PERSON addressBook [ MAX_PEOPLE ] ;
int arrayIndex;
int itemsInAddressBook = 0;
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


//main function ******************************

int main ( )
{

arrayIndex = 0;
PERSON temp;
char choice;
string firstName , lastName;

do
{
cout << endl;
cout << "menu ***********" << endl;
cout << "1. add person" << endl;
cout << "2. get person " << endl;
cout << "3. find person ( full name ) " << endl;
cout << "4. find person ( first name ) " << endl;
cout << "5. print address book " << endl;
cout << "6. insert after another item " << endl;
cout << "7. quit" << endl;

cout << endl << "enter choice ";


cin >> choice;
while ( choice != '1' && choice != '2' && choice != '3' && choice != '4' && choice != '5'
&& choice != '6' && choice != '7' )
{
cout << "Bad choice ( 1,2,3,4,5,6,7 ) , enter choice : ";
cin >> choice;
}
switch ( choice )
{
case '1':
{
cout << endl;
cout << "enter first name: ";
cin >> temp.firstName;
cout << "enter last name: ";
cin >> temp.lastName;
cout << "enter address ( $ to end input ): ";
cin.get();
getline ( cin , temp.address , '$' );
cout << "enter phone number: ";
cin >> temp.phoneNumber;
addPerson ( temp );
};
break;
case '2':
{
getPerson ( temp );
cout << endl;
cout << "name retrieved :";
cout << "first name : " << temp.firstName << endl;
cout << "last name : " << temp.lastName << endl;
cout << "address : " << temp.address << endl;
cout << "phone number : " << temp.phoneNumber << endl;
};
break;
case '4':
{
cout << "enter first name: ";
cin >> firstName;
if ( findPerson ( firstName ) )
cout << "name is in the address book " << endl;
else
cout << "name is not in the address book " << endl;
};
break;
case '3':
{
cout << "enter first name: ";
cin >> firstName;
cout << "enter last name: ";
cin >> lastName;


if ( findPerson ( firstName , lastName ) )
cout << "name is in the address book " << endl;
else
cout << "name is not in the address book " << endl;
};
break;
case '5':
{
printBook ( );
};
break;
case '6':
{
cout << "enter first name: ";
cin >> temp.firstName;
cout << "enter last name: ";
cin >> temp.lastName;
cout << "enter index to enter after : ";
int index;
cin >> index;
insertAfter ( temp , index );
};
break;
};
}
while ( choice != '7' );




return 0 ;
}


/******************************* FUNCTION DEFINITION ******************************

Name : addPerson
Parameters :

thisPerson a(n) PERSON & ( PERSON & )


Returns: Void type
Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void addPerson ( PERSON & thisPerson )
{



if ( arrayIndex >= MAX_PEOPLE )
arrayIndex = 0;

if ( itemsInAddressBook < MAX_PEOPLE )
itemsInAddressBook ++;

if ( checkDuplicate ( thisPerson ) == true )
{
cout << endl << "item already in data base ! " << endl;
return;
}


addressBook [ arrayIndex ].firstName = thisPerson.firstName;
addressBook [ arrayIndex ].lastName = thisPerson.lastName;
addressBook [ arrayIndex ].address = thisPerson.address;
addressBook [ arrayIndex ].phoneNumber = thisPerson.phoneNumber;

arrayIndex ++;
return;
}
/******************************* FUNCTION DEFINITION ******************************

Name : getPerson
Parameters :

thisPerson a(n) PERSON & ( PERSON & )


Returns: Void type
Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void getPerson ( PERSON & thisPerson )
{



if ( itemsInAddressBook == 0 )
{
cout << "adress book empty" << endl;
return;
}

if ( arrayIndex >= MAX_PEOPLE )
arrayIndex = 0;


thisPerson.firstName = addressBook [ arrayIndex ].firstName;
thisPerson.lastName = addressBook [ arrayIndex ].lastName ;
thisPerson.address = addressBook [ arrayIndex ].address;
thisPerson.phoneNumber = addressBook [ arrayIndex ].phoneNumber;
arrayIndex ++;
if ( arrayIndex >= MAX_PEOPLE )
arrayIndex = 0;
if ( itemsInAddressBook < MAX_PEOPLE )
itemsInAddressBook ++;

return;
}
/******************************* FUNCTION DEFINITION ******************************

Name : findPerson
Parameters :

firstName a(n) string & ( string & )


Returns: user defined type , bool

Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool findPerson ( string & firstName )
{
int i = 0;

while ( i < itemsInAddressBook )
{
if ( firstName == addressBook [ i ].firstName )
return true;
i ++;
}
return false;
}
/******************************* FUNCTION DEFINITION ******************************

Name : findPerson
Parameters :

firstName a(n) string & ( string & ) ,
lastName a(n) string & ( string & )


Returns: user defined type , bool

Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool findPerson ( string & firstName , string & lastName )
{

int i = 0;

while ( i < itemsInAddressBook && i < MAX_PEOPLE )
{
if ( firstName == addressBook [ i ].firstName && lastName == addressBook [ i ].lastName )
return true;
i ++;
}
return false;
}
/******************************* FUNCTION DEFINITION ******************************

Name : printBook
Parameters :

void ( void )


Returns: Void type
Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void printBook ( void )
{
int i = 0;
while ( i < itemsInAddressBook && i < MAX_PEOPLE )
{
cout << i << ". " << addressBook [ i ].firstName
<< " " << addressBook [ i ].lastName
<< " " << addressBook [ i ].address
<< " " << addressBook [ i ]. phoneNumber << endl;
i ++;
}
return;
}
/******************************* FUNCTION DEFINITION ******************************

Name : checkDuplicate
Parameters :

personToCheck a(n) person & ( person & )


Returns: user defined type , bool

Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool checkDuplicate ( PERSON & personToCheck )
{

int i = 0;
while ( i < itemsInAddressBook && i < MAX_PEOPLE )
{
if ( personToCheck.firstName == addressBook [ i ].firstName
&& personToCheck.lastName == addressBook [ i ].lastName )
return true;
i ++;
}


return false;
}
/******************************* FUNCTION DEFINITION ******************************

Name : insertAfter
Parameters :

personToAdd a(n) person & ( person & ) ,
index a(n) int ( int )


Returns: user defined type , bool

Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool insertAfter ( PERSON & personToAdd , int index )
{


if ( checkDuplicate ( personToAdd ) == true )
{
cout << endl << "item already in data base ! " << endl;
return false;
}
if ( index > itemsInAddressBook )
{

addressBook [ arrayIndex ].firstName = personToAdd.firstName;
addressBook [ arrayIndex ].lastName = personToAdd.lastName;
addressBook [ arrayIndex ].address = personToAdd.address;
addressBook [ arrayIndex ].phoneNumber = personToAdd.phoneNumber;
arrayIndex ++;
if ( arrayIndex >= MAX_PEOPLE )
arrayIndex = 0;
if ( itemsInAddressBook > MAX_PEOPLE )
itemsInAddressBook ++;
}
else if ( index < itemsInAddressBook )
{

addressBook [ index + 1 ].firstName = personToAdd.firstName;
addressBook [ index + 1 ].lastName = personToAdd.lastName;
addressBook [ index + 1 ].address = personToAdd.address;
addressBook [ index + 1 ].phoneNumber = personToAdd.phoneNumber;

if ( itemsInAddressBook <= MAX_PEOPLE )
itemsInAddressBook ++;

}
return true;
}