I want to re-explain the problem, as I still do not have all the solutions.
The assignment is to modify a prior lab. Here are the relevant lines of unmodified code from the prior lab:
Code:
/*=======================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];
init_costs(costTotalByType, 6); /* initialize cost array to zero */
do
{
title();
/* display data entry items and take input */
prodNum = getint(1,9999,"Product Number");
prodType = getint(1,5,"Product Type");
getstring(desc);
prodQuant = getint(1,50,"Product Quantity");
prodCost = getreal(5.00,900.00,"Product Cost");
prodPrice = getreal(6.00,1000.00,"Product Price");
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');
}
/*=====================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);
}
The assignment instructions are to modify the prior Lab by adding code for manipulating a text file. The text file called limits.txt contains the validation ranges (minimum and maximum) for each validation of product number, product type, quantity, cost, and price. Place each validation range on a separate line, with minimum and maximum on each line separated by a space. The text file should look like this:
Code:
1 9999
1 5
1 50
5.00 900.00
6.00 1000.00
Then, I am to remove the current validation values from my code and within my add() function, open my new text file and read the minimums and maximums into appropriate variables and use those variables in the calls to getint() and getreal().
Here is what I came up with:
Code:
/*=======================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 */
int min= 0, max = 0;
FILE *fp; /* create file pointer */
init_costs(costTotalByType, 6); /* initialize cost array to zero */
fp = fopen("limits.txt", "rb"); /* open file for read only */
if (fp == NULL)
{
printf("File does not exist");
}
while (fscanf(fp, "%d%d", &min, &max) != EOF)
{
do
{
title();
/* display data entry items and take input */
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");
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);
fclose(fp);
}
while (choice != 'N');
}
}
/*=====================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);
}
When I run the program, I do not get the right values for validation for each input. I only get 1 and 9999 for the int operations and 1.00 9999.00 for the double operations.
Here is an example run:
Code:
Sierra Sporting Goods
Enter the Product Number: 1 to 9999: 1
Enter the Product Type: 1 to 9999: 1
Enter the description: nerf footballs
Enter the Product Quantity: 1 to 9999: 1
Enter the Product Cost: 1.00 to 9999.00: 1
Enter the Product Price: 1.00 to 9999.00: 2
The product number is --> 0001
The product type is ----> 1
The description is -----> Nerf Footballs
The quantity is --------> 1
The cost is ------------> 1.00
The price is -----------> 2.00
Total product price ----> 2.00
Total product cost -----> 1.00
Total product profit ---> 1.00
Product Cost by Type
Type Dept Cost
----------------------------------
1 Camping 1.00
2 Tennis 0.00
3 Golf 0.00
4 Snow Sports 0.00
5 Water Sports 0.00
Total 1.00
Continue (Y/N):
Obviously, the values from the text file are not being properly read, or something. Is it because of a change needed in my getint() and getreal() functions? I am confused and need some help to solve this.