944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3584
  • C++ RSS
Apr 7th, 2005
0

Address Book.

Expand Post »
:evil: The following C program is mainly written to work as an address book. I have written the code but the VC++ compiler shows four errors and I cant find them :rolleyes: . Can some one please help me find them..my assignment deadline line is on the 10th of this month.
C++ Syntax (Toggle Plain Text)
  1. //initializing the header files
  2.  
  3. #include<stdio.h>
  4. #include<ctype.h>
  5. #include<string.h>
  6. #include<stdlib.h>
  7.  
  8. //defining the properties of the fields used in the program
  9.  
  10. #define NAME 50
  11. #define ADDRESS 8
  12. #define PHONE 20
  13. #define EMAIL 20
  14. #define SIZE 500
  15.  
  16. //declaring the functions
  17.  
  18. void Insert();
  19. void Del_info();
  20. void Display();
  21. void Search();
  22. void Exit();
  23.  
  24. char info[SIZE];
  25.  
  26. //using structures that represent the fields in the program
  27.  
  28. struct addressbook
  29. {
  30. char name [NAME] ;
  31. char address [ADDRESS];
  32. char phone [PHONE];
  33. char email [EMAIL] ;
  34. char Target[NAME];
  35.  
  36. } addressbook;
  37.  
  38. //initializing the files used in the program
  39.  
  40. FILE *AddressBook;
  41. FILE *rectemp;
  42.  
  43. int main()
  44. {
  45. int choice=0;
  46. int k;
  47. printf(" Welcome \n");
  48. printf(" Address Book\n");
  49. printf(" This program is licensed to Kashfee\n");
  50. printf("Please enter the password(an integer): ");
  51. scanf("%d",&k);
  52. if (k!=8912685)
  53. {
  54. printf("Password Invalid\n");
  55. return 0;
  56. }
  57.  
  58. else{
  59. printf("\n My Address Book can perform the following operations \t");
  60. printf("\n 1> Insert new Entry\n");
  61. printf("\n 2> Delete Entry\n");
  62. printf("\n 3> Display all Entry\n");
  63. printf("\n 4> Search Entry\n");
  64. printf("\n 5> Exit the program\n");
  65. printf("\n Enter your choice <1-5>: ");
  66. scanf("%i",&choice);
  67.  
  68. switch (choice)
  69. {
  70.  
  71. case 1:
  72. {
  73. Insert();
  74. break;
  75. }
  76.  
  77. case 2:
  78. {
  79. Del_info();
  80. break;
  81. }
  82.  
  83. case 3:
  84. {
  85. Display();
  86. break;
  87. }
  88.  
  89. case 4:
  90. {
  91. Search();
  92. break;
  93. }
  94.  
  95. case 5:
  96. {
  97. Exit();
  98. break;
  99. }
  100.  
  101.  
  102. default:
  103. {
  104.  
  105. printf("**Invalid Input.**\n");
  106.  
  107. }
  108.  
  109.  
  110. }
  111. }
  112. }
  113. void Insert()
  114. {
  115. char choice1;
  116. do
  117. { //opening the AddressBook file
  118. AddressBook = fopen("AddressBook.txt","a+");
  119.  
  120.  
  121. printf("Enter Person's Name\n");
  122. scanf("%s",addressbook.name);
  123.  
  124.  
  125. printf("Enter Person's Address\n");
  126. scanf("%s",addressbook.address);
  127.  
  128. printf("Enter Person's Phone no.\n");
  129. scanf("%s",addressbook.phone);
  130.  
  131.  
  132. printf("Enter Person's E-mail\n");
  133. scanf("%s",addressbook.email);
  134.  
  135.  
  136. //printing all the entries to the main file (AddressBook.txt)
  137. fprintf(AddressBook,"%s %s %s %s \n",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
  138. fclose(AddressBook);
  139.  
  140.  
  141.  
  142. //initializing the choice character to give an option to continue
  143. printf("Press y/Y to Execute the Program Again \n");
  144. scanf("%c",&choice1);
  145.  
  146.  
  147. }
  148. while(choice1=='y'||choice1=='Y');
  149. main();
  150. }
  151.  
  152. void Del_info()
  153. {
  154. char choice2;
  155. char Target[SIZE];
  156. int Found=0;
  157. rectemp=fopen("rectemp.txt","w");
  158. if((AddressBook=fopen("AddressBook.txt","r"))==NULL)
  159. printf("**The File is Empty**\n\n");
  160.  
  161. else{
  162. printf("\tEnter Person's Name : ");
  163. gets(Target);
  164.  
  165. while(!feof(AddressBook))
  166. {
  167. fscanf(AddressBook,"%s %s %s %s",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
  168. if(feof(AddressBook))
  169. break;
  170. if(strcmp(Target,addressbook.name)!=0)
  171.  
  172. fprintf(rectemp,"%s %s %s %s \n",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
  173. else {
  174.  
  175. Found=1;
  176. }
  177. if (!Found)
  178. {
  179. printf("\t**There is no such Entry\n");//incase no files are located
  180. }
  181. printf("\t**Item is Deleted**\n");
  182.  
  183. fclose(AddressBook);
  184. fclose(rectemp);
  185. remove("Addressbook.txt");
  186.  
  187. rename("rectemp.txt","AddressBook.txt");
  188.  
  189. do{
  190. printf("Press y/Y to Execute the Program Again \n");
  191. scanf("%c",&choice2);
  192.  
  193. }
  194. while(choice2=='y'||choice2=='Y');
  195. main();
  196.  
  197.  
  198. }
  199.  
  200. void Display()
  201. {
  202. char choice3;
  203.  
  204. AddresssBook = fopen("AddressBook.txt","a+");
  205. do
  206. {
  207.  
  208. fgets(info,SIZE,AddressBook);
  209. printf("%s\n",info);
  210.  
  211. }while(!feof(AddressBook));
  212. fclose(AddressBook);
  213. do{
  214. printf("Press y/Y to Execute the Program Again \n");
  215. scanf("%c",&choice3);
  216.  
  217.  
  218. }while(choice3=='y'||choice3=='Y');
  219.  
  220. main();
  221. }
  222.  
  223. void Search()
  224. {
  225. char choice4;
  226. char Target[SIZE];
  227. int Found=0;
  228. if((AddressBook=fopen("AddressBook.txt","r"))==NULL)
  229.  
  230. printf("**The file is empty** !!\n\n");
  231. else
  232. {
  233. printf("\tEnter The Name:");
  234.  
  235. scanf("%s",Target);
  236. while(!feof(Address)&& Found==0)
  237. {
  238. fscanf(AddressBook,"%s %s %s %s ",addressbook.name,addressbook.address,addressbook.phone,addressbk.email);
  239. if(strcmp(Target,addressbook.name)==0)
  240. Found=1;
  241. }
  242. if(Found)
  243. {
  244. printf("The Name is: %s\n",addressbook.name);
  245. printf("The address is: %s\n",addressbook.address);
  246. printf("The phone number is: %s\n",addressbook.phone);
  247. printf("The e-mail address is:%s\n",addressbook.email);
  248.  
  249. main();
  250. }
  251. else if(!Found)
  252. printf("**There is no such Entry**\n");
  253. fclose(Addressbook);
  254. }
  255. do{
  256. printf("Press y/Y to Execute the Program Again \n");
  257. scanf("%c",&choice4);
  258.  
  259.  
  260. }
  261. while(choice4=='y'||choice4=='Y');
  262. main();
  263. }
  264.  
  265.  
  266. void Exit()
  267. {
  268. printf("**********************\n");
  269. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acidburns is offline Offline
6 posts
since Mar 2005
Apr 7th, 2005
0

Re: Address Book.

The list of things you've done wrong is too long for me to go into detail, so here are the biggies:

1) gets is wrong
2) main should not be called recursively
3) feof is not meant to be a loop condition
4) Your constructs are very inconsistent

At least you didn't use void main. Here is the code that compiles:
C++ Syntax (Toggle Plain Text)
  1. //initializing the header files
  2.  
  3. #include<stdio.h>
  4. #include<ctype.h>
  5. #include<string.h>
  6. #include<stdlib.h>
  7.  
  8. //defining the properties of the fields used in the program
  9.  
  10. #define NAME 50
  11. #define ADDRESS 8
  12. #define PHONE 20
  13. #define EMAIL 20
  14. #define SIZE 500
  15.  
  16. //declaring the functions
  17.  
  18. void Insert();
  19. void Del_info();
  20. void Display();
  21. void Search();
  22. void Exit();
  23.  
  24. char info[SIZE];
  25.  
  26. //using structures that represent the fields in the program
  27.  
  28. struct addressbook
  29. {
  30. char name [NAME] ;
  31. char address [ADDRESS];
  32. char phone [PHONE];
  33. char email [EMAIL] ;
  34. char Target[NAME];
  35. } addressbook;
  36.  
  37. //initializing the files used in the program
  38.  
  39. FILE *AddressBook;
  40. FILE *rectemp;
  41.  
  42. int main()
  43. {
  44. int choice=0;
  45. int k;
  46. printf(" Welcome \n");
  47. printf(" Address Book\n");
  48. printf(" This program is licensed to Kashfee\n");
  49. printf("Please enter the password(an integer): ");
  50. scanf("%d",&k);
  51. if (k!=8912685)
  52. {
  53. printf("Password Invalid\n");
  54. return 0;
  55. }
  56. else{
  57. printf("\n My Address Book can perform the following operations \t");
  58. printf("\n 1> Insert new Entry\n");
  59. printf("\n 2> Delete Entry\n");
  60. printf("\n 3> Display all Entry\n");
  61. printf("\n 4> Search Entry\n");
  62. printf("\n 5> Exit the program\n");
  63. printf("\n Enter your choice <1-5>: ");
  64. scanf("%i",&choice);
  65.  
  66. switch (choice)
  67. {
  68. case 1:
  69. {
  70. Insert();
  71. break;
  72. }
  73. case 2:
  74. {
  75. Del_info();
  76. break;
  77. }
  78. case 3:
  79. {
  80. Display();
  81. break;
  82. }
  83. case 4:
  84. {
  85. Search();
  86. break;
  87. }
  88. case 5:
  89. {
  90. Exit();
  91. break;
  92. }
  93. default:
  94. {
  95. printf("**Invalid Input.**\n");
  96. }
  97. }
  98. }
  99. }
  100. void Insert()
  101. {
  102. char choice1;
  103. do
  104. { //opening the AddressBook file
  105. AddressBook = fopen("AddressBook.txt","a+");
  106.  
  107. printf("Enter Person's Name\n");
  108. scanf("%s",addressbook.name);
  109.  
  110. printf("Enter Person's Address\n");
  111. scanf("%s",addressbook.address);
  112.  
  113. printf("Enter Person's Phone no.\n");
  114. scanf("%s",addressbook.phone);
  115.  
  116. printf("Enter Person's E-mail\n");
  117. scanf("%s",addressbook.email);
  118.  
  119. //printing all the entries to the main file (AddressBook.txt)
  120. fprintf(AddressBook,"%s %s %s %s \n",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
  121. fclose(AddressBook);
  122.  
  123. //initializing the choice character to give an option to continue
  124. printf("Press y/Y to Execute the Program Again \n");
  125. scanf("%c",&choice1);
  126. }
  127. while(choice1=='y'||choice1=='Y');
  128. main();
  129. }
  130.  
  131. void Del_info()
  132. {
  133. char choice2;
  134. char Target[SIZE];
  135. int Found=0;
  136. rectemp=fopen("rectemp.txt","w");
  137. if((AddressBook=fopen("AddressBook.txt","r"))==NULL)
  138. printf("**The File is Empty**\n\n");
  139. else{
  140. printf("\tEnter Person's Name : ");
  141. gets(Target);
  142.  
  143. while(!feof(AddressBook))
  144. {
  145. fscanf(AddressBook,"%s %s %s %s",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
  146. if(feof(AddressBook))
  147. break;
  148. if(strcmp(Target,addressbook.name)!=0)
  149. fprintf(rectemp,"%s %s %s %s \n",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
  150. else {
  151. Found=1;
  152. }
  153. if (!Found)
  154. {
  155. printf("\t**There is no such Entry\n");//incase no files are located
  156. }
  157. printf("\t**Item is Deleted**\n");
  158.  
  159. fclose(AddressBook);
  160. fclose(rectemp);
  161. remove("Addressbook.txt");
  162.  
  163. rename("rectemp.txt","AddressBook.txt");
  164.  
  165. do{
  166. printf("Press y/Y to Execute the Program Again \n");
  167. scanf("%c",&choice2);
  168. }
  169. while(choice2=='y'||choice2=='Y');
  170. main();
  171. }
  172. }
  173. }
  174.  
  175. void Display()
  176. {
  177. char choice3;
  178.  
  179. AddressBook = fopen("AddressBook.txt","a+");
  180. do
  181. {
  182. fgets(info,SIZE,AddressBook);
  183. printf("%s\n",info);
  184. }while(!feof(AddressBook));
  185. fclose(AddressBook);
  186. do{
  187. printf("Press y/Y to Execute the Program Again \n");
  188. scanf("%c",&choice3);
  189.  
  190.  
  191. }while(choice3=='y'||choice3=='Y');
  192.  
  193. main();
  194. }
  195.  
  196. void Search()
  197. {
  198. char choice4;
  199. char Target[SIZE];
  200. int Found=0;
  201. if((AddressBook=fopen("AddressBook.txt","r"))==NULL)
  202.  
  203. printf("**The file is empty** !!\n\n");
  204. else
  205. {
  206. printf("\tEnter The Name:");
  207.  
  208. scanf("%s",Target);
  209. while(!feof(AddressBook)&& Found==0)
  210. {
  211. fscanf(AddressBook,"%s %s %s %s ",addressbook.name,addressbook.address,addressbook.phone,addressbook.email);
  212. if(strcmp(Target,addressbook.name)==0)
  213. Found=1;
  214. }
  215. if(Found)
  216. {
  217. printf("The Name is: %s\n",addressbook.name);
  218. printf("The address is: %s\n",addressbook.address);
  219. printf("The phone number is: %s\n",addressbook.phone);
  220. printf("The e-mail address is:%s\n",addressbook.email);
  221.  
  222. main();
  223. }
  224. else if(!Found)
  225. printf("**There is no such Entry**\n");
  226. fclose(AddressBook);
  227. }
  228. do{
  229. printf("Press y/Y to Execute the Program Again \n");
  230. scanf("%c",&choice4);
  231. }
  232. while(choice4=='y'||choice4=='Y');
  233. main();
  234. }
  235.  
  236.  
  237. void Exit()
  238. {
  239. printf("**********************\n");
  240. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 7th, 2005
0

Re: Address Book.

Thank you so much...your suggestions really helped
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acidburns is offline Offline
6 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: very important please
Next Thread in C++ Forum Timeline: Calculations





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC