Simple add, search, display functions

hi guys.

im working on a project which includes adding , searching and display functions.
i got all codes of these functions but the problem is i cant merge it into one program..

can somebody help me merge it? your help is very much appreciated. ty!

heres the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<ctype.h>

main()
{
FILE *SariSariStore;
int item_id;
char answer;
char item_name[50];
SariSariStore = fopen("c:\\myfolder\\sarisari.txt","a+");
do {
   clrscr();
   cout<<"Enter Item no.: ";
   cin>>item_id;
   cout<<"Enter Item name: ";
   fflush(stdin);
   gets(item_name);
   cout<<"Do you want to save? Y/N: ";
   cin>>answer;
   if(toupper(answer)=='Y')
     fprintf(SariSariStore,"%i %s \n", item_id, item_name);
   }while(toupper(answer)!='N');
   fclose(SariSariStore);



return 0;
}


/* DISPLAY
  #include <stdio.h>
  #include<iostream.h>
  #include<conio.h>
   main( )
   {
     FILE *SariSariStore;
     char c;
     clrscr();
     SariSariStore = fopen("sarisari.txt", "r");
     if (SariSariStore == NULL) printf("File doesn't exist\n");
     else {
      do {
       c = getc(SariSariStore); // get one character from the file

	 putchar(c); // display it on the monitor

       } while (c != EOF); // repeat until EOF (end of file)

     }
    fclose(SariSariStore);
    getch();
    return 0;
   }

*/



/*SEARCH
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
#include <ctype.h>

main()
{
  FILE *SariSariStore;
  int found = 0;
  char name[100], target[100], name1[100];

  clrscr();

  SariSariStore = fopen("c:\\sarisari.txt" ,"r");

if(SariSariStore==NULL)
printf("File not found");
else
  printf("Enter Name to Search : ");


  fflush(stdin);
  gets(target);

do
{
	fscanf(SariSariStore,"%s %s",&name,&name1);
	if(strcmp(name,target)==0)
	{
	 found =1;
	}

}while(!feof(SariSariStore) && found == 0);

if(found==1)
{
printf("Name: %s %s",name,name1);
}
else
	printf("Not Found");


fclose(SariSariStore);
getch();

return 0;

}


*/
thank you for the reply zaita. would u mind giving the final code??
Makoy, all you have to do is remove the unneeded calls for libraries and replace the int main() methods with a new function name. For instance you could change Display to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Display() {
     FILE *SariSariStore;
     char c;
     clrscr();
     SariSariStore = fopen("sarisari.txt", "r");
     if (SariSariStore == NULL) printf("File doesn't exist\n");
     else {
      do {
       c = getc(SariSariStore); // get one character from the file

	 putchar(c); // display it on the monitor

       } while (c != EOF); // repeat until EOF (end of file)

     }
    fclose(SariSariStore);
    getch();
   }


That could will NOT work exactly as is though but the format would be the same. By the way your main function currently has no type. You need to specify it as int main() not just main. After writing that out though all you need to do is make a call to the method from the main function.
Last edited on
Topic archived. No new replies allowed.