Text files and validation
Good Morning,
I have been pulling my hair out trying to understand how to implement the use of a text file in a section of code where functions are used for validation.
The add() function had the actual validation ranges instead of variables. The task at hand is to use an external file to pass in the validation to the variables min and max, which I added in place of the numbers. Perhaps I'm wrong here. Anyway, the text file should be as follows:
1 9999
1 5
1 50
5.00 900.00
6.00 1000.00
So, I removed the current validation values from my code and replaced them with the variables min and max within my add() function> Next I open my new text file and need to read the minimums and maximums into appropriate variables and use those variables in my calls to getint() and getreal(). This latter part is where I am totally lost.
I would appreciate any help in figuring this out. I don't know why I am so confused. I don't know how to 'pull' the data out of the text file and 'place' it in the variables. Especially since the type of variable changes (from int to double) in some places.
Here's my code:
Code:
/*--------------------------------------------------------------------------*/
/* */
/* Description: This program is to accept data entry and then display */
/* the results for a fictitious company. Multiple data */
/* entry to be accepted until operator quits. Data */
/* to be calculated and displayed. */
/* with validation: product number (1 - 9999) */
/* product type (1 - 5) */
/* quantity (1 -50) */
/* cost (5.00 - 900.00) */
/* price (6.00 - 1000.00) */
/* Add 7 functions: main(), menu(), add(), getreal(), */
/* getint(), total(), and show(). */
/* Include category array: 1 = Camping, 2 = Tennis, */
/* (with running totals) 3 = Golf, 4 = Snow sports, */
/* (display totals) 5 = Water sports */
/* Include product description: create new function */
/* getstring() that uses gets() for keyboard input, */
/* new function strcase() to convert desc. first */
/* letter of each word uppercase + balance lowercase, */
/* display new desc. in show(), create 2D array for */
/* showcosts() dept.name */
/* Convert init-costs() & show-costs() to use pointers */
/* Also, modify getstring() and all string functions */
/* Add limits.txt to be used for validation (remove old) */
/* */
/* Compiler: Microsoft Visual C++ 2010 Express (Windows XP) */
/* */
/*--------------------------------------------------------------------------*/
/* Disable warning messages 4996 - (scanf deprecated; use scanf_s) */
#pragma warning( disable : 4996 )
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define COMPANY "Sierra Sporting Goods" /* define company for potential multiple uses */
#define TRUE 1
int menu(void);
void add(void);
int getint(int min, int max, char prompt[]);
double getreal(double min, double max, char prompt[]);
double total(int q, double c);
void show(int prodNum, int prodType, int prodQuant, double prodCost, double prodPrice, char desc[]);
void title(void);
void message(char msg[]);
int quit(void);
void init_costs(double *c, int t);
void show_costs(double *c, int t);
void getstring(char *desc);
void strcase(char *desc);
char *department[] = {"Camping", "Tennis", "Golf", "Snow Sports", "Water Sports"};
main() /* main function */
{
int selection, active = TRUE;
do
{
selection = menu();
switch (selection)
{
case 1: add(); break;
case 2:
case 3:
case 4:
case 5: active = quit(); break;
}
}
while(active);
return 0;
}
/*========================menu()=================================================*/
int menu()
{
int choice;
title();
/* display menu items */
printf("\t1 = Add a record\n");
printf("\t2 = Report\n");
printf("\t3 = Delete a record\n");
printf("\t4 = Change a record\n");
printf("\t5 = Quit\n\n");
/* display selection question and validate */
choice = getint(1,5,"choice");
return (choice);
}
/*=======================add()==================================================*/
void add()
{
int prodNum, prodType, prodQuant;
double prodCost, prodPrice;
char choice;
double costTotalByType[6]; /* define array to hold total costs by type (0 != category */
char desc[40];
/* vars to have values read in from text file (Is this right?)*/
int min = 0, max = 0;
FILE *fp; /* create file pointer */
init_costs(costTotalByType, 6); /* initialize cost array to zero */
fp = fopen("limits.txt", "r"); /* open file for read only */
if (fp == NULL)
{
printf("File does not exist");
}
do
{
title();
/* display data entry items and take input */
/* what do I put here to get the values from the text file into the variables min and max? */
prodNum = getint(min,max,"Product Number");
prodType = getint(min,max,"Product Type");
getstring(desc);
prodQuant = getint(min,max,"Product Quantity");
prodCost = getreal(min,max,"Product Cost");
prodPrice = getreal(min,max,"Product Price");
fclose(fp); /* does this go here? */
costTotalByType[prodType] += prodCost;
show(prodNum, prodType, prodQuant, prodCost, prodPrice, desc);
show_costs(costTotalByType,6);
/* ask continue? */
printf("Continue (Y/N): ");
scanf("%c%*c", &choice);
choice = toupper(choice);
}
while (choice != 'N');
}
/* How do I modify the following two functions? */
/*=====================getint()====================================================*/
int getint(int min, int max, char item[])
{
int n;
printf("Enter the %s: %d to %d: ", item, min, max);
scanf("%d%*c", &n);
while (n < min || n > max)
{
message("\nChoice not in range.\a");
printf("Enter the %s: %d to %d: ", item, min, max);
scanf("%d%*c", &n);
}
return(n);
}
/*=====================getreal()====================================================*/
double getreal(double min, double max, char item[])
{
double n;
printf("Enter the %s: %.2f to %.2f: ", item, min, max);
scanf("%lf%*c", &n);
while (n < min || n > max)
{
message("\nChoice not in range.\a");
printf("Enter the %s: %.2f to %.2f: ", item, min, max);
scanf("%lf%*c", &n);
}
return(n);
}
/*===================show()======================================================*/
void show(int prodNum, int prodType, int prodQuant, double prodCost, double prodPrice, char desc[])
{
printf("\nThe product number is --> %0.4d", prodNum);
printf("\nThe product type is ----> %d", prodType);
/* convert description to proper case structure */
strcase(desc);
printf("\nThe description is -----> %s", desc);
printf("\nThe quantity is --------> %d", prodQuant);
printf("\nThe cost is ------------> %.2f", prodCost);
printf("\nThe price is -----------> %.2f\n", prodPrice);
/* display totals */
printf("\n\nTotal product price ----> %.2f", total(prodQuant, prodPrice));
printf("\nTotal product cost -----> %.2f", total(prodQuant, prodCost));
printf("\nTotal product profit ---> %.2f\n\n", (total(prodQuant, prodPrice) - total(prodQuant, prodCost)));
}
/*=================total()========================================================*/
double total(int q, double c)
{
return(q*c);
}
/*=====================message()====================================================*/
void message(char msg[])
{
printf("%s\n", msg);
}
/*====================title()=====================================================*/
void title()
{
/* clear screen */
system("cls");
/* display title */
printf("%s\n\n", COMPANY);
}
/*====================quit()=====================================================*/
int quit()
{
system("cls");
message("\n\tGoodbye!\n");
return 0;
}
/*=====================init_costs()==============================================*/
void init_costs(double *c, int t)
{
int i;
for (i = 1; i < t; i++)
{
*(c+i) = 0;
}
}
/*====================show_costs()===============================================*/
void show_costs(double *c, int t)
{
int i;
double total = 0;
printf("\nProduct Cost by Type\n\n");
printf("Type Dept Cost\n");
printf("----------------------------------\n");
for (i = 1; i < t ; i++)
{
printf("%2d %-13s %9.2lf\n", i, *(department+i-1), *(c+i));
total += *(c+i);
}
printf("\nTotal%29.2lf\n\n", total);
}
/*====================getstring()===========================================*/
void getstring(char *desc)
{
printf("Enter the description: ");
gets(desc);
}
/*====================strcase()=============================================*/
void strcase(char *desc)
{
int i = 1;
/* convert first ch to upper */
*(desc) = toupper(*desc);
while (*(desc+i) != (int)NULL)
{
*(desc+i) = tolower(*(desc+i)); /* convert to lower */
if (*(desc+i) == ' ') /* convert ch after space to upper */
{
i++;
*(desc+i) = toupper(*(desc+i));
}
i++;
}
}
/*=========================================================================*/