// Data file:
// LONG 12:00 13:52
// LOCAL 15:00 15:27
// LOCAL 16:10 18:40
// LOCAL 19:00 20:30
// LOCAL 22:20 23:20#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
unsigned short ExtractMinutes(string timeString)
{
unsigned short colonPosition=0, count, length, hours, minutes;
char minuteString[6];
char hourString[6];
length = timeString.size();
colonPosition = timeString.find(":");
if (colonPosition)
{
count = colonPosition;
strcpy(hourString, timeString.substr(0, count).c_str());
hours = atoi(hourString);
count = length - colonPosition - 1;
strcpy(minuteString, timeString.substr(colonPosition+1, count).c_str());
minutes = atoi(minuteString);
}
else
{
hours = 0;
count = length;
strcpy(minuteString, timeString.c_str());
minutes = atoi(minuteString);
}
return (hours*60 + minutes);
}
int main()
{
unsigned int local_mins, free_mins, total_mins;
float cost;
char file[50];
string word;
string start_time;
string stop_time;
int count=0;
cout.flags(ios::fixed);
cout << setprecision(2); //set up floating-pt output format
cout << "Please enter maximum number of free minutes for local";
cout << " calls per month: " << endl;
cin >> free_mins;
cout << "Please enter the cost per minute for local calls that";
cout << " exceed the maximum: " << endl;
cin >> cost;
cout << "Minutes = " << free_mins << ", Cost = " << cost << endl;
cout << "Please input name of file for processing: " << endl;
cin >> file;
ifstream infile;
infile.open(file);
if (!infile.good())
{
infile.clear(); // clear fail bit
cout << "Cannot open file" << endl;
cout << "Please input name of file for processing: " << endl;
cin >> file;
infile.open(file);
}
if (infile.good())
{
total_mins = 0;
// read first line here to set initial value for eof flag
infile >> word >> start_time >> stop_time;
while(!infile.eof())
{
if (word == "LONG")
{
cout << "Long Distance call." << endl;
}
else
{
cout << "Local call." << endl;
local_mins = ExtractMinutes(stop_time) - ExtractMinutes(start_time);
cout << "minutes = "<< local_mins << endl;
total_mins += local_mins;
}
cout << "Word = " << word << endl;
cout << "Start of call: " << start_time << ", Call end: " << stop_time << endl;
infile >> word >> start_time >> stop_time;
}
infile.close();
}
else
cout << "Cannot open file" << endl;
cout << endl;
cout << "local minutes = "<< total_mins << endl;
cout << "free minutes = "<< free_mins << endl;
cout << "charged minutes = "<< total_mins - free_mins << endl;
cout << endl;
cout << "charge per minute = "<< cost << endl;
cout << "charged total ($) = "<< (total_mins - free_mins)*cost << endl;
return 0;
}