I'm having trouble with the following program. i want it to have the options to save a list of data entered by the user and display it the next time they run it. Could someone please either show me a sample program or tell me where i'm going wrong.
i use DEV C++
Written in C
[code]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char friends_name[20];
int number_of_friends_i;
int found_i;
/* Prototype */
int f_menu();
void f_recall();
void f_enter_names_ages();
void f_display_list();
void f_extend_list();
void f_delete_name();
void f_search();
void f_save();
typedef struct
{
int friends_age_i;
char friends_name[20];
}FRIENDS;
FRIENDS friends_details[20]; /* Stes up 20 friends in an array */
int main(void)
{
int Choice_d = 0;
printf ("This is a program to store and display a list of names and ages\n");
printf ("You can enter upto 20 names and ages, add and delete from the list, display the list and search for a name on the list\n\n");
do
{
Choice_d = f_menu ();
switch (Choice_d)
{
case 1: f_recall();
break;
case 2: f_enter_names_ages();
break;
case 3: f_display_list();
break;
case 4: f_extend_list();
break;
case 5: f_delete_name();
break;
case 6: f_search();
break;
case 7: f_save();
break;
case 8: printf ("Bye Bye\n\n");
break;
}
}
while (Choice_d != 8);
system ("Pause");
return 0;
}
/* ************************************************** ******************* */
int f_menu ()
{
int Selected_d = 0;
do
{
printf ("(1) Load saved list\n(2) Enter names & ages\n(3) Display list\n(4) Extend list\n(5) Delete from list\n(6) Search for name\n(7) Save list\n(8) Quit\n\n");
printf ("Enter a number that corresponds to your choice > ");
scanf ("%d", &Selected_d);
printf("\n");
if (( Selected_d < 1 ) || ( Selected_d > 8))
{
printf("You have entered an invalid choice. Please try again\n\n\n");
}
}
while (( Selected_d < 1) || ( Selected_d > 8));
return Selected_d;
}
/* ************************************************** ************************** */
void f_recall()
{
FILE *fp;
int i;
/* Set up blank array */
for (i = 0; i < 20; i++ )
{
strcpy ("", friends_details[i].friends_name);
friends_details[i].friends_age_i = 0;
}
number_of_friends_i = 0;
if ((fp = fopen ("friends_age_i", "rb" )) == NULL)
{
printf("No list saved to file\n Creating new file\n");
}
else
{
/* Read existing data from file */
fread ( &friends_details[number_of_friends_i], sizeof (FRIENDS), 1, fp);
while ( !(feof (fp)))
{
number_of_friends_i ++;
fread ( &friends_details[number_of_friends_i], sizeof (FRIENDS), 1, fp);
}
printf("There are %d friends already stored\n", number_of_friends_i);
fclose(fp);
}
}
/* ************************************************** ************************** */
void f_enter_names_ages()
{
int count_i = 0;
printf("Enter Names and Ages of up to 20 friends\n");
printf("Enter number of friends you wish to enter > ");
scanf("%d", &number_of_friends_i);
fflush(stdin);
printf("\n");
do
{
printf("Enter friends name > ");
scanf("%s", &friends_details[count_i].friends_name);
printf("enter %s's age > ", friends_details[count_i].friends_name);
scanf("%d", &friends_details[count_i].friends_age_i );
fflush(stdin);
count_i ++;
} while ((count_i <= 19) && (count_i < number_of_friends_i));
printf("\n\n");
}
/* ************************************************** ************************** */
void f_display_list() /* Display list in alphabetical order */
{
int count_i = 0;
int i;
int sorted_i; /* Flag that tells if array is already sorted */
FRIENDS temp;
if(number_of_friends_i < 1)
{
printf("List empty\n\n");
}
else
{
do /* Sort into alphabetical order */
{
sorted_i = 0;
for ( i = 0; i < number_of_friends_i - 1; i++)
{
if(strcmp (friends_details[i].friends_name,friends_details[i + 1].friends_name) > 0 )
{
temp = friends_details[i];
friends_details[i] = friends_details[i + 1];
friends_details[i + 1] = temp;
sorted_i = 1;
}
}
}while (sorted_i == 1);
printf("Friend\n"); /* Display sorted list */
do
{
printf("%d: %s age %d\n", count_i + 1, friends_details[count_i].friends_name, friends_details[count_i].friends_age_i );
count_i ++;
}
while (count_i < number_of_friends_i);
printf("\n\n");
}
}
/* ************************************************** ************************** */
void f_extend_list() /* Add a friend to the list */
{
int i = number_of_friends_i;
if (number_of_friends_i < 19) /* Max 20 names on list. Can't add another if list is full */
{
printf("Enter friends name > ");
scanf("%s", &friends_details[i].friends_name);
printf("enter %s's age > ", friends_details[i].friends_name);
scanf("%d", &friends_details[i].friends_age_i );
fflush(stdin);
printf("\n\n");
number_of_friends_i = number_of_friends_i + 1; /* List increased by 1 */
}
else
{
printf("Sorry, you can't enter any more to the list");
}
}
/* ************************************************** ************************** */
void f_delete_name() /* Remove name from list */
{
char search_name [20];
int i;
printf("Enter the name of the friend you want to delete > ");
scanf("%s", &search_name);
fflush(0);
printf("\n");
found_i = 0;
i = 0;
while (( i < number_of_friends_i ) && ( found_i == 0))
{
if (strcmp (friends_details[i].friends_name, search_name) == 0)
{
found_i = 1;
}
i++;
}
if (found_i == 1)
{ /* Copy last entry on list onto name to be deleted. Reduce list by 1 */
i --;
printf("%s age %d has been deleted\n\n", friends_details[i].friends_name, friends_details[i].friends_age_i);
strcpy(friends_details[i].friends_name, friends_details[number_of_friends_i - 1].friends_name);
friends_details[i].friends_age_i = friends_details[number_of_friends_i - 1].friends_age_i;
number_of_friends_i = number_of_friends_i - 1;
}
else
{
printf("name not found\n\n");
}
}
/* ************************************************** ************************** */
void f_search() /* Find name on list */
{
char search_name [20];
int i;
printf("Enter the name of the friend you want to find > ");
scanf("%s", &search_name);
fflush(0);
printf("\n");
found_i = 0;
i = 0;
while (( i < number_of_friends_i ) && ( found_i == 0))
{
if (strcmp (friends_details[i].friends_name, search_name) == 0)
{
found_i = 1;
}
i++;
}
if (found_i == 1)
{
i --;
printf("%s age %d has been found\n\n", friends_details[i].friends_name, friends_details[i].friends_age_i);
}
else
{
printf("name not found\n\n");
}
}
/* ************************************************** ************************** */
void f_save()
{
FILE *fp;
int i;
if ((fp = fopen ( "friends_age_i", "wb" )) == NULL )
{
printf("Cannot open file\n");
return;
}
for (i = 0; i < number_of_friends_i; i ++)
{
fwrite (&friends_details[i], sizeof (FRIENDS), 1, fp);
}
fclose (fp);
}
[code]
Thanks