|
-
Jan 29th, 2001, 03:58 PM
#1
Thread Starter
Hyperactive Member
I get errors when running this, please tell me whats wrong
#include <stdio.h>
void main(void)
{
int acct_num;
char use_code;
int peak_hrs;
int off_peak_hrs;
double charge;
float cost, cost2, total_cost;
void displayInstructions();
double computeCharge (char, int, int); /*declares function*/
FILE *in_file;
char f_name[13];
printf("\n file to be processed: ");
gets(f_name);
in_file = fopen (f_name, "r");
if (in_file == NULL)
{
printf("\n failed to open the file.\n");
exit (1);
}
while (fscanf(in_file, "%d %c %d %d ", &acct_num, &use_code, &peak_hrs, &off_peak_hrs) != EOF)
{
/*function called*/
charge = computeCharge(use_code, peak_hrs, off_peak_hrs);
if (charge == 0)
printf ("\n\n ACCT:%4d not processed. Invalid code entered:%c",acct_num, use_code);
else
printf ("\n ACCT#:%4d CUSTOMER CODE:%c", acct_num, use_code);
printf ("\n PEAK HRS:%4d OFF PEAK HRS:%4d",peak_hrs, off_peak_hrs);
printf ("\n TOTAL CHARGES:%4.2f \n",charge);
}
}
void displayInstructions()
{
char text;
FILE *IN_FILE;
in_file = fopen (text.dat, "r");
while (fscanf(in_file, " %c ", &text) != EOF)
{
printf("\n%c", text);
}
fclose(in_file);
}
/*computing charge function based on file input*/
double computeCharge(char x, int y, int z)
{
double charge;
float cost, cost2;
if (x == 'R' || x == 'r')
{
charge = ((y * 0.052) + 6.00);
}
else if (x == 'C' || x == 'c')
if (y > 1000)
{
charge = ((y - 1000) * 0.045) + 60.00;
}
else
{
charge == 60.00;
}
else if (x == 'I' || x == 'i')
{
if (y == 0 && z == 0)
{
charge = 10;
}
else if (y > 0 && z < 1000)
{
cost = 76;
if (z > 0 && z < 1000)
{
cost2 = 40;
}
else if (z > 0 && z > 1000)
{
cost2 = ((z - 1000) * 0.028 + 40);
}
charge = cost + cost2;
}
else if (y > 0 && y > 1000)
{
cost = ((y - 1000) * 0.065 + 76);
if (z > 0 && z < 1000)
{
cost2 = 40;
}
else if (z > 0 && z > 1000)
{
cost2 = ((z - 1000) * 0.028 + 40);
}
charge = cost + cost2;
}
}
else
{
charge = 0;
}
return (charge);
}
-
Jan 29th, 2001, 04:13 PM
#2
Monday Morning Lunatic
What errors do you get?
PS: put code in [ code ] [/ code ] tags (without the spaces).
One thing I noticed...you can't nest functions - put them separately, outside of main().
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 29th, 2001, 06:00 PM
#3
Thread Starter
Hyperactive Member
Errors Generated!
The Errors Generated Are As Follows:
Undefined Symbol 'in_file'
Structure required on left side of . or .*
heres the code again.
Code:
#include <stdio.h>
void main(void)
{
int acct_num;
char use_code;
int peak_hrs;
int off_peak_hrs;
double charge;
float cost, cost2, total_cost;
void displayInstructions();
double computeCharge (char, int, int); /*declares function*/
FILE *in_file;
char f_name[13];
printf("\n file to be processed: ");
gets(f_name);
in_file = fopen (f_name, "r");
if (in_file == NULL)
{
printf("\n failed to open the file.\n");
exit (1);
}
while (fscanf(in_file, "%d %c %d %d ", &acct_num, &use_code, &peak_hrs, &off_peak_hrs) != EOF)
{
/*function called*/
charge = computeCharge(use_code, peak_hrs, off_peak_hrs);
if (charge == 0)
printf ("\n\n ACCT:%4d not processed. Invalid code entered:%c",acct_num, use_code);
else
printf ("\n ACCT#:%4d CUSTOMER CODE:%c", acct_num, use_code);
printf ("\n PEAK HRS:%4d OFF PEAK HRS:%4d",peak_hrs, off_peak_hrs);
printf ("\n TOTAL CHARGES:%4.2f \n",charge);
}
}
void displayInstructions()
{
char text;
FILE *IN_FILE;
in_file = fopen (text.dat, "r");
while (fscanf(in_file, " %c ", &text) != EOF)
{
printf("\n%c", text);
}
fclose(in_file);
}
/*computing charge function based on file input*/
double computeCharge(char x, int y, int z)
{
double charge;
float cost, cost2;
if (x == 'R' || x == 'r')
{
charge = ((y * 0.052) + 6.00);
}
else if (x == 'C' || x == 'c')
if (y > 1000)
{
charge = ((y - 1000) * 0.045) + 60.00;
}
else
{
charge == 60.00;
}
else if (x == 'I' || x == 'i')
{
if (y == 0 && z == 0)
{
charge = 10;
}
else if (y > 0 && z < 1000)
{
cost = 76;
if (z > 0 && z < 1000)
{
cost2 = 40;
}
else if (z > 0 && z > 1000)
{
cost2 = ((z - 1000) * 0.028 + 40);
}
charge = cost + cost2;
}
else if (y > 0 && y > 1000)
{
cost = ((y - 1000) * 0.065 + 76);
if (z > 0 && z < 1000)
{
cost2 = 40;
}
else if (z > 0 && z > 1000)
{
cost2 = ((z - 1000) * 0.028 + 40);
}
charge = cost + cost2;
}
}
else
{
charge = 0;
}
return (charge);
}
-
Jan 29th, 2001, 10:26 PM
#4
Try this, I made some changes...Check the comments..
I got it to compile, put I didn't have time to study what it does, therefor I didn't play with it to see if it works.
Code:
#include <stdio.h>
#include <cstdlib> // need this for the exit function
void main(void)
{
int acct_num;
char use_code;
int peak_hrs;
int off_peak_hrs;
double charge;
float cost, cost2, total_cost;
void displayInstructions();
double computeCharge (char, int, int); /*declares function*/
FILE *in_file;
char f_name[13];
printf("\n file to be processed: ");
gets(f_name);
in_file = fopen (f_name, "r");
if (in_file == NULL)
{
printf("\n failed to open the file.\n");
exit (1);
}
while (fscanf(in_file, "%d %c %d %d ", &acct_num, &use_code, &peak_hrs, &off_peak_hrs) != EOF)
{
/*function called*/
charge = computeCharge(use_code, peak_hrs, off_peak_hrs);
if (charge == 0)
printf ("\n\n ACCT:%4d not processed. Invalid code entered:%c",acct_num, use_code);
else
printf ("\n ACCT#:%4d CUSTOMER CODE:%c", acct_num, use_code);
printf ("\n PEAK HRS:%4d OFF PEAK HRS:%4d",peak_hrs, off_peak_hrs);
printf ("\n TOTAL CHARGES:%4.2f \n",charge);
}
}
void displayInstructions()
{
char text;
//FILE *IN_FILE;
FILE *in_file;
// Is this what you meant to do...? in_file isn't declared
// nor is it passed into this fuction.... you created a FILE *IN_FILE
// above, didn't you mean FILE *in_file?
// Also, this line to C++ is looking at text.dat as a object and you are
// calling on one of it's methods.
//in_file = fopen (text.dat, "r");
in_file = fopen("text.dat", "r");
while (fscanf(in_file, " %c ", &text) != EOF)
{
printf("\n%c", text);
}
fclose(in_file);
}
/*computing charge function based on file input*/
double computeCharge(char x, int y, int z)
{
double charge;
float cost, cost2;
if (x == 'R' || x == 'r')
{
charge = ((y * 0.052) + 6.00);
}
else if (x == 'C' || x == 'c')
if (y > 1000)
{
charge = ((y - 1000) * 0.045) + 60.00;
}
else
{
charge == 60.00;
}
else if (x == 'I' || x == 'i')
{
if (y == 0 && z == 0)
{
charge = 10;
}
else if (y > 0 && z < 1000)
{
cost = 76;
if (z > 0 && z < 1000)
{
cost2 = 40;
}
else if (z > 0 && z > 1000)
{
cost2 = ((z - 1000) * 0.028 + 40);
}
charge = cost + cost2;
}
else if (y > 0 && y > 1000)
{
cost = ((y - 1000) * 0.065 + 76);
if (z > 0 && z < 1000)
{
cost2 = 40;
}
else if (z > 0 && z > 1000)
{
cost2 = ((z - 1000) * 0.028 + 40);
}
charge = cost + cost2;
}
}
else
{
charge = 0;
}
return (charge);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|