S
S Kemplay
Hi all,
I am after any pointers you may have to improve these two functions.
They both work but are part of an assignment - I am looking for feedback.
The first one reads records from a file - the teacher won't allow a
delimeted file he wants spaces between words joined with an underscore.
The records are read into an array of structs:
typedef struct drink
{
int stockCode;
char drinkName[45];
char manufacturer[25];
float purchasePrice;
} DRINK;
void option1(DRINK drinkArray[], DRINK drinkRec)
{
FILE* fp = fopen("products.txt", "r");
char line[255] = "";
int count = 0;
printf("\nRead records from file\n");
printf("======================\n\n");
if(fp)
{
while((fgets(line, sizeof(line), fp) != NULL))
{
sscanf(line, "%d %44s %24s %f\n", &drinkRec.stockCode,
drinkRec.drinkName, drinkRec.manufacturer,
&drinkRec.purchasePrice);
if(addDrinkArray( drinkArray, drinkRec))
++count;
else
{
printf("Array is full: ");
break;
}
}
fclose(fp);
}
else
printf("Error opening file");
printf("%d records succsefffully added\n", count);
}
The second function is to validate user input - I need a float for
purchasePrice. I have a similar one for an int.
I want to be able to accept a newline as valid when this function is used to
get input to update a record ie "Enter a purchase price or press enter to
leave unchanged" so I have set a flag. If the flag is true, the return
signals whether a newline was entered or not.
int getValidPurchasePrice(float* purchasePrice, int bypass)
{
char line[255];
char* test;
int retval = 1;
while(1)
{
printf("Please enter the purchase price ");
fgets(line, sizeof(line), stdin);
*purchasePrice = strtod(line, &test);
if(isspace(*test) && line[0] != '\n')
{
break;
}
else if(line[0] == '\n' && bypass != 0)
{
retval = 0;
break;
}
line[strlen(line) - 1] = '\0';
printf("%s is not a valid purchase price\n", line);
}
return retval;
}
Thanks in advance
Sean Kemplay
I am after any pointers you may have to improve these two functions.
They both work but are part of an assignment - I am looking for feedback.
The first one reads records from a file - the teacher won't allow a
delimeted file he wants spaces between words joined with an underscore.
The records are read into an array of structs:
typedef struct drink
{
int stockCode;
char drinkName[45];
char manufacturer[25];
float purchasePrice;
} DRINK;
void option1(DRINK drinkArray[], DRINK drinkRec)
{
FILE* fp = fopen("products.txt", "r");
char line[255] = "";
int count = 0;
printf("\nRead records from file\n");
printf("======================\n\n");
if(fp)
{
while((fgets(line, sizeof(line), fp) != NULL))
{
sscanf(line, "%d %44s %24s %f\n", &drinkRec.stockCode,
drinkRec.drinkName, drinkRec.manufacturer,
&drinkRec.purchasePrice);
if(addDrinkArray( drinkArray, drinkRec))
++count;
else
{
printf("Array is full: ");
break;
}
}
fclose(fp);
}
else
printf("Error opening file");
printf("%d records succsefffully added\n", count);
}
The second function is to validate user input - I need a float for
purchasePrice. I have a similar one for an int.
I want to be able to accept a newline as valid when this function is used to
get input to update a record ie "Enter a purchase price or press enter to
leave unchanged" so I have set a flag. If the flag is true, the return
signals whether a newline was entered or not.
int getValidPurchasePrice(float* purchasePrice, int bypass)
{
char line[255];
char* test;
int retval = 1;
while(1)
{
printf("Please enter the purchase price ");
fgets(line, sizeof(line), stdin);
*purchasePrice = strtod(line, &test);
if(isspace(*test) && line[0] != '\n')
{
break;
}
else if(line[0] == '\n' && bypass != 0)
{
retval = 0;
break;
}
line[strlen(line) - 1] = '\0';
printf("%s is not a valid purchase price\n", line);
}
return retval;
}
Thanks in advance
Sean Kemplay