You are here:

C++/help me!!!!


Write a program that computes the cost of a long-distance call. The cost of the call is determined
according to the following rate schedule:
a. Any call started between 8:00 a.m. and 6:00 p.m., Monday through Friday, is billed at
rate of RM 0.40 per minute.
b. Any call starting before 8:00 a.m. or after 6:00 p.m., Monday through Friday, is charged
at a rate of RM 0.25 per minute.
c. Any call started on a Saturday or Sunday is charged at a rate of RM 0.15 per minute.
The input will consist of the day of the week, the time the call started, and the length of the call in
minutes. The output will be the cost of the call. The time is to be input in 24-hour notation, so the time
1:30 p.m. is input as
The day of the week will be read as one of the following pairs of characters values, which are stored in
two variables of type char:
Mo Tu We Th Fr Sa Su
Be sure to allow the user to use either uppercase or lowercase letters or a combination of the two. The
number of minutes will be input as a value of type int. (You can assume that the user rounds the input to
a whole number of minutes). Your program should include a loop that lets the user repeat this
calculation until the user says she or he is done.

I will try to help you, but you have not told me what you have tried or what you are having trouble with or how much experience you have. I assume you donít want me to do the whole assignment for you.  Start by writing out what the program needs to do. At the highest level it needs to do two things.

1) Accept Input
2) Calculate cost

Then you need to refine each step and think about how the input will be used in the program. How it is used will affect how you represent the input. To accept input means to:

1.1) Accept a day. You know from the problem that the user is allowed to input any combination of upper-case and lower-case so its good to convert everything to lower case in your program. This will make checking the starting day easier.

1.2) Accept starting time. You know from reading the problem that you will have to compare your starting time with other numbers in your program and the starting time may be on a fraction of an hour, so maybe your starting time should be a floating point number. You know from the problem that the user will input the starting time in hours:minutes format so you will need some method to convert the string to a floating point value.

1.3) Accept a call duration. This will be in minutes so it can be an integer.
To calculate the cost you need three things: the starting day, the starting time, and the duration. Pretend the computer is a child that knows how to do arithmetic, and knows how to follow instructions, but knows nothing else. You have to tell that child how to do each step to calculate the cost. Write out what you would tell that child. Something like this:

Calculate Cost
Inputs are starting day, starting time, duration
Outputs are cost
if starting day is saturday or sunday then
       rate is 0.15
       if the call start time >= 8.0 or the call start time <= 18.0 then
         rate is 0.40
         rate is 0.25

cost is rate x duration

So far I've just taken the problem description an written it in a different way, but I hope you can see a program taking shape. I think the steps above are detailed enough that we can start writing code.

Lets start with step 1 Accept input. The problem states that the day must be stored in two variables of type char. That's not how I would do it but we must follow the rules of the problem. Input is the hardest part, because we cannot assume the user will input valid data and because we have to deal with the starting time in hours:minutes notation. It is also hard because you need to know many C functions that are not obvious. We can read all the input at once using the scanf function. You should read about the scanf and the tolower functions in your help system or do an internet search. The program is below. Notice that we are storing the starting time in a character array, but later we will need to treat it as a number so we need to find the semi-colon character in the starting time string and convert the hours and minutes into a floating point starting time. We do that with a separate function called timeToFloat. The timeToFloat function is complicated because it uses pointers. I don't know if you have experience with pointers, but feel free to ask more questions.

The program below accepts user input, and prints the user's input back to the screen. This will tell you if the input part is working properly. Once you are sure the input is working properly, and you understand the program so far, you can move on to the easy part, which is calculating the cost. When that is done, then add the loop to let the user do many calculations. I will continue to help you with the next steps, but you must show me an attempt at the function to calculate the cost. I have given you the function declaration.

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

float calculateCost(char daychar1, char daychar2, float startTime, int duration)
   float result = 0;

   return result;

float timeToFloat(char* timeText)
   char* pointerToSemicolon;
   char* pointerToHours;
   char* pointerToMinutes;
   float result;

   pointerToSemicolon = strchr(timeText, ':');
   if (pointerToSemicolon != NULL)
       *pointerToSemicolon = 0;

       pointerToHours = timeText;
       pointerToMinutes = pointerToSemicolon + 1;

       result = atof(pointerToHours) + atof(pointerToMinutes)/60;
       result = atof(timeText);

   return result;

int main(void)
   char daychar1;
   char daychar2;
   char startTimeString[6];
   float startTime;
   int duration;
   printf("Input DD HH:MM duration\n");
   if (scanf("%c%c %5s %d", &daychar1, &daychar2, &startTimeString, &duration) != 4)
       printf("Invalid input");
       daychar1 = tolower(daychar1);
       daychar2 = tolower(daychar2);
       printf("Your input Day1=%c Day2=%c startTimeString=%s duration=%d\n",
         daychar1, daychar2, startTimeString, duration);
       startTime = timeToFloat(startTimeString);
       printf("Your call start time in hours from midnight is %f\n", startTime);

       printf("The cost of tha call is %f\n",
         calculateCost(daychar1, daychar2, startTime, duration));

   return 0;


All Answers

Answers by Expert:

Ask Experts




No longer taking questions.


No longer taking questions.

No longer taking questions.

©2016 All rights reserved.