#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#define input "f:\\tcpp\\bin\\files\\input.txt"
#define tempo "f:\\tcpp\\bin\\files\\tempo.txt"
void menu(void)
{
cout << "Menu\n\n";
cout << "A - Add\n";
cout << "D - Delete\n";
cout << "I - Display\n";
cout << "E - Edit\n";
cout << "S - Search\n";
cout << "X - Exit\n\n\n";
cout << "Enter your choice: ";
}
void add(void)
{
FILE *infile;
char Name[50], Target[50];
int ID = 0;
float Amount;
char Gender;
if((infile = fopen(input, "a+")) == NULL)
ID = 1;
else
{
do{
fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
}while(!feof(infile));
ID += 1;
}
cout << "ID : " << ID << endl;
cout << "Name: ";
cin >> Name;
cout << "Gender: ";
cin >> Gender;
cout << "Amount: ";
cin >> Amount;
fprintf(infile, "%i %s %c %f\n", ID, Name, Gender, Amount);
fclose(infile);
}
void deletes(void)
{
FILE *infile;
FILE *tfile;
char Name[50], Target[50];
int ID = 0, found = 0;
float Amount;
char Gender;
tfile = fopen(tempo, "w");
if((infile = fopen(input, "r")) == NULL)
cout << "File Empty!" << endl;
else
{
cout <<"Enter Name to delete: ";
cin >> Target;
while(!feof(infile))
{
fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
if(feof(infile))
break;
if(strcmp(Target, Name) != 0)
fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
else
{
found = 1;
cout << "ID: " << ID << endl;
cout << "Name: " << Name << endl;
if(toupper(Gender) == 'F')
cout << "Gender:\tFemale\n";
else
cout << "Gender:\tMale\n";
cout << "Amount: " << Amount << endl;
}
}
if (!found)
cout << "Record not found!\n";
}
cout << "\nRecord deleted.";
fclose(infile);
fclose(tfile);
remove(input);
rename(tempo, input);
}
void display(void)
{
FILE *infile;
char Name[50], Target[50];
int ID = 0;
float Amount;
char Gender;
if((infile = fopen(input, "a+")) == NULL)
cout << "File Empty!";
else
{
while(!feof(infile))
{
fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
if(feof(infile))
break;
cout << ID << " " << Name << " " << Gender << " " << Amount << endl;
}
}
cout << "\nEnd of file, press any key to exit." << endl;
fclose(infile);
}
void search(void)
{
FILE *infile;
char Name[50], Target[50];
int ID = 0, found = 0;
float Amount;
char Gender;
if ((infile = fopen(input, "r+")) == NULL)
cout << "File Empty!";
else
{
cout << "Enter Name to search: ";
cin >> Target;
while(found == 0 && !feof(infile))
{
fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
if(strcmp(Target, Name) == 0)
found = 1;
}
if(found)
{
cout << "ID: " << ID << endl;
cout << "Name: " << Name << endl;
if(toupper(Gender) == 'F')
cout << "Gender: Female\n";
else
cout << "Gender: Male\n";
cout << "Amount: " << Amount << endl;
}
}
fclose(infile);
}
void edit(void)
{
FILE *infile;
FILE *tfile;
char Name[50], Target[50];
int ID = 0, found = 0, back;
float Amount;
char Gender;
tfile = fopen(tempo, "a+");
if ((infile = fopen(input, "r+")) == NULL)
cout << "File Empty!";
else
{
cout << "Enter Name to edit: ";
cin >> Target;
while(found == 0)
{
while(!feof(infile))
/* I tried to replace !feof(infile) as mentioned above */
{
fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
if(strcmp(Target, Name) != 0)
fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
else
{
back = ID;
found = 1;
cout << "ID: " << ID << endl;
cout << "Name: " << Name << endl;
if(toupper(Gender) == 'F')
cout << "Gender: Female\n";
else
cout << "Gender: Male\n";
cout << "Amount: " << Amount << endl;
}
}
}
if(found)
{
ID = back;
cout << "Enter new information\n\n";
cout << "ID : " << ID << endl;
cout << "Name: ";
cin >> Name;
cout << "Gender: ";
cin >> Gender;
cout << "Amount: ";
cin >> Amount;
cout << "Record edited!" << endl;
fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
}
}
fclose(infile);
fclose(tfile);
remove(input);
rename(tempo, input);
}
int main(void)
{
char choice;
do{
clrscr();
menu();
scanf("%c", &choice);
switch(toupper(choice))
{
case 'A':
clrscr();
add();
getch();
break;
case 'D':
clrscr();
deletes();
getch();
break;
case 'I':
clrscr();
display();
getch();
break;
case 'E':
clrscr();
edit();
getch();
break;
case 'S':
clrscr();
search();
getch();
break;
}
}while(toupper(choice) != 'X');
return 0;
}
Plssss

New Topic/Question
Reply




MultiQuote





|