Member Avatar for Member #1021832

Good afternoon guys,

I am reaching out for help on C++. I just started the semester taking the Computer programming 1 at my college which is taught in C++. I am working on a chapter project qusetion and I feel lost on how to achieve the solution using the basic knowledge on Chapter 3 so far. The book we are using is Problem solving with CPP 10th Edition. And here is the question:

  1. 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 am and 6:00 pm, Monday through Friday,
    is billed at a rate of $0.40 per minute.
    b. Any call starting before 8:00 am or after 6:00 pm, Monday through
    Friday, is charged at a rate of $0.25 per minute.
    c. Any call started on a Saturday or Sunday is charged at a rate of $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 pm is input as
    13:30
    The day of the week will be read as one of the following pairs of character
    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.

Here is my code so far, I was thinking of using enums so that the code will be able to identify the days of the week but I am kinda lost. As you can see as of now, I am just testing for Monday. However, I do not know how to make the code understand that the time starts from 8 AM to 6 PM. I can do 08:00 to 17:59. But to make to make it acknowledge 08:00 to 18:00 is rather hard since counting goes from 1 to 100. How can I create a simpler count from 1 - 60 as done with time ? I think if I can overcome this hurdle, then I can complete the code to work in how it works. Also, where can I use an enum?

// This program computes the cost of a long-distance call.
// Any call started between 8 am to 6 pm on Mon - Fri is billed @ $0.40
// Any call starting between 6:01 pm to 7:59 am on Mon - Fri is billed @ $0.25
// Any call started on a Saturday or Sunday is charged @ $0.15

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

// constants for the fixed costs
const double week_day = 0.40; // Mon - Fri from 8 am to 6 pm
const double week_nights = 0.25; // Mon to Friday 6:01 pm to 7:59 am
const double weekend = 0.25; // Saturday and Sunday rates
//enum days = {Monday = 'Mo', Tuesday = 'Tu', Wednesday = 'We', Thursday = 'Th', Friday = 'Fr', Saturday = 'Sa', Sunday = 'Su'};

int main(){
    // Declaration of the variables to be used
    int hour;
    int hrs;
    int minute;
    int mins;
    int call_mins;
    string time_input;
    string hour_part;
    string minute_part;
    char day_first;
    char day_second;


    cout << "Enter the time that the call was placed in 24-hour format eg. 13:30 for 1:30 PM: ";
    cin >> time_input;
    cout << endl << "You entered: " << time_input << endl;

    // Break the time input to hour and minutes
    //cout << time_input.find(':') <<endl;
    //cout << time_input.find(':') << endl;
    //cout << time_input.length() << endl;

    if ((time_input.find(':') < time_input.length()) && time_input.length() == 5) {
        hour = stoi(time_input.substr(0, time_input.find(':'))); // from str[0] to where : is found
        minute = stoi(time_input.substr(time_input.find(':') + 1)); // from : to str[str.length()]
        //cout <<"hours:"<<hours<<endl; // check for whether break was successful
        //cout<<"mins:"<<minutes<<endl; // check for whether break was successful

        // Nested if statements to make sure that our user's input falls into the normal time format
        if ((hour >= 0 && hour <=24) && (minute >= 0 && minute <= 59)){
            cout << hour << endl;
            cout << minute << endl;
            cout << endl;

            // If it does, ask the user to enter the length of the call
            // Lets ask the user for the number of minutes that the call lasted.
            cout << "How long was the call (in minutes)?: ";
            cin >> call_mins;
            cout <<endl << "You entered " << call_mins << " minutes" <<endl<<endl;
        }
        else{
            cout << "You entered an invalid input" << endl;
            // Insert input loop here
        }
    }
    else{
        cout << "You entered an invalid input" << endl;

    }
    // Determine when the call took place
    cout << "When did the call take place?: ";
    cin >> day_first >> day_second;
    day_first = tolower(day_first);
    day_second = tolower(day_second);
    //cout << "You entered " << day_first<<day_second; 

    // Here we are validating the day the call was made input
    if (day_first == 'm' && day_second == 'o'){ // Weekday
        cout << "Monday" << endl;
        if ((hour >= 8 && minute  >= 0) && (hour <= 18 && minute <= 59)){

            if (call_mins >= 60){
              //  do{ // Fix this loop
                hour = hour + (call_mins / 60);
                minute = minute + (call_mins % 60);
                while (minute >= 60){
                    cout << "start loop" << endl;
                    hour += minute / 60;
                    minute = minute % 60;
                }
               // } while (minute >= 60);
            }
        cout << "New hours: " << hour <<endl;
        cout << "New minutes: " << minute <<endl;
        cout << "Day shift"<< endl;

        }
        else if ((hour >= 18 && minute >= 01) && (hour <= { // Mostly lost here
            cout << "night shift";
        }
    }

    else if (day_first == 't'&& day_second == 'u'){ // Weekday
        cout << "Tuesday" << endl; 
    }
    else if (day_first == 'w' && day_second == 'e'){ // Weekday
        cout << "Wednesday" << endl;
    }
    else if (day_first == 't' && day_second == 'h'){ // Weekday
        cout << "Thursday" << endl;
    }
    else if (day_first == 'f' && day_second == 'r'){ // Weekday
        cout << "Friday" << endl;
    }
    else if (day_first == 's' && day_second == 'a'){ // Weekend
        cout << "Saturday" << endl;
    }
    else if (day_first == 's' && day_second == 'u'){ // Weekend
        cout << "Sunday" << endl;
    }
    else{
        cout << "The day input is invalid!!!";
    }

    return 0;
}

Thanks to everyone who will take the time to look at my code and contribute; and this is not homework, I am just trying to do all the chapter questions so that I have a strong grasp before moving to the next chapter.

Dani AI

Generated

A simpler, more robust approach is to decide the rate from the call start time only and work entirely in minutes. That avoids the headache of carrying minutes into hours and of trying to treat 08:00–18:00 as a mixed base. As suggested, convert the start time to "minutes since midnight", test weekday vs weekend, then pick the rate. Note: the weekend rate constant in the original code should be 0.15, not 0.25.

Example logic (illustrative):

const int DAY_START = 8 * 60;   // 480
const int DAY_END   = 18 * 60;  // 1080

int startMinutes = hour * 60 + minute;

if (day == "sa" || day == "su")
    rate = 0.15;
else if (startMinutes >= DAY_START && startMinutes < DAY_END)
    rate = 0.40;   // day
else
    rate = 0.25;   // night

double cost = callMinutes * rate;

Clarify inclusivity: the example treats 08:00 as day and 18:00 as night (i.e., [08:00,18:00) ). If the textbook expects 18:00 to be billed as day, change the comparison to <= DAY_END.

Where enums help: use an enum for readable categories (days or rate kinds). For example, enum RateKind { DAY_RATE, NIGHT_RATE, WEEKEND_RATE }; then set the enum from the checks above and map it to the numeric rate. Enums make switch statements and later extensions (different rates, holidays) clearer.

Quick tips: validate the time string (check ':', length, hour 0–23, minute 0–59), normalize day input to lowercase and compare the first two chars, use fixed+setprecision(2) when printing the cost, and wrap the whole calculation in a loop that asks whether to repeat. If instead the assignment required prorating a call that crosses rate boundaries, then splitting the call into segments (and computing end time) would be needed — but the phrase "Any call started..." implies a single-rate decision based on start time.

One suggestion. Just like we do with dollars and cents, you should change your time to just full minutes to save constantly juggling hours and minutes.

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.