0
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#define input "d:\\tcpp\\bin\\files\\input.txt"
#define tempo "d:\\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], Name2[50], Target[50], Checker[50], Answer[5], Answer2[5] = "No";
	int ID = 0, Found = 0;
	float Amount;
	char Gender;

	clrscr();
	if((infile = fopen(input, "a+")) == NULL)
		ID = 1;
	else
	{
	do{
		do{
			fscanf(infile, "%i %s %c %f\n", &ID, Name, &Gender, &Amount);
			ID++;
			cout << "ID  :    " << ID << endl;
			cout << "Name:    " << flush;
			cin >> Name2;
			cout << "Gender:  " << flush;
			cin >> Gender;
			cout << "Amoun:  " << flush;
			cin >> Amount;


				if(strcmp(Name,Name2) == 0)
					cout << "Record Exist. Cannot add. " << endl;
				else
				{
				fprintf(infile, "%i %s %c %f\n", ID, Name, Gender, Amount);
				Found = 1;
				}

		}while(!feof(infile) && Found == 0);
		cout << "Do you want to add another [Yes/No]: ";
		cin >> Answer;
	}while(strcmp(Answer,Answer2) != 0);
	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
	{
		cout << "POLYTECHNIC UNIVERSITY OF THE PHILIPPINES" << endl;
		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)
		{
			while(!feof(infile))
			{
				fscanf(infile, "%i %s %c %f\n", &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;

	cout.precision(2);
	cout.setf(ios :: fixed);
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;
}

Can anyone fix my code? I have a problem on adding. How can I test if the user already input an existing one?
example:

ID: 2
Name: John
Gender : M
Amount: 123

Record existed! Cannot add!

ID: 2
Name: Jojo
Gender: F
Amount 123

Can anyone fix my code? I swear Im the one who made this code.

Edited by __avd: Fix [code] tags.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by daviddoria
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.