mardi 26 juillet 2016

How Do I Add the Results of a Looped Input File?

I'm a beginner at C++ and created an input file with calculated call rates. I was able to calculate the cost of each call individually; however, I'm not sure how to...

  1. Take the results of each call and total them up together.

  2. Accurately calculate the costs of calls that span from day to nighttime/weekday to weekend.

This is what I have so far. Any help is much appreciated. Thank you!

Call_History.txt

(Day/Time/Duration/Cost)

Mo 1330 16 $6.40

Mo 815  35 $14.00

Tu 750  20 $3.00

We 1745 30 $12.00

Th 800  45 $18.00

Su 2350 30 $4.50

Code

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const double DAYTIME = 0.40;
const double NIGHT = 0.25;
const double WEEKEND = 0.15;

int main()
{
    ifstream fin;
    fin.open("Call_History.txt");

    string day;
    int time;
    int duration;
    int dayOfWeek;
    double cost;
    double total;

    // Set the numeric output formatting.
    cout << fixed << showpoint << setprecision(2);

    cout << "Day Time Duration Cost\n" << endl;

    while (fin >> day >> time >> duration)
    {
        if (day == "Mo")
        {
            dayOfWeek = 1;
        }
        else if (day == "Tu")
        {
            dayOfWeek = 2;
        }
        else if (day == "We")
        {
            dayOfWeek = 3;
        }
        else if (day == "Th")
        {
            dayOfWeek = 4;
        }
        else if (day == "Fr")
        {
            dayOfWeek = 5;
        }
        else if (day == "Sa")
        {
            dayOfWeek = 6;
        }
        else if (day == "Su")
        {
            dayOfWeek = 7;
        }

        // Determine cost of call based on rate schedule.
        if ((time >= 800) && (time <= 1800) && (dayOfWeek <= 5))
        {
            cost = duration * DAYTIME;
        }
        else if ((time < 800) && (time > 1800) && (dayOfWeek <= 5))
        {
            cost = duration * NIGHT;
        }
        else
        {
            cost = duration * WEEKEND;
        }
        cout << day << " " << time << " " << duration << " $" << cost << endl;

    }

    cout << "\nTotal $" << endl;

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire