[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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);
 }
0
Jdub
Asked:
Jdub
  • 3
  • 2
1 Solution
 
AlexVirochovskyCommented:
jdub, wait a bit. Tomorrow i 'll
send you reply to Q(and for prev. too).
0
 
JdubAuthor Commented:
Adjusted points to 100
0
 
JdubAuthor Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JdubAuthor Commented:
Adjusted points to 150
0
 
yodanCommented:
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.
0
 
AlexVirochovskyCommented:
Next is you programm:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define discount 0.15
#define full_price 0.25
#define fed_tax 1.08
#define TRUE 1
#define FALSE 0

int Minutes(int s_hour, int s_minute, int e_hour, int e_minute,int *start);
void Customer_Summary(int call_count, long customer_time,double customer_charge);
double Bill(double c_time, int s_time);
void Total_Summary(int, int, double, double, double);
//1-st par: name of input,2-nd name of out
void main(int num, char *var[])
{
   char szNameI[255],szNameO[255];

   int total_calls = 0,
       total_customers = 0;

    long total_minutes = 0;
    double total_amount =0,total_charge = 0;
    double total_tax =0;

   if (num <= 1)
     strcpy(szNameI,"input.txt");
   else
     strcpy(szNameI,var[1]);

   if (num <= 2)
     strcpy(szNameO,"output.txt");
   else
     strcpy(szNameO,var[2]);
   clrscr();
   FILE *fIn  = fopen(szNameI,"rt");
   if (fIn)
     {
      FILE *fOut = fopen(szNameO,"wt");
      while (!feof(fIn))
        {
           char buff[81],szName[81],ac[10],phon[10];
           int number,i,hs,ms,he,me,start;
           double customer_charge = 0;
           long customer_duration = 0;

           if (!fgets(buff, sizeof(buff)-1, fIn))//1-st field: name
            break;
           strtok(buff, "\n");            //delete /n
           strcpy(szName,buff);            //form name
           if (!fgets(buff, sizeof(buff)-1, fIn))//
            break;//2-nd field: number
           number = atoi(buff);
           printf("\nCall     Start time    Ending time   Duration    Charge");
           for (i = 0; i < number; i++)
             {
             int start,duration;
             double charge;

             fgets(buff, sizeof(buff)-1, fIn);//next field: number + time
             strtok(buff, "\n");            //delete /n
             sscanf(buff,"%s %s %d:%d %d:%d",ac,phon,&hs,&ms,&he,&me);
             duration = Minutes(hs,ms,he,me,&start);
             charge = Bill(duration, start);
             printf("\n %s %s %02d:%02d %02d:%02d %10d %11.2f",ac,phon,hs,ms,he,me
                  ,duration, charge);
             customer_charge += charge;
             customer_duration += (long)duration;
             }
           total_customers++;
           total_calls += number;
           total_minutes += customer_duration;
           total_tax += customer_charge * .08;
           total_charge += customer_charge;

           Customer_Summary(number, customer_duration, customer_charge);
        }
      fclose(fIn);
      fclose(fOut);//print to fout you can make youself
     }
   total_amount = total_charge*1.08; //???
   Total_Summary(total_customers, total_calls, total_minutes, total_amount, total_tax);
 }

int Minutes(int s_hour, int s_minute, int e_hour, int e_minute,int *start)
 {
   int iStart = s_hour *24 + s_minute;
   int iEnd   = e_hour *24 + e_minute;

   if (iStart > iEnd)
     iEnd += 24*60;
   *start = iStart;
   return iEnd - iStart;
}
// s_time: start time
// c_time: duration
//return money
double Bill(double c_time, int s_time)
{
  double charge;

  if ( (s_time > 8) || (s_time < 18) )
    {
      charge = full_price * c_time;
      if (c_time > 60)
      charge -= charge * 0.1;
    }
  else
    {
      charge = discount * c_time;
      if (c_time > 60)
      charge -= charge * 0.1;
   }
  return charge;
}

void Customer_Summary(int call_count, long customer_time,double customer_charge)
 {
  printf("\n\nThe total number of calls is:          %d", call_count);
  printf("\nThe total time used in minutes is:      %ld", 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));
 }

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 time is:       %.0f", total_minutes);
  printf("\nThe total amount is:               $%.2f", total_amount);
  printf("\nthe total tax is:                  $%.2f", total_tax);
 }
Test file:
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
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

I hope, it helps. Alex
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now