-2

I have a problem trying to retrieve info from 2 structs. The first is:

struct PhoneCall{
    std::string date;       
    int minutes;            
    int seconds;

the second is :

struct Bill{
    Customer holder;                            
    Plan plan;
    PhoneCall callList[BILL_CALL_LIST_SIZE];                                
    int count;                                  
    long durationSeconds;                       
    int block;  

I have higlighted PhoneCall which has been created from the struct PhoneCall.

I need to solve the function

int GetTotalHours(Bill& bill)

Im finding it difficult to create pointer from bill to Phonecall . I get a message saying no conversion exists.

I have since attempted the following code which was kindly offered as a solution.( I had used something similar but it seems to return an address 001A11EF).

int total_hours = 0;

        for (int call = 0; call < BILL_CALL_LIST_SIZE; ++call)
        {
            total_hours += bill.callList[call].minutes / 60+ bill.callList[call].seconds / 3600;
        }
        return total_hours;

There is another function later to return total minutes, which is why the function the GetTotalHours is an int type.

I am incredibly new to programming and have jumped in the deep end but hope you can help :)

1 Answers1

0

Based on your struct, if you are given a Bill then GetTotalHours could be something like

double GetTotalHours(Bill const& bill)
{
    double total_hours = 0.0;
    for (int call = 0; call < BILL_CALL_LIST_SIZE; ++call)
    {
        total_hours += bill.callList[call].minutes / 60.0 + bill.callList[call].seconds / 3600.0;
    }
    return total;
}

If you have access to C++11, this could be written as

#include <iterator>
#include <numeric>
double GetTotalHours(Bill const& bill)
{
    return std::accumulate(std::begin(bill.callList),
                           std::end(bill.callList),
                           0.0,
                           [](double total_hours, PhoneCall const& call){ return total_hours + bill.callList[call].minutes / 60.0 + bill.callList[call].seconds / 3600.0; });
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • My apologies will the poor questioning. Please let me Clarify. The main aim of the program is to get the two cheapest phone bill plans. There are 2 more structs but for simplicity,I have left them out. – muppetblues Aug 23 '15 at 01:24
  • Thank you for your answer. i did try something similar, and i also tried your code but all I got was 001A11EF. Looks Like an address. is there something missing? – muppetblues Aug 23 '15 at 02:01