I'm having a problem with data files.
I figured out how to get my pointers and such to work while the program results were printed on the screen. However, when I try to switch over to datafiles (with an input and output file), my input file goes in (I think), and nothing comes out on my output file except for the "Total amount:" footer. No calculations were done at all.
I checked and my fscan and fprints were stated correctly, as far as I know (the other print statments are going on the compile screen, not the output file). I just can't figure out what I am missing. Here is my code.
Code:
#define ZERO 0.0
#define COMMISSION_RATE 0.144
#define BONUS_COMM_RATE 0.188
#define TRUE 1
#define FALSE 0
#define HIGH_SALESPERSON 7
#define ARRAY_SIZ 75
#define READ "r"
#define INPUT "inputfile7.in"
#define WRITE "w"
#define OUTPUT "outputfile7.out"
#include <stdio.h>
int input_fun(int *s_code, int *c_code, double *sale_amount, int
count, char *spv_str, char *cust_str, char *sale_str, char
*sale_err);
void output_fun(int *s_code, int *c_code, double
*sale_amount,double *sale_comm, int *flag, int number, double
saletotal, double commtotal,char *report_header, char
*column_header, char *bonus_mark, char *no_mark, char
*all_total, char *sale_count);
int sales_code(FILE *in, char *spn_str);
int cust_code(FILE *in, char *cust_str);
double sale(FILE *in, char *sale_str);
void sale_error(char *sale_err);
main()
{
int s_code[ARRAY_SIZ];
int c_code[ARRAY_SIZ];
double sale_amount[ARRAY_SIZ];
double sale_comm[ARRAY_SIZ];
int flag[ARRAY_SIZ];
char *spn_str = "Salesperson code (1-7)(Enter 0 to quit): ";
char *cust_str = "Customer code: ";
char *sale_str = "Sale amount in dollars and cents: ";
char *sale_err = "Salesperson code must be 1 to 7";
char *report_header = " SALES REPORT";
char *column_header = "SP CUST SALE COMM";
char *bonus_mark = "**";
char *no_mark = " ";
char *all_total = "Total: ";
char *sale_count = "Number of sales: ";
int num_sales = 0;
double total_sales, total_comm = 0;
int n = 0;
num_sales = input_fun(s_code, c_code, sale_amount, num_sales, spn_str,
cust_str, sale_str, sale_err);
for (n = 0; n < num_sales; n++)
{
if (sale_amount[n] > 1450)
{
flag[n] = TRUE;
sale_comm[n] = (208.8 + ((sale_amount[n] - 1450) * BONUS_COMM_RATE));
}
else
{
flag[n] = FALSE;
sale_comm[n] = (sale_amount[n] * COMMISSION_RATE);
}
total_sales += sale_amount[n];
total_comm += sale_comm[n];
}
output_fun(s_code, c_code, sale_amount, sale_comm, flag, num_sales,
total_sales, total_comm, report_header, column_header,
bonus_mark, no_mark, all_total, sale_count);
}
int
input_fun(int *s_code, int *c_code, double *sale_amount,
int
count, char *spn_str, char *cust_str, char *sale_str,
char *sale_err)
{
FILE *in;
if (( in = fopen(INPUT, READ)) == NULL)
{
printf("\nCannot open file\n");
exit(1);
}
s_code[count] = sales_code(in, spn_str);
while (!EOF)
{
if (s_code[count] <= HIGH_SALESPERSON && s_code[count] > ZERO)
{
c_code[count] = cust_code(in, cust_str);
sale_amount[count] = sale(in, sale_str);
count++;
s_code[count] = sales_code(in, spn_str);
}
else
{
sale_error(sale_err);
s_code[count] = sales_code(in, spn_str);
}
}
fclose(in);
return count;
}
void
output_fun(int *s_code, int *c_code, double *sale_amount,
double *sale_comm, int *flag, int number, double saletotal,
double commtotal, char *report_header, char *column_header,
char *bonus_mark, char *no_mark, char *all_total, char *sale_count)
{
int o = 0;
FILE *out;
out = fopen(OUTPUT, WRITE);
for (o = 0; o < number; o++)
{
fprintf(out, "\n%d %3d $%9.2lf", s_code[o], c_code[o], sale_amount[o]);
if (flag[o] == TRUE)
{
fprintf(out, "%s", bonus_mark);
}
else
{
fprintf(out, "%s", no_mark);
}
fprintf(out, " $%9.2lf", sale_comm[o]);
}
fprintf(out, "\n\n%s", all_total);
fprintf(out, "$%9.2lf $%9.2lf", saletotal, commtotal);
fprintf(out, "\n%s", sale_count);
fprintf(out, "%d", number);
fclose(out);
return;
}
int
sales_code(FILE *in, char *spn_str)
{
int sval;
fscanf(in, "%d", &sval);
return sval;
}
int
cust_code(FILE *in, char *cust_str)
{
int cval;
fscanf(in, "%d", &cval);
return cval;
}
double
sale(FILE *in, char *sale_str)
{
double amount;
fscanf(in, "%lf", &amount);
return amount;
}
void
sale_error(char *sale_err)
{
printf("%s", sale_err);
return;
}
The file is constructed like this
(saleperson number, customer number, sale amount)
xxx xx xxx.xx
xxx xx xxx.xx
and so on. While I know my other code is wrong for now, I feel that I have written the open and close file statements incorrectly. If they were correct, I would at least get some numbers before the program was thrown out of whack.
Also, I'm getting a "statement not reached" warning for this line
Code:
if (s_code[count] <= HIGH_SALESPERSON && s_code[count] > ZERO)
Is there a different way to write my file statements or is this actually the correct way? I'm pretty unsure at the moment.
Thanks in advance.
(The browser was really awful, so some of my code might be split up in a funky way (in particular on the function headers))