C++ Programming
08-02-2009, 02:36 AM
Ok So I have the following code but want to get it to write the entered people to a file and then rather then just reading from the stored array read it back from the file and show it on screen in the same format,
This is the code:
#include
#include
#include
#define MAX_NAME_LEN 80 ///< Maximum lenght of a name buffer
typedef struct
{
char name[MAX_NAME_LEN + 1];
int age;
char gender;
int storeID;
int loyaltyCardPoints;
} reg;
/// Pointer to global array of customers
reg *g_a_customers;
// Used to track number of records in the array
int g_num_customers;
/// Prototypes to go into header
void add_customer(reg *cust);
void display_customer(reg *cust);
int main(void)
{
int count=0;
int i;
/// Set the array to 0
memset(&g_a_customers, 0, sizeof(g_a_customers));
printf("How many customers to enter: ");
scanf("%d", &count);
g_a_customers = (reg*)calloc(count, sizeof(reg));
if (count < 1)
{
count = 0;
printf("Error, invalid number of customers!\n");
}
for ( i = 0; i < count; i++ )
add_customer(&g_a_customers[i]);
for ( i = 0; i < count; i++ )
display_customer(&g_a_customers[i]);
return 0;
}
void add_customer(reg *cust)
{
char gender[2];
char first[MAX_NAME_LEN + 1];
char last[MAX_NAME_LEN + 1];
int len;
printf("\nAdd New Customer:\n");
printf("First Name: ");
scanf("%80s", first);
len = strlen(first);
printf("Last Name: ");
scanf("%80s", last);
if ( len < MAX_NAME_LEN )
{
first[len] = ' ';
len++;
first[len] = 0;
}
strncpy(cust->name, first, len);
strncat(cust->name, last, MAX_NAME_LEN - len);
cust->name[MAX_NAME_LEN] = 0;
printf("Age: ");
scanf("%d", &cust->age);
if ( cust->age < 0 )
cust->age = 0;
printf("Gender (M or F): ");
scanf("%1s", &gender); ///< Take first letter to see if it is M or F
if ( gender[0] == 'm' || gender[0] == 'M' )
cust->gender = 'M';
else
cust->gender = 'F';
printf("Store ID: ");
scanf("%d", &cust->storeID);
if ( cust->storeID < 0 )
cust->storeID = 0;
printf("Loyalty Card Points: ");
scanf("%d", &cust->loyaltyCardPoints);
if ( cust->loyaltyCardPoints < 0 )
cust->loyaltyCardPoints = 0;
return;
}
void display_customer(reg *cust)
{
printf(
"\nDetail for Customer: %.*s\n"
"--------------------\n"
" Age: %d\n"
" Gender: %s\n"
" Store ID: %d\n"
" Loyalty Pts: %d\n",
MAX_NAME_LEN, cust->name,
cust->age,
(cust->gender == 'M' ? "Male" : "Female"),
cust->storeID,
cust->loyaltyCardPoints);
return;
}
Thanks in advance!
This is the code:
#include
#include
#include
#define MAX_NAME_LEN 80 ///< Maximum lenght of a name buffer
typedef struct
{
char name[MAX_NAME_LEN + 1];
int age;
char gender;
int storeID;
int loyaltyCardPoints;
} reg;
/// Pointer to global array of customers
reg *g_a_customers;
// Used to track number of records in the array
int g_num_customers;
/// Prototypes to go into header
void add_customer(reg *cust);
void display_customer(reg *cust);
int main(void)
{
int count=0;
int i;
/// Set the array to 0
memset(&g_a_customers, 0, sizeof(g_a_customers));
printf("How many customers to enter: ");
scanf("%d", &count);
g_a_customers = (reg*)calloc(count, sizeof(reg));
if (count < 1)
{
count = 0;
printf("Error, invalid number of customers!\n");
}
for ( i = 0; i < count; i++ )
add_customer(&g_a_customers[i]);
for ( i = 0; i < count; i++ )
display_customer(&g_a_customers[i]);
return 0;
}
void add_customer(reg *cust)
{
char gender[2];
char first[MAX_NAME_LEN + 1];
char last[MAX_NAME_LEN + 1];
int len;
printf("\nAdd New Customer:\n");
printf("First Name: ");
scanf("%80s", first);
len = strlen(first);
printf("Last Name: ");
scanf("%80s", last);
if ( len < MAX_NAME_LEN )
{
first[len] = ' ';
len++;
first[len] = 0;
}
strncpy(cust->name, first, len);
strncat(cust->name, last, MAX_NAME_LEN - len);
cust->name[MAX_NAME_LEN] = 0;
printf("Age: ");
scanf("%d", &cust->age);
if ( cust->age < 0 )
cust->age = 0;
printf("Gender (M or F): ");
scanf("%1s", &gender); ///< Take first letter to see if it is M or F
if ( gender[0] == 'm' || gender[0] == 'M' )
cust->gender = 'M';
else
cust->gender = 'F';
printf("Store ID: ");
scanf("%d", &cust->storeID);
if ( cust->storeID < 0 )
cust->storeID = 0;
printf("Loyalty Card Points: ");
scanf("%d", &cust->loyaltyCardPoints);
if ( cust->loyaltyCardPoints < 0 )
cust->loyaltyCardPoints = 0;
return;
}
void display_customer(reg *cust)
{
printf(
"\nDetail for Customer: %.*s\n"
"--------------------\n"
" Age: %d\n"
" Gender: %s\n"
" Store ID: %d\n"
" Loyalty Pts: %d\n",
MAX_NAME_LEN, cust->name,
cust->age,
(cust->gender == 'M' ? "Male" : "Female"),
cust->storeID,
cust->loyaltyCardPoints);
return;
}
Thanks in advance!