hi all, i need urgent help. i am currently learning c++ and i have been given a program to write, I am really struggling with it and as it is an assignment whic needs to be handed in tomorrow i really need help :( - im not lazy its just that im not grasping the concept very well at the moment and my tutor is too damn strict, wont give me help directley related to the assignment, can someone please help me out - below is the problem in detail. i am using the bloodshed compiler - http://www.bloodshed.net

I have to write a program that calculates charges for long-distance telephone calls according to the following scheme:

The regular charge rate is £1.50 for the first three minutes (or part thereof) and 30 pence for any minute after the first three. Rates are reduced during certain times of the day according to the following:

Monday to Friday
Before 8 a.m - 40% discount
8.am to 6pm (inclusive - regular rate)
after 6 pm - 40% discount

Saturday and Sunday
60% discount applies

the input to the program is to consist of the day on which the call was begun, the starting time of the call and the end time of the call. No call is to be greater than ten hours in duration.

the day is to be indicated by using a key letter: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.

the times are to be entered to the nearest minute using 24 hour clock format. each time should consist of two parts - hours and minutes. midnight is 00 hours and 00 minutes. 1.35 pm is represented as 13 hours and 35 minutes.

the program is to display the day of the week as a full name, the call duration in minutes and the total charge.

The program should execute in a loop and so consideration must be given to how the processing requirements are to end.

complete the program by including a summary analysis which will determine the total number of calls made, the total duration of the calls and the total cost of all the calls made

many thanks in advance

Dani AI

Generated

Good progress so far — already has the duration check working and 's tip fixed the single-letter day input. Below is a compact plan and a ready-to-run C++ pattern that closes the gaps seen in the thread: repeat-with-sentinel (do/while), robust validation for day/hour/minute, correct handling when the call crosses midnight, applying the “first 3 minutes or part thereof” rule, and applying discounts based on the day and the call start time. The day-letter scheme used in the thread is: M, U, W, T, F, A, S (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday).

Key processing steps (summary)

  • Read a day letter (or Q to quit); convert with toupper and map to a full name.
  • Read/validate start and end hours (0–23) and minutes (0–59).
  • Compute minutes-from-midnight for start and end; if end <= start add 24*60 to handle calls across midnight. Reject if duration > 600.
  • Compute base charge: 1.50 pounds for up to 3 minutes (or part thereof); for minutes > 3 add 0.30 per extra minute.
  • Apply discount: weekends (A or S) => 60% discount (pay 40%), weekdays before 08:00 or after 18:00 => 40% discount (pay 60%). Treat 08:00..18:00 inclusive as regular rate.
  • Keep running totals (calls, minutes, cost) and print a final summary when the loop ends.

Example C++ pattern (keeps things simple and portable):

#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>

using namespace std;

string dayName(char d) {
    switch (toupper(d)) {
        case 'M': return "Monday";
        case 'U': return "Tuesday";
        case 'W': return "Wednesday";
        case 'T': return "Thursday";
        case 'F': return "Friday";
        case 'A': return "Saturday";
        case 'S': return "Sunday";
        default:  return "";
    }
}

double baseCharge(int minutes) {
    if (minutes <= 3) return 1.50;
    return 1.50 + (minutes - 3) * 0.30;
}

double applyDiscount(double charge, char day, int startMin) {
    char D = toupper(day);
    if (D == 'A' || D == 'S') return charge * 0.4;        // weekend: pay 40%
    if (startMin < 8*60 || startMin > 18*60) return charge * 0.6; // off-peak weekdays
    return charge;
}

int main() {
    int totalCalls = 0, totalDur = 0;
    double totalCost = 0.0;
    char day;
    do {
        cout << "Enter day (M,U,W,T,F,A,S) or Q to quit: ";
        if (!(cin >> day) || toupper(day) == 'Q') break;
        string name = dayName(day);
        if (name.empty()) { cout << "Invalid day letter\n"; continue; }

        int sh, sm, eh, em;
        cout << "Start hour minute: "; if (!(cin >> sh >> sm)) break;
        cout << "End   hour minute: "; if (!(cin >> eh >> em)) break;
        if (sh<0||sh>23||eh<0||eh>23||sm<0||sm>59||em<0||em>59) {
            cout << "Invalid time\n"; continue;
        }

        int start = sh*60 + sm;
        int end   = eh*60 + em;
        int dur = end - start;
        if (dur <= 0) dur += 24*60;           // crosses midnight
        if (dur > 600) { cout << "Call exceeds 10 hours\n"; continue; }

        double charge = baseCharge(dur);
        charge = applyDiscount(charge, day, start);
        cout << name << " duration " << dur << " minutes cost " << fixed << setprecision(2) << charge << " pounds\n";

        totalCalls++; totalDur += dur; totalCost += charge;
    } while (true);

    cout << "\nSummary: " << totalCalls << " calls, " << totalDur << " minutes, total cost "
         << fixed << setprecision(2) << totalCost << " pounds\n";
    return 0;
}

Troubleshooting notes

  • Use cin >> day or scanf(" %c",&ch) (leading space) to avoid leftover-newline problems — pointed this out.
  • Test edge cases: exactly 3 minutes, start at 07:59, start at 18:00, calls crossing midnight, and durations of 600 minutes.
  • If the assignment actually requires prorating a call that crosses a rate boundary, split the interval minute-by-minute (or into segments) and apply the per-minute discount — that is more work but is the correct precise approach.

This pattern should slot into the code you already posted and gives a clean do/while structure plus the final summary aggregation.

Recommended Answers

All 10 Replies

this is what i have so far

#include <stdio.h>

const float hi_rate=1.50;
const float norm_rate=0.30;
const float max_dur=600;

char day;
int st_hr, end_hr, st_min, end_min, call_dur, total_calls, total_dur;
float call_cost, total_cost;

main()
{       
    /* printf("\n\t\tDay of call:   ");
    scanf("%",&day);*/
    printf("\n\t\tEnter Call Start Hour:   ");
    scanf("%d",&st_hr);
    printf("\n\t\tEnter Call Start Minute:   ");
    scanf("%d",&st_min);
    printf("\n\t\tEnter Call End Hour:   ");
    scanf("%d",&end_hr);
    printf("\n\t\tEnter Call End Minute:   ");
    scanf("%d",&end_min);
    getchar();    

    call_dur=(end_hr - st_hr)*60 + end_min-st_min;
    if (call_dur < max_dur)
    {    
    /*printf("\n\t\tday was: %   ",day);*/
    printf("\n\t\tDuration of call was %d minutes  ",call_dur);
    printf("\n\t\tCost of call was %d ",call_cost);
    }  
  else 
  printf("\n\t\tCall time exceeded");    


getchar();
}

I have the validation working so that it does not allow any calls to be more than 10 hours (600 minutes) long. But now i need to start using do while and nested if loops and i havn't got a clue where to start, if someone has a similar problem or examples of code - doesnt have to be this question specific, i can still work from that - i just don't know how to write from scratch..

please help..

thanks for that jwenting, i have had a look at the announcment - and have posted what i have so far - i am trying.. honestly! it's just that i can't seem do this on my own - im not asking anyone to write me the program, i just need a helping hand

I posted that link at the same time you were posting your code it seems...

What's so hard about using loops and conditionals?
Your courseware should have ample examples about them, and so does any programming book.

well - i just need help in knowing how to structure them, i mean i think i have understood the problem but just cannot seem to put it in code correctly - there is lots of validation that needs to be done and i cant figure how/where it should go.

for example - when entering a day, you only enter one letter but the output must be the full day name - i have no idea how to do this, there is sumthing i have found which is using the SWITCH statement, but i can't get it to work

#include <stdio.h>
main()
{ 
 int i;
  printf("Enter a day  ");
  scanf("%d",&i);

  switch (i)
   {
     case 1: 
      printf("Monday");
      break;
     case 2:
      printf("Tuesday");
      break;
     case 3:
      printf("Wednesday");
      break;
     case 4:
      printf("Thursday");
      break;
     case 5:
      printf("Friday");
      break;
     case 6:
      printf("Saturday");
      break;
     case 7:
      printf("Sunday");
      break;

     default:
      printf("Day Not Recognised");
      getchar();
   }  
getchar();
getchar();
}

like that piece of code will do what i want.. with the exception that i cannot enter a letter, i need to enter either 1-7 to display what i need - is there a better way of doing this using if statements? i dont know :confused:

please? anyone?? :(

please? anyone?? :(

Yes, sure...Your code works on my compiler; except you should use int main () . What kind of errors dou you get?

Yes, sure...Your code works on my compiler; except you should use int main () . What kind of errors dou you get?

Yeah, i mean the code works fine as it is.. but it doesn't quite work in the way i want it to.

I need to enter a single letter to display a day name .. ie

M= monday
U= tuesday
W= wednesday
T= thursday
F= friday
A= saturday
S= sunday

the switch i have got is fine, but it doesn't allow me to enter a character to display the name, it only allows me to use numbers 1-7 (which are the cases) I don't know if switch will accept characters in the case identifier as i cannot get it to work, i was wondering if there is any other way of doing what i want - ie - displaying a full day name from a single keystroke.. (make any sense)

char i;
//...
scanf("%c", &i);
//...
case 'w':
case 'W':
      printf("Wednesday");
      break;
//...

Good enough?

thank you, yes i have got that part of my program working fine now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.