Hi, i have written a program for my assignment, however i need a little bit of help..

the program is written in c++ and im using the bloodshed compiler - www.bloodshed.net

i need to put some kind of validation in whereby - it calculates the duration of a call in minutes (done the calculation) but if the duration of a call is longer than 600 mins (10 hours) then it should give an error message and take you back to the beginning of the program - pasted below is the complete code for the program i have.. just the calculations for the calls are not working for some reason - if someone can have a look at these for me i'd be very grateful

#include <stdio.h>

const float hi_rate=1.50;
const float lo_rate=0.30;
const float max_dur=600;
const float wkday_disc=0.4;
const float wkend_disc=0.6;


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

main()
{   
    /* Set all counters to zero, needed to calculate total summary */    
    total_calls=0;
    total_dur=0;
    total_cost=0;
    printf("\n\t\tEnter The Day:   ");
    scanf("%c",&day);   
    
    /* Program control loop - program ends if 'x' is entered */
   while (day != 'x')
    {   
        /* Input call start and end times */
        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();    

        /* Calculate call duration */
        call_dur=(end_hr - st_hr)*60 + end_min - st_min;
    
       if (call_dur > max_dur)
       printf("\n\t\tTime not valid");  
                
       /* Calculate call costs */
       if (call_dur <= 3)
          call_cost = hi_rate;
         else
       if (call_dur > 3) 
          call_cost = (call_dur-3)*lo_rate + hi_rate;
         else
       if (st_hr <= 7 || st_hr >= 17)
          call_cost = ((call_dur*lo_rate) + hi_rate)*wkday_disc;
         else      
       if (day =='a' || day=='s')
          call_cost = ((call_dur*lo_rate) + hi_rate)*wkend_disc;     
         
       /* Calculate totals summary */
       total_calls = total_calls + 1;
       total_dur = total_dur + call_dur;
       total_cost = total_cost + call_cost;
    
       /* Switch used to display the day */
       switch (day)
         {
           case 'm': 
            printf("\n\t\tCall made on - Monday");
           break;
           case 'u':
            printf("\n\t\tCall made on - Tuesday");
           break;
           case 'w':
            printf("\n\t\tCall made on - Wednesday");
           break;
           case 't':
            printf("\n\t\tCall made on - Thursday");
           break;
           case 'f':
            printf("\n\t\tCall made on - Friday");
           break;
           case 'a':
            printf("\n\t\tCall made on - Saturday");
           break;
           case 's':
            printf("\n\t\tCall made on - Sunday");
           break; 
           default:
            printf("\n\t\tDay Not Recognised");
           break;
         }    
    /* Displays summary of current call */
    printf("\n\t\tDuration of call was %d minutes  ",call_dur);
    printf("\n\t\tCost of call was %4.2f ",call_cost);
    printf("\n\n\t\tEnter The Day:   ");
    scanf("%c",&day);  
    
    }
    /* Displays total summary of program run */
    printf("\n\t\tTotal number of calls: %d ",total_calls);
    printf("\n\t\tTotal duration of calls: %d minutes",total_dur);
    printf("\n\t\tTotal cost of calls: %4.2f ",total_cost);  
    getchar();
    getchar(); 
}

many thanks in advance

Recommended Answers

All 8 Replies

I compiled your code, and it works just fine. Only change main () to int main (). And the language seems to be C, not C++...

hi,

thanks, yes i know the code compiles fine, but i want to put some sort of control in there - whereby if the calculation for call duration is greater than 600 then i want the program to alert me and take me back to the beginning of the program

also - the calculations dont seem to be working as i want them to - so im wondering if the layout of my if statements are correct.

there are no syntax errors but just the processing layout maybe wrong? does this make sense?

lol

A kind of

if (call_dur > max_dur)
         printf("\n\t\tTime not valid");  
       
       else
       {
       //from /* Calculate call costs */

       //to scanf("%c",&day);
       }

? :D

>>also - the calculations dont seem to be working as i want them to - so im wondering if the layout of my if statements are correct.

How should your calculations work? I mean, what do you want them to output?

thanks so much frrossk!! you are a star!

//How should your calculations work? I mean, what do you want them to output?

right ... ok now here is how the calculations should work..

/* Calculate call costs */
       if (call_dur <= 3)
          call_cost = hi_rate;

ok that bit says - if call duration is less than or equal to 3 then call cost is hi rate - that one works fine

else
       if (call_dur > 3) 
          call_cost = (call_dur-3)*lo_rate + hi_rate;

this bit is if the call duration is greater than 3 then take the total call duration and - 3 from it.. then times it by the low rate and then add the high rate - this one works fine.. aswell..

else
       if (st_hr <= 7 || st_hr >= 17)
          call_cost = ((call_dur*lo_rate) + hi_rate)*wkday_disc;

now for this one.. its slightly different.. its saying if a call is placed before and including 7 or after 17:00 hours then calculate the rate as before but then put the wkday discount on.. - this doesnt work.. it just seems to be doing the calculation without any discount..

else      
       if (day =='a' || day=='s')
          call_cost = ((call_dur*lo_rate) + hi_rate)*wkend_disc;

this is the same as before except it doesnt matter about the time - but the days have to be either equal to saturday (a) or sunday (s) - but again i the output i get is without any discount...

basically what i want the program to do is.. give a 40% discount if the call is placed before 8am or after 6 pm, charge normal rates between 8am and 6pm and give a 60% discount on the entire weekend..


im sorry i know its all a bit messy :( -

Yes, I see now... I wasn't looking too deep in your code first time; but now I saw it's logical that your program never reach the

if (st_hr <= 7 || st_hr >= 17)
          call_cost = ((call_dur*lo_rate) + hi_rate)*wkday_disc;
         else      
       if (day =='a' || day=='s')
          call_cost = ((call_dur*lo_rate) + hi_rate)*wkend_disc;

part. You have first:

if (call_dur <= 3)
          call_cost = hi_rate;
         else
       if (call_dur > 3) 
          call_cost = (call_dur-3)*lo_rate + hi_rate;
       else ...

As long as call_dur is always <=3 or >3 (this means the whole range of real numbers), the program jumps over the other next "elses" and "ifs", to the

total_calls = total_calls + 1;

part. So you have to combine the conditions in if statements, so you can test if a call is between 7-17 hours saturday/sunday etc...

PS. I'm not a star...I prefer to stay on the ground... :lol:

thanks again, i realised that it wasnt actually getting to the other bits as the first condition was always being met..

so i rewrote the code to this.. but.. it still doesnt seem to work :(

/* Calculate call costs */
              if (day == 'a' || day == 's' && call_dur <= 3)
                 call_cost = hi_rate*wkend_disc;
              else
                 call_cost = ((call_dur-3*lo_rate) + hi_rate)*wkend_disc;
                        
              if (st_hr <= 7 || st_hr >= 17 && call_dur <= 3)
                 call_cost = hi_rate*wkday_disc;
              else
                 call_cost = ((call_dur-3*lo_rate) + hi_rate)*wkday_disc;
               
              if (call_dur <= 3)
                 call_cost = hi_rate;
              else  
                  call_cost = (call_dur-3)*lo_rate + hi_rate;

I think it should be more something like this:

if ((st<=7 || st >=17) && (day = 'a' || day = 's') && count <= 3)
   //some formula
else if ((st<=7 || st >=17) && (day = 'a' || day = 's') && count >= 3)
   //some formula
else if ((st<=7 || st >=17) && (day != 'a' && day != 's') && count <= 3)
   //some formula

since I understand the call_cost depends on day, on st_hr and call_dur in the same time.

thanks - but ive finally got it working!!! below is the code i have used..

thanks so much frrossk - really appreciate it! i'll buy u a beer lol

if (day == 'a' || day == 's' && call_dur <= 3)
                 call_cost = hi_rate*wkend_disc;
              else
              if (day == 'a' || day == 's' && call_dur > 3)
                 call_cost = ((call_dur-3*lo_rate) + (hi_rate))*wkend_disc;
              else          
              if (st_hr <= 7 || st_hr >= 17 && call_dur <= 3)
                 call_cost = hi_rate*wkday_disc;
              else
              if (st_hr <= 7 || st_hr >= 17 && call_dur > 3)
                 call_cost = ((call_dur-3*lo_rate) + (hi_rate))*wkday_disc;
              else 
              if (call_dur <= 3)
                 call_cost = hi_rate;
              else  
                 call_cost = (call_dur-3)*lo_rate + hi_rate;
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.