Link to home
Start Free TrialLog in
Avatar of Jdub
Jdub

asked on

Help with source code!

The following is the code I have so far.I am having trouble with the code to read the input file in and creating the output file. I know I am supposed to us fopen() and fclose() but I dont know how. I am also having trouble with the code for using the array of structures and calculating the duration of the calls and charge of the calls once the file has been read into the program.


#include <stdio.h>
#include <stdlib.h>
#define discount 0.15
#define full_price 0.25
#define fed_tax 1.08
#define TRUE 1
#define FALSE 0


void Minutes(int [], int [], int [], int [], double *, double [], int *);
void Bill(int [], double [], double, double [], int *);
void Customer_Array(int, int[], int [], int [], int [], int [], double [], double []);
void Customer_Summary(int, double [], double [], double *, double *, double *);
void Total_Summary(int, int, double, double, double);

main()
{

  char response;

  int  s_hr[10],
       s_min [10],
       e_hr[10],
       e_min[10],
       call_count = 0,
       call_numbers [10],
       total_calls = 0,
       total_customers = 0;

  double duration [10],
         cost [10],
         start,
         total_minutes = 0,
         total_amount = 0,
         total_tax =0;


  do
   {
    do
     {

      Minutes(s_hr, s_min, e_hr, e_min, &start, duration, &call_count);
      Bill(call_numbers, duration, start, cost, &call_count);

      printf("\n\nIs there another call (Y or N)? ");
      fflush(stdin);
      scanf("%c", &response);
     }
    while  ((response == 'y') || (response == 'Y'));
      Customer_Array(call_count, call_numbers, s_hr, s_min, e_hr, e_min, duration, cost);
      Customer_Summary(call_count, duration, cost, &total_minutes, &total_amount, &total_tax);

      total_calls += call_count;
      s_hr [10] = s_min [10] = e_hr [10] = e_min [10] = call_numbers [10] = 0;
      duration [10] = cost [10] = start = call_count = 0;

      total_customers += 1;
      printf("\n\nIs there another customer(Y or N)? ");
      fflush(stdin);
      response = getchar();
    }
   while ((response == 'y') || (response == 'Y'));
   Total_Summary(total_customers, total_calls, total_minutes, total_amount, total_tax);
 }


void Minutes(int s_hour[], int s_minute[], int e_hour[], int e_minute[], double *start, double duration[], int *n)
 {
  double end,
         call_time;

  *start = s_hour[*n] + (s_minute[*n]/60.0);
  end = e_hour[*n] + (e_minute[*n]/60.0);

  if (*start > end)
   call_time = ((24 - *start) + end) * 60;
   if (*start < end)
    {
     call_time = (end - *start) * 60;
    }
  duration[*n] = call_time;
 }

void Bill(int c_num[], double c_time[], double s_time, double costs[], int *n)
 {
  double charge;

  if ((s_time > 8) || (s_time < 18))
   if (c_time[*n] > 60)
    {
     charge = full_price * c_time[*n];
     charge = charge - (charge * .10);
    }
   else
    charge = full_price * c_time[*n];

  if ((s_time > 18) || (s_time < 8))
   if (c_time[*n] > 60)
    {
     charge = discount * c_time[*n];
     charge = charge - (charge * .10);
    }
   else
    charge = discount * c_time[*n];

  costs[*n] = charge;
  c_num[*n] = *n;
  ++*n;
 }

void Customer_Array(int call, int c_num[], int s_hour[], int s_minute[], int e_hour[], int e_minute[],
                    double duration[], double charge[])
 {
  int number;
   printf("\nCall  Start time    Ending time   Duration    Charge");
  for (number = 0; number < call; ++number)
   {
    printf("\n%3d%8d:%02d%11d:%02d%10.0f%11.2f", c_num[number] +1,
             s_hour[number], s_minute[number], e_hour[number],
              e_minute[number], duration[number], charge[number]);
   }
 }

void Customer_Summary(int call_count, double duration[], double cost[],
                      double *total_minutes, double *total_amount, double *total_tax)
 {
  int number;

  double customer_time, customer_charge;

  for (number = 0; number < call_count; ++number)
   {
    customer_time += duration[number];
    *total_minutes +=duration[number];
    customer_charge +=cost[number];
    *total_amount += cost[number];
   }

  printf("\n\nThe total number of calls is:          %d", call_count);
  printf("\nThe total time used in minutes is:      %.0f", customer_time);
  printf("\nThe total charge is:                    $%.2f", customer_charge);
  printf("\nThe total tax is:                      $%.2f", customer_charge * .08);
  printf("\nThe total amount due is:               $%.2f", customer_charge + (customer_charge * .08));

  *total_tax += customer_charge * .08;
 }

void Total_Summary(int total_customers, int total_calls, double total_minutes,
                   double total_amount, double total_tax)
 {
  printf("\nTOTAL SUMMARY\n");
  printf("\nThe total number of customers:     %d", total_customers);
  printf("\nthe total number of calls is:      %d", total_calls);
  printf("\nThe total amount of tiem is:       %.0f", total_minutes);
  printf("\nThe total amount is:              $%.2f", total_amount);
  printf("\nthe total tax is:                  $%.2f", total_tax);
 }
Avatar of AlexVirochovsky
AlexVirochovsky

jdub, wait a bit. Tomorrow i 'll
send you reply to Q(and for prev. too).
Avatar of Jdub

ASKER

Adjusted points to 100
Avatar of Jdub

ASKER

This is a sample of the input:
Williams S. Hunt
4
(202) 427-2129     8:15   9:30
(303) 481-4848     7:15  10:05
(425) 391-4941    17:55  18:25
(505) 913-9193    14:50  16:15
Avatar of Jdub

ASKER

Adjusted points to 150
You should use
scanf("%s", buf)
if you want the program to answer after reading input (in the case the smallest bit of input is a _line_).

That I remember there's no ANSI function that reads one-key only and then returns to the caller. You should stick with the programming environment you use (Unix console, Borland C++ OS-handling libraries, ...). In Windows it's _getch I think.
Or else you can use the scanf("%s", buf) and then handle
each(or the first of the characters) of the line read...
You should do fflush(stdin) _after_ reading line, but since you read one line you don't need it...
Also: you usually don't use fflush on stdin, it's a console input!

If you like more ask. If you believe this to be as good answer you may rate it as an answer, please.
ASKER CERTIFIED SOLUTION
Avatar of AlexVirochovsky
AlexVirochovsky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial