[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Help with source code!

Posted on 1999-11-13
6
Medium Priority
?
273 Views
Last Modified: 2012-05-04
I am trying to write a program in C that simulate the billing of a telephone company. I am haveing trouble with the arrays to print out the summary for each customer. The summary should print the start and end times for each call. My code is included. Can You Help?!?
Thanks

#include <stdio.h>
#define reg_rate 0.25
#define discount 0.10
#define tax_rate 0.08
#define pm_discount 0.40

double Total_Time(double);
double Call_Cost(double);
double cost[10];
int    duration[10];

double tax, total_cost, sub_total, sub_total1, sub_total2, grand_total, cust_total, total_time;

int    response, hrs, min, end_hrs, end_min, cust_count, call_count, time_total,
       cust_time, cust_call_count;

main()
 {
  printf("\n*******Eastern Atlantic Telephone company*******\n\n");
  cust_count = 0;
  call_count = 0;
  grand_total = 0;
  time_total = 0;
  cust_time = 0;
  cust_call_count = 0;
 
  printf("\nDo you have a customer to process?");
  printf("\nEnter 1 for yes or 0 for No: ");
  scanf("%d", &response);
   while (response == 1)
    {
     cust_time = 0;
     cust_total = 0;
     ++cust_count;
     cust_call_count = 0;

     printf("\nEnter the start time of the call: ");
     scanf("%d:%d", &hrs, &min);
      if ((hrs >= 24) || (hrs < 0) || (min >=60) || (min < 0 ))
       {
        hrs = 0;
        min = 0;
        printf("\nThe time entered is incorrect!");
        printf("\nEnter the start time of the call: ");
        scanf("%d:%d", &hrs, &min);
       }

     printf("Enter the end time of the call: ");
     scanf("%d:%d", &end_hrs, &end_min);
      if ((end_hrs >= 24) || (end_hrs < 0) || (end_min >=60) || (end_min < 0 ))
       {
        end_hrs = 0;
        end_min = 0;
        printf("\nThe time entered is incorrect!");
        printf("\nEnter the end time of the call: ");
        scanf("%d:%d", &end_hrs, &end_min);
       }
   
     ++cust_call_count;
     ++call_count;

     total_time = Total_Time(total_time);
     total_cost = Call_Cost(total_cost);

     cust_total += total_cost;
     cost[10] = cust_total;
     time_total += total_time;
     cust_time += total_time;
     duration[10] = cust_time;

     printf("\nDo you have a another call to process?");
     printf("\nEnter 1 for yes or 0 for No: ");
     scanf("%d", &response);
      while (response == 1)
       {
        ++cust_call_count;
        ++call_count;

        printf("\nEnter the start time of the call: ");
        scanf("%d:%d", &hrs, &min);
         if ((hrs >= 24) || (hrs < 0) || (min >=60) || (min < 0 ))
          {
           hrs = 0;
           min = 0;
           printf("\nThe time entered is incorrect!");
           printf("\nEnter the start time of the call: ");
           scanf("%d:%d", &hrs, &min);
          }

        printf("Enter the end time of the call: ");
        scanf("%d:%d", &end_hrs, &end_min);
         if ((end_hrs >= 24) || (end_hrs < 0) || (end_min >=60) || (end_min < 0 ))
          {
           end_hrs = 0;
           end_min = 0;
           printf("\nThe time entered is incorrect!");
           printf("\nEnter the end time of the call: ");
           scanf("%d:%d", &end_hrs, &end_min);
          }

        total_time = Total_Time(total_time);
        total_cost = Call_Cost(total_cost);

     time_total += total_time;
     cust_total += total_cost;
     cost[10] = cust_total;
     cust_time += total_time;
     duration[10] = cust_time;

     printf("\nDo you have a another call to process?");
     printf("\nEnter 1 for yes or 0 for No: ");
     scanf("%d", &response);
    }
  printf("\n******Customer Summary Report******");

  printf("\nThe total number of calls is %d.", cust_call_count);
  printf("\nThe total time for this customer is %d minutes.", duration[10]);
  printf("\nThe total for this customer is $%.2f", cost[10]);

  grand_total += cust_total;

  printf("\nDo you have a customer to process?");
  printf("\nEnter 1 for yes or 0 for No: ");
  scanf("%d", &response);
 }
printf("\nThe Grand Total is $%5.2f.", grand_total);
printf("\nThe Total Number of calls is %d.", call_count);
printf("\nThe Total Time used by all customers is %d minutes.", time_total);
printf("\nThe Total Number fo customers is %d.", cust_count);
}

double Total_Time(double total_time)
{
 if ((end_min < min ) && (end_hrs > hrs))
  {total_time = ((end_min + 60) - min) + ((end_hrs - 1 - hrs) * 60);}
 else if ((end_min >= min) && (end_hrs >= hrs))
  {total_time = (end_min - min) + ((end_hrs - hrs) * 60);}
 else if ((end_min < min) && (end_hrs < hrs))
  {total_time = ((end_min + 60) - min) + (((end_hrs -1) - (hrs - 24)) * 60);}
 else if ((end_min > min) && (end_hrs < hrs))
  {total_time = (end_min - min) + ((end_hrs - (hrs -24)) * 60);}
 return total_time;
}

double Call_Cost(double total_cost)
{
 if ((hrs < 18) && (hrs>= 8) && (total_time >= 60))
  {
   sub_total = total_time * reg_rate;
   sub_total1 = sub_total - (sub_total * discount);
   tax = sub_total1 * tax_rate;
   total_cost = sub_total1 + tax;
  }
 else if ((hrs < 18) && (hrs >=8))
  {
   sub_total = total_time * reg_rate;
   tax = sub_total * tax_rate;
   total_cost = sub_total + tax;
  }
 else if ((hrs >= 18) && (total_time >=60))
  {
   sub_total = total_time * reg_rate;
   sub_total1 = sub_total - (sub_total * pm_discount);
   sub_total2 = sub_total1 - (sub_total1 * discount);
   tax = sub_total2 * tax_rate;
   total_cost = sub_total2 + tax;
  }
 else if ((hrs >= 18) || (hrs < 8))
  {
   sub_total = total_time * reg_rate;
   sub_total1 = sub_total - (sub_total * pm_discount);
   tax = sub_total1 * tax_rate;
   total_cost = sub_total1 + tax;
  }
 else if ((hrs < 8) && (total_time >= 60))
  {
   sub_total = total_time * reg_rate;
   sub_total1 = sub_total - (sub_total * pm_discount);
   sub_total2 = sub_total1 - (sub_total1 * discount);
   tax = sub_total2 * tax_rate;
   total_cost = sub_total2 + tax;
  }
 return total_cost;
}    
0
Comment
Question by:Jdub
  • 3
  • 2
6 Comments
 

Expert Comment

by:rdf
ID: 2210661
what language?
0
 

Author Comment

by:Jdub
ID: 2213121
Edited text of question.
0
 
LVL 24

Expert Comment

by:SunBow
ID: 2221958
What the h@ck does any bill look like anymore, from what company to who?

Is it the bill that is to be paid?

Or the calculations needed to assess the call?

(the former needs some "appearance" enhancements, and both have their legal aspects, while the latter is more continual)

it's nearly obviouds that length = end of call - begin, give or take some rings and pauses.  But, where do you get the information on costs (handling midnight is extra credit)? from where to where? WHat feeds you the timing information? (may need time zone adjustments)?

Do you feed this info in from a tape, are their live transactions occurring between the Bells? Are there different rates? Is the charge for the first minute the same as for the last minute?

Is a call from Germany to Australia the same rate as a call from Paris to London? Or from Rome to Rome?
----------
With this little info, I'd start with the bill to the house. Go get one, change a number here and there, then write up some printing code to send the numbers towards the printed page (bill), like a 'report', skipping all the math that would never get done.
0
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 

Author Comment

by:Jdub
ID: 2222384
Edited text of question.
0
 
LVL 24

Expert Comment

by:SunBow
ID: 2222908
Trusting you can handle the other parts, math, midnight tricks, and that is either done now or will be soon, the first appearance is that you do not want or should not want any numerics between the array brackets following the main() invocation.

This looks like you want to do like a 'for loop' for ten, exactly ten clients. From one to ten. Or, from the first customer to the last customer in words, or whatever.  Assuming also you'll do error handling, where for some reason you only had eight customers.

If that's close approx., then you only want a variable between the brackets. Once you invoke Main(). Not much time.  I'll have to check back later, I can't tell what variable, I think you're running at least two, one for customer, and one for call/for/this/customer.  

First look from the code is like you are only handing call #10 for cust #10.

I'll try closer look later, note to self, anyone else, my mention of variable for an array would have candidates like:

     ++cust_count;
     ++cust_call_count;
     ++call_count;

The Arrays:

  cost[10]     = cust_total;
  duration[10] = cust_time;


0
 
LVL 24

Accepted Solution

by:
SunBow earned 200 total points
ID: 2222975
OK, for q: on printing arrays, here's the array print part ref:

  printf("\nThe total time for this customer is %d minutes.", duration[10]);
  printf("\nThe total for this customer is $%.2f", cost[10]);

The latter, cost[10] looks like intent is cost for any customer, but it only does customer #10, so if you only enter nine customers, this is not telling you much now.  If the customer ID variable is : ++cust_count, then that should be your index:

  printf("\nThe total for this customer is $%.2f", cost[cust_count]);

I'm saying this the long way, to ensure you can adapt it elsewhere as needed.

Likewise, the former on duration per customer would go:

  printf("\nThe total time for this customer is %d minutes.", duration[cust_count]);

That's the print part, I'm done.  But this data is fed by math parts such as:

     cust_total += total_cost;
     cost[10] = cust_total;
     time_total += total_time;
     cust_time += total_time;
     duration[10] = cust_time;

Which must be similary changed.  If you are using this to track by customer, where cust_count indicates which one:

     cust_total += total_cost;
     cost[cust_count] = cust_total;
     time_total += total_time;
     cust_time += total_time;
     duration[cust_count] = cust_time;

Looks like you are only print simple summary, while considering a possibility to track line items for each customer:

     ++cust_call_count;
     ++call_count;

But only using these as a simple bill summary, not to be tracked in any array.

And then, are you printing the array...
That needs an index not seen.

Oh, I see, you ran a cheat, printing after figuring out each single customer.   So just plugging in the index gets you AN answer, but for THE answer you should move the client bill print code down out of the client loop. After you've determined no more customers to process. Otherwise you are not really using the array. This means you need another variable, at least I'd make another one, to keep it clear here, to indicate how many customers you have. So before exiting the main customer loop, make a new Total_Customers, so you can wrap the array printing in a separate while loop dedicated to the printer people, then you can reuse the ++cust_count to walk your way through the array until you can get to the last one, Total_Customers. Don't forget to reinitialize it.
This would likely have impact elsewhere:

printf("\nThe Total Number fo customers is %d.", cust_count);

of course. But you'll be correcting the spelling there anyways.

On errors, I was off on my prior message, having only eight customers is less important than having twelve, the way the data is gathered.  So don't let #11 ever show up (force the answer to your question "any more?" - do not allow that to be a choice.

OOPS, I reread the question as changed.  You DO want each call tracked after all. Well, I think my answer gets you there, but will try to address after a break if there's time, else I'll just submit as-is. This s/b enuf for you.
-----------------------break ----------

Skip the comment above on moving the print code for now to keep it simple. Don't print customer by array this day. You DO need another WHILE loop for printing the line items, either below the total (so customer can rip it off and mail it in) or above the total, like an adding machine would do in nice columns. I'd do the latter, put entire loop immediately after printing the line including "***Customer Summary".
In the math part, the
     cust_total += total_cost;
     cost[10] = cust_total;
     time_total += total_time;
     cust_time += total_time;
     duration[10] = cust_time;
Now becomes
 cust_total += total_cost;
 cost[cust_call_count] = cust_total;
 time_total += total_time;
 cust_time += total_time;
 duration[cust_call_count] = cust_time;

Where ++cust_call_count reflects single calls, it becomes the index for the array during the loop. The loop is exited when total calls for customer is achieved. The NEW printf's in the loop of course need this index as in:

 printf("\n .. minutes:", duration[cust_call_count]);

While you'll now need to dump the arrays from the bottom line per customer, for they are used now for line items. Something like:

  printf("\nThe total time for this customer is %d minutes.", cust_time);
  printf("\nThe total for this customer is $%.2f", cust_total);

That should be clear enough without having to write it all here.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question