Problems in Address book programming

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 4
Reputation: sohamghosh is an unknown quantity at this point 
Solved Threads: 0
sohamghosh sohamghosh is offline Offline
Newbie Poster

Problems in Address book programming

 
0
  #1
Apr 15th, 2008
My code for address book is as follows:
  1. #include <iostream>
  2.  
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. struct
  9.  
  10.  
  11. {
  12.  
  13. char name[101][30];
  14.  
  15. char telephone[101][8];
  16.  
  17. char email[101][20];
  18.  
  19. char address[101][50];
  20.  
  21. char postal[101][6];
  22.  
  23. }employee;
  24.  
  25.  
  26. void enter()
  27.  
  28. {
  29. int n;
  30.  
  31. cout << "Number of entries?\n";
  32.  
  33. cin >> n;
  34.  
  35. for (int i=0; i<n; i++){
  36.  
  37. gets_s (employee.name[i]);
  38. gets(
  39. employee.email[i]);
  40. gets( employee.address[i]);
  41. gets( employee.postal[i]);
  42. gets( employee.telephone[i]);
  43. gets ( employee.telephone[i]);
  44. }
  45.  
  46. }
  47.  
  48.  
  49. void printall(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8],int n)
  50.  
  51. {
  52.  
  53. for (int i=0; i<n; i++){
  54.  
  55. puts (employee.name[i]);
  56.  
  57. puts (employee.email[i]);
  58.  
  59. puts (employee.address[i]);
  60.  
  61. puts (employee.postal[i]);
  62.  
  63. puts (employee.telephone[i]);
  64.  
  65. }
  66.  
  67. }
  68.  
  69. void deleteentry(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
  70.  
  71. {
  72.  
  73. gets(name [100]);
  74.  
  75.  
  76.  
  77. for (int i=0; i<100; i++)
  78.  
  79.  
  80. { if (name[i]-'0'== name[100]-'0')
  81.  
  82. {
  83.  
  84. employee.name[i][0]='\0';
  85.  
  86. employee.email[i][0]='\0';
  87.  
  88. employee.address[i][0]='\0';
  89.  
  90. employee.postal[i][0]='\0';
  91.  
  92. employee.telephone[i][0]='\0';
  93.  
  94. }
  95.  
  96. }
  97. }
  98. void update(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
  99. {
  100. gets(name[100]);
  101. int i;
  102. for (i=0; i<100; i++)
  103. { if (name[i]-'0'==name[100]-'0')
  104. deleteentry(name,email,address,postal,telephone);
  105. enter(i);
  106. }
  107. }
  108.  
  109. void find(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
  110. {
  111.  
  112. gets(name[100]);
  113.  
  114. for (int i=0; i<30; i++)
  115.  
  116. {if (strcmp(name[i], name[100])==0)
  117.  
  118. {cout << i << "\n";
  119.  
  120. }
  121.  
  122. }
  123. };
  124.  
  125. void sort(char name[101][30],char email[101][20],char address[101][50],char postal[101][6],char telephone[101][8])
  126. {
  127. bool sorted;
  128.  
  129. char temp;
  130.  
  131. int limit=101, last;
  132.  
  133. while (!sorted)
  134.  
  135. {
  136.  
  137. sorted = true;
  138. for
  139. (int i = 0; i < limit; i++)
  140.  
  141. {
  142.  
  143. if(name[i][0]-'0' > name[i+1]-'0')
  144.  
  145. {
  146.  
  147. sorted = false;
  148.  
  149. temp = name[i][0];
  150.  
  151. name[i][0] = name[i+1][0];
  152.  
  153. name[i+1][0] = temp;
  154.  
  155. last = i;
  156.  
  157. }
  158.  
  159. }
  160. limit = last;
  161. }
  162. }
  163. void menu ()
  164. {
  165. int i,n;
  166. while (cin >> i, i>0){
  167. switch (i)
  168. {
  169. case 1: cin >> n; enter(); break;
  170.  
  171. case 2: deleteentry(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
  172.  
  173. case 3: update(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
  174.  
  175. case 4: find(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
  176.  
  177. case 5: printall(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
  178.  
  179. case 6: sort(employee.name, employee.email, employee.address, employee.postal, employee.telephone); break;
  180.  
  181. default: cerr << "Please enter a valid option\n";
  182. }
  183.  
  184.  
  185. system ("pause");
  186.  
  187. system ("cls");
  188. }
  189.  
  190. };
  191. int main
  192. ()
  193. {
  194. menu();
  195. system ("pause");
  196. }
The problems I face are:
1> In input one of the entries is overlapped with another, as in if employee.postal==570306
employee. address== employee.postal
2> In outputting (printing) what happens is that one of the data is not printed. Same with gets(). I used it twice, otherwise it was inputting only 4 things. I think 1 and 2 might have a connection.
Can someone suggest me what to do, so that I can debug other numerous problems?
Thanks.
Last edited by cscgal; Apr 16th, 2008 at 1:44 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Problems in Address book programming

 
0
  #2
Apr 15th, 2008
Well, postal is a character array, as is address, so you can't use the == operator to do comparisons. strcmp should do it though.

I assume you don't realise that you can pass a structure directly into a function as a parameter.
  1. struct thing{
  2. int x, y;
  3. };
  4.  
  5. void thingy( thing &i_thing ) { // pass by reference or value as you feel fit
  6. i_thing.x = 2;
  7. }
which should save all those parameters going into your function. A structure, basically, is a type which tidies up all that messy parameter passing. And as it's a defined type you can pass it into a function.

You might also consider using the std::string (from <string>). It allows == comparison which character arrays do not. Again, tidy way of doing things.

There are also better alternatives to system("pause")
Last edited by twomers; Apr 15th, 2008 at 7:41 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Re: Problems in Address book programming

 
0
  #3
Apr 15th, 2008
Hey I'm writing an address program as well. If anyone could help me with it i'd greatly appreciate it. I'm aware of how terrible looking it is but if someone can decipher what's going on in my program and try to help me that'd be much appreciated. These are the requierments:

************Read “AddressBook.txt” file (if the file exists) Module:

Insert each entry into the array of structures by name
a. First record (line) contains the number of entries
b. Entry consists of the following information
Name (maximum 30 characters)
BirthDate (mm/dd/yyyy)
Address Information
Type (maximum 8 characters: Friend, Family, Business)
c. Address Information consists of the following information:
Street (maximum 35 characters)
City (maximum 20 characters)
State (2 characters)
Zip (5 characters)
Phone (14 characters)

4) User can choose the following options:
a. List all entries. This option will result in an alphabetic list.
b. Look up a specific entry by Name (use binary search). Ask user for the desired name. Output error message if the name is not found otherwise output the information.
c. List all the entries for a specific Type. Ask user for the desired type (Friend, Family, Business). Output the type followed by all the entries that match this type (i.e. Friend). This will also result in an alphabetic listing.***********************



Unfortunately this i sall i have so far...


cplusplus Syntax (Toggle Plain Text)
int NAME_LENGTH;
int BIRTHDAY_LENGTH;
int ADDRESS_LENGTH;
int MAX_LISTING;
char relation [relation_+1];
struct addressType
{ string street;
string city;
string state;
string zip;
string phone;
};


struct contactType
{
char name[NAME_LENGTH];
int Bday;
addressType address;
char relation;
};

contactType newContact;
contactType contacts[100];

void inputName (ifstream &, char , nameType &);void inputBday (ifstream &, int , BdayType &);
void inputAddress (ifstream &, int , addressType &);void inputRelation (ifstream &, char , relationType &);
int extern search (const nameType [], char, const char *);int NAME_LENGTH;
int BIRTHDAY_LENGTH;
int ADDRESS_LENGTH;
int MAX_LISTING;
char relation [relation_+1];



struct addressType
{
string street;
string city;
string state;
string zip;
string phone;
};


struct contactType
{
char name[NAME_LENGTH];
int Bday;
addressType address;
char relation;
};
contactType newContact;

contactType contacts[100];




void inputName (ifstream &, char , nameType &);
void inputBday (ifstream &, int , BdayType &);
void inputAddress (ifstream &, int , addressType &);
void inputRelation (ifstream &, char , relationType &);
int extern search (const nameType [], char, const char *);






This is the main

Help with Code Tags cplusplus Syntax (Toggle Plain Text)
int main (){ int numNames, numBdays, numAdds, index, location; char searchArg[birthday_LENGTH+1];

ifstream infile;
ofstream outfile;
contactType allcontact[MAX_CONTACTS]
infile.open (".AddressBook.txt");
assert (!infile.fail());
outfile.open ("./contacts.txt");
assert (!outfile.fail());
inputName ();
inputBday ();
searchArg[birthday_LENGTH] = '\0';

while (true) { cout << "\n\nEnter a Birthday(mm/dd/yyyy) or 0000000 to end: ";

cin >> searchArg;

if (strcmp (searchArg, "0000000") == 0) break;}
infile.close();


And this one I think is the input

for (int j = 0; j < 100; j++)

cin >> contacts[j].name.first >> contacts[j].name.middle >> contacts[j].name.last;


for (int j = 0; j < 100; j++)
cin >> contacts[j].Bday.month >> contacts[j].Bday.day >> contacts [j].Bday.year;

for (int j = 0; j < 100; j++)

cin >> contacts[j].address.street >> contacts[j].name.state >> contacts[j].address.zip >> contacts[j].address.phone;


for (int j = 0; j < 100; j++)

cin >> contacts[j].relation.Friend >> contacts[j].relation.Family >> contacts[j].relation.business;




I know that this needs a lot of changes.I also have an example of binary coding so if sum1 could show me where my information would go that would be appreciated as well.


int search (const studentType list[], int length, const char * searchArg){ bool found = false; int low = 0, high = length-1, mid; while (!found && low <= high) { mid = (low + high) / 2; if (strcmp(searchArg, list[mid].ID) == 0) found = true; else if (strcmp(searchArg, list[mid].ID) < 0) high = mid - 1; else low = mid + 1; } if (found) return mid; else return -1;} int main ()
{

int numNames, numBdays, numAdds, index, location;
char searchArg[birthday_LENGTH+1];
ifstream infile;
ofstream outfile;
contactType allcontact[MAX_CONTACTS]





infile.open (".AddressBook.txt");
assert (!infile.fail());

outfile.open ("./contacts.txt");
assert (!outfile.fail());

inputName ();


inputBday ();



searchArg[birthday_LENGTH] = '\0';
while (true)
{
cout << "\n\nEnter a Birthday(mm/dd/yyyy) or 0000000 to end: ";
cin >> searchArg;
if (strcmp (searchArg, "0000000") == 0)
break;
}


infile.close();




And this one I think is the input

for (int j = 0; j < 100; j++)
cin >> contacts[j].name.first >> contacts[j].name.middle
>> contacts[j].name.last;

for (int j = 0; j < 100; j++)
cin >> contacts[j].Bday.month >> contacts[j].Bday.day
>> contacts[j].Bday.year;

for (int j = 0; j < 100; j++)
cin >> contacts[j].address.street >> contacts[j].name.state
>> contacts[j].address.zip >> contacts[j].address.phone;

for (int j = 0; j < 100; j++)
cin >> contacts[j].relation.Friend >> contacts[j].relation.Family
>> contacts[j].relation.business;


I know that this needs a lot of changes.
I also have an example of binary coding so if sum1 could show me where my information would go that would be appreciated as well.


int search (const studentType list[], int length, const char * searchArg)
{
bool found = false;
int low = 0, high = length-1, mid;

while (!found && low <= high)
{
mid = (low + high) / 2;
if (strcmp(searchArg, list[mid].ID) == 0)
found = true;
else
if (strcmp(searchArg, list[mid].ID) < 0)
high = mid - 1;
else
low = mid + 1;
}

if (found)
return mid;
else
return -1;
}




It is greatly apreciated if this is at least looked at. If for any reason this doesn't look like what programs posted should I can't figure out why because i copied a version that someone modified for me to model mine after when posting. Thanks again.
Last edited by Spagett912; Apr 15th, 2008 at 1:45 pm. Reason: this was crappy
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 4
Reputation: sohamghosh is an unknown quantity at this point 
Solved Threads: 0
sohamghosh sohamghosh is offline Offline
Newbie Poster

Re: Problems in Address book programming

 
0
  #4
Apr 16th, 2008
Thanks for the advice. I am working on it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC