0

Ive been trying to figure this out all day but have been unable to make as progress so I was wondering if anyone here could help me out.

Im making a program that allows the user to add a name, update the current name list , remove a name from the list, search for a name in the list & display all entered names.

So far Ive made my program create the text file that the entered & updated names store to, allowed the user to add a name to the text file, update the current name list with a new name, and have gotten the program to display entered names. Im still looking into how to make a name seachable and make a name removable.

I have one small (I hope) problem & one idea (I cant figure out how to implent it in to the program)

Problem:
The user enters a name and then decides he/she wants to display the name via the saved list, but once they enter the name, they cant display the entered name in the saved list straight away they need to close the program after entering the name, re-run the program and then the name shows up in the list, what I want is I dont want the user to have to close the program down inorder to see the name in the saved file, I want them to be able to enter a name, have the name be saved in the text file and then have the name viewable in the saved file (hope I havent over complicated my problem)

Idea:
I want to place a file check in my program so say user 1 has already told the program to create the saved file, entered a name and closes the program but user 2 doesnt realise the file has already be created, they select add new name and it overwrites the already inputted names (thats what it currently does). I want to tell the user 2 that the file has already been created and then tell the program to jump to the append file section.

Thanks for any ideas and/or solutions you can give/help me with, I will continue to reseach my problems and find out how to make entered names removable and searchable if I do come across anything that works I will post and keep anyone who is intrested or doing a similar program to mine informed Im also writing this program in visual studio C++ 2008.

Incase any one was wondering this is for an assignment but the notes I have confuse me a bit I have spoken to my lectuer and tried to get her to explain it to me but it still confuses me, I do have 2 c programming books both called C Programming second edition but ones by Mike McGrath and the other on is by Brian Wernighan/Dennis Ritchie.

#include <stdio.h> //
#include <stdlib.h> //
#include <string.h> //


struct PERSON //
{
	char title[10]; //Allows the title 
	char first_name[25]; //
	char surname[25]; //
};

// Tells the compiler to expect the defined custom fuctions 
int f_menu (); 
void f_add_get_ent ( struct PERSON ent[], int input1 ); 
void f_update_get_ent ( struct PERSON ent[], int input2 );
void f_remove (); 
void f_search (); 
void f_display(); 

struct PERSON ent[3]; //

void main () //
{
	int input = 0; //

	printf ( "Welcome to the naming database \n" ); //

	do //
	{
		input = f_menu (); //

		switch ( input ) //
		{
		case 1 : //
			f_add_get_ent (ent, 3); //
			break; //
		case 2 : //
			f_update_get_ent (ent, 3); //
			break; //
		case 3 : //
			f_remove (); //
			break; //
		case 4 : //
			f_search (); //
			break; //
		case 5 : //
			f_display (); //
			break; //
		case 6 : //
			printf ( "\nThank you for using this program\n\n" ); //
			printf("Press any key to continue\n"); //
			getch(); //
		}
	} while ( input !=6 ); //
}
int f_menu ( ) //
{

	int input = 0; //

	printf ( "\n\nNaming Database Menu\n" ); // 
	printf ( " 1. Add a name\n 2. Update a entered name\n 3. Remove a entered name\n 4. Search for a name\n 5. Display names entered\n 6. Quit\n " ); //
	printf ( "\nPlease enter choice: " ); //

	do //
	{
		scanf ( "%d", &input ); //
		fflush ( stdin ); //

		if (( input < 1 ) || ( input > 6 ) ) //
		{
			printf ( "Invalid choice. Please try again: " ); //
		}
	} while ( ( input < 1 ) || ( input > 6 ) ); //
	return ( input ); //
}


void f_add_get_ent ( struct PERSON ent[], int entry1) //
{
	char reply = NULL; //
	int input1 = 0; //

	FILE *name_file; //
	
	name_file = fopen( "names.txt", "wb+" ); //

	if (name_file > NULL) //
	{
		printf ( "File created\n" ); //

		printf ( "Please enter title: \n" ); //
		scanf ( "%s", &ent[input1].title ); //
		fflush (stdin ); //
		fputs ( &ent[input1].title, name_file ); //
		fputs ( " ", name_file ); //

		printf ( "Please enter first name: \n" ); //
		scanf ( "%s", &ent[input1].first_name ); //
		fflush (stdin ); //
		fputs ( &ent[input1].first_name, name_file ); //
		fputs ( " ", name_file ); //

		printf ( "Please enter surname: \n" ); //
		scanf ( "%s", &ent[input1].surname ); //
		fflush (stdin ); //
		fputs ( &ent[input1].surname, name_file ); //
		fputs ( "\r\n", name_file ); //
 
		printf ( "Full entered name: %s %s %s\n", &ent[input1].title, &ent[input1].first_name,&ent[input1].surname );//

		do //
		{

		input1++; //

		if ( input1 <= entry1 ) //
		{
			printf ( "\nWould you like to add another name Y/N: " ); //
			do //
			{
				scanf ( "%c", &reply ); //
				fflush ( stdin ); //
				reply = toupper( reply ); //

				if ( ( reply !='Y') && (reply != 'N') ) //
				{
					printf ( "Invaild entry. (Must be Y or N). Try again: " ); //
				}
			} while ( ( reply != 'Y' ) && (reply != 'N' ) ); //
		}
				if (( reply == 'Y' )) //
				{
					printf ( "Please enter title: \n" ); //
					scanf ( "%s", &ent[input1].title ); //
					fflush (stdin ); //
					fputs ( &ent[input1].title, name_file ); //
					fputs ( " ", name_file ); //

					printf ( "Please enter first name: \n" ); //
					scanf ( "%s", &ent[input1].first_name ); //
					fflush (stdin ); //
					fputs ( &ent[input1].first_name, name_file ); //
					fputs ( " ", name_file ); //

					printf ( "Please enter surname: \n" ); //
					scanf ( "%s", &ent[input1].surname ); //
					fflush (stdin ); //
					fputs ( &ent[input1].surname, name_file ); //
					fputs ( "\r\n", name_file ); //

					printf ( "Full entered name: %s %s %s\n", &ent[input1].title, &ent[input1].first_name,&ent[input1].surname );//

				}
	} while ( ( reply == 'Y' ) && ( input1 < entry1 ) ); //
	return ( input1 ); //


		fclose( name_file ); //
		return 0; //
	}
	else if (name_file <= NULL)
	{
		printf ( "File already created\n" );
	}
	else //
	{
		printf ( "Unable to create file.\n" ); //
	}
}


void f_update_get_ent ( struct PERSON ent[], int entry2 )
{
	char reply = NULL; //
	int input2 = 0; //

	FILE *name_file; //
	
	name_file = fopen( "names.txt", "ab+" ); //


	if (name_file != NULL) //
	{

		printf ( "Please enter title: \n" ); //
		scanf ( "%s", &ent[input2].title ); //
		fflush (stdin ); //
		fputs ( &ent[input2].title, name_file ); //
		fputs ( " ", name_file ); //

		printf ( "Please enter first name: \n" ); //
		scanf ( "%s", &ent[input2].first_name ); //
		fflush (stdin ); //
		fputs ( &ent[input2].first_name, name_file ); //
		fputs ( " ", name_file ); //

		printf ( "Please enter surname: \n" ); //
		scanf ( "%s", &ent[input2].surname ); //
		fflush (stdin ); //
		fputs ( &ent[input2].surname, name_file ); //
		fputs ( "\r\n", name_file ); //
 
		printf ( "Full entered name: %s %s %s\n", &ent[input2].title, &ent[input2].first_name,&ent[input2].surname );//

		do //
		{

		input2++; //

		if ( input2 <= entry2 ) //
		{
			printf ( "\nWould you like to add another name Y/N: " ); //
			do //
			{
				scanf ( "%c", &reply ); //
				fflush ( stdin ); //
				reply = toupper( reply ); //

				if ( ( reply !='Y') && (reply != 'N') ) //
				{
					printf ( "Invaild entry. (Must be Y or N). Try again: " ); //
				}
			} while ( ( reply != 'Y' ) && (reply != 'N' ) ); //
		}
				if (( reply == 'Y' )) //
				{
					printf ( "Please enter title: \n" ); //
					scanf ( "%s", &ent[input2].title ); //
					fflush (stdin ); //
					fputs ( &ent[input2].title, name_file ); //
					fputs ( " ", name_file ); //

					printf ( "Please enter first name: \n" ); //
					scanf ( "%s", &ent[input2].first_name ); //
					fflush (stdin ); //
					fputs ( &ent[input2].first_name, name_file ); //
					fputs ( " ", name_file ); //

					printf ( "Please enter surname: \n" ); //
					scanf ( "%s", &ent[input2].surname ); //
					fflush (stdin ); //
					fputs ( &ent[input2].surname, name_file ); //
					fputs ( "\r\n", name_file ); //

					printf ( "Full entered name: %s %s %s\n", &ent[input2].title, &ent[input2].first_name,&ent[input2].surname );//

				}
	} while ( ( reply == 'Y' ) && ( input2 < entry2 ) ); //
	return ( input2 ); //


		fclose( name_file ); //
		return 0; //
	}
	else //
	{
		printf ( "Unable to create file.\n" ); //
	}
}
void f_remove () //
{	
}
void f_search () //
{

}
void f_display ()  //
{

FILE *name_file ;
char ch ;

name_file = fopen ( "names.txt", "rb" ) ;

while ( 1 )
{
ch = fgetc ( name_file) ;

if ( ch == EOF )
break ;

printf ( "%c", ch ) ;
}
fclose ( name_file ) ;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

The user enters a name and then decides he/she wants to display the name via the saved list, but once they enter the name, they cant display the entered name in the saved list straight away ...

The solution is to save the new name in the list as soon as the user enters it. Why wait until the program is closed to save the list?

To delete a name you have to rewrite the entire file, omitting the name you want deleted.

What's all that about user #1 and user #2?? Are you running the program on a network which is accessed by multiple computers?

Edited by Ancient Dragon: n/a

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.