I am going to post the header file and the implementation file so maybe somebody can help me figure out why this is not working. I am trying to input data from a file into an array of records, but it does not seem to be working properly. Anyone with suggestions I would appreciate it. I have commented out some functions because I was trying to identify the bugs.
#include <iostream>
#include <fstream>
#include <string>
#include "calls.h"
usingnamespace std;
/**********************************************************
Name: Default constructor
Precondition: call_DB and count have not been initialized
Postconditon: Elements stored in the input file have been
entered into call_DB. The value of count is equal
to the number of elements stored in call_DB.
Description: Enter records from a user-specified datafile
into the array call_DB.
***********************************************************/
CALLS::CALLS()
{
this->call_DB = NULL;
cout<<"Default Constructor has been called.\n";
ifstream input ("call_data.txt");
string filename;
CAPACITY = 5;
call_DB = new call_record[CAPACITY];
count = 0;
//cout<<"Enter the filename: ";
//cin>>filename;
//input.open(filename.c_str());
while(!input.eof() && (*this).count < this->CAPACITY)
{
if (!input.eof())
{
input>>this->call_DB[count].cellPhoneNumber;
input>>this->call_DB[count].relays;
input>>this->call_DB[count++].minutes;
}
}
input.close();
}
/****
Name: CALLS destructor
Precondition: call_DB has dynamic memory
Postconditon: call_DB's dynamic memory has been de-allocated
entered into call_DB. The value of count is equal
to the number of elements stored in call_DB.
Description: Enter integers from a user-specified datafile
into the array call_DB.
***********************************************************/
CALLS::~CALLS()
{
cout<<"The destructor has been called.\n";
delete [ ] call_DB;
}
/**********************************************************
Name: search_DB
Precondition: call_DB and key have been initialized.
Postconditon: key location has been found; if not, -1
is return
Description: Search for key inside call_DB
***********************************************************/
int CALLS::search_DB(string key) const
{
for(int i=0; i<count; i++)
{
if (call_DB[i].cellPhoneNumber==key)
{
return i;
}
}
return -1;
}
/**********************************************************
Name: process_data_DB
Precondition: call_DB and key have been initialized.
Postconditon: Tax rate, net cost, tax on call, and total cost of call are calculated.
Description: Function calculates the rest of the information for the phone record.
***********************************************************/
void CALLS::process_data_DB()
{
for(int i=0; i<count; i++)
{
if (1<=call_DB[i].relays&&call_DB[i].relays<=5) //calculates tax rate
call_DB[i].taxRate = .01;
elseif (6<=call_DB[i].relays&&call_DB[i].relays<=11)
call_DB[i].taxRate = .03;
elseif (12<=call_DB[i].relays&&call_DB[i].relays<=20)
call_DB[i].taxRate = .05;
elseif (21<=call_DB[i].relays&&call_DB[i].relays<=50)
call_DB[i].taxRate = .08;
elseif (51>=call_DB[i].relays)
call_DB[i].taxRate = .12;
elseif (call_DB[i].relays <= 0)
call_DB[i].taxRate = 0;
call_DB[i].netCost = ((0.40 * call_DB[i].minutes)*(call_DB[i].relays/50.0)); //makes calculations for the cost of the phone calls
call_DB[i].taxOnCall = (call_DB[i].netCost * call_DB[i].taxRate);
call_DB[i].totalCostOfCall = (call_DB[i].netCost + call_DB[i].taxOnCall);
}
}
/**********************************************************
Name: call_Stats
Precondition: call_DB and key have been initialized.
Postconditon: Prints formatted call stat to the screen.
Description: User inputs cell phone number to be printed to the screen.
***********************************************************/
void CALLS::call_Stats(const string & key) const
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
call_record total;
total.relays=0;
total.minutes=0;
total.netCost=0;
total.taxOnCall=0;
total.totalCostOfCall=0;
for(int i=0; i<count; i++)
{
if(key==call_DB[i].cellPhoneNumber)
{
cout<<"\t"<<call_DB[i].cellPhoneNumber<<"\t";
cout<<call_DB[i].relays<<"\t";
cout<<call_DB[i].minutes<<"\t";
cout<<call_DB[i].netCost<<"\t";
cout<<call_DB[i].taxRate<<"\t";
cout<<call_DB[i].taxOnCall<<"\t";
cout<<call_DB[i].totalCostOfCall<<endl;
total.relays+=call_DB[i].relays;
total.minutes+=call_DB[i].minutes;
total.netCost+=call_DB[i].netCost;
total.taxOnCall+=call_DB[i].taxOnCall;
total.totalCostOfCall+=call_DB[i].totalCostOfCall;
}
}
cout<<"Totals\t\t\t";
cout<<total.relays<<"\t";
cout<<total.minutes<<"\t";
cout<<total.netCost<<"\t\t";
cout<<total.taxOnCall<<"\t";
cout<<total.totalCostOfCall<<"\t"<<endl;
}
/******************************************************
Name: IsFull
Precondition: None
Postcondtion: True is return if call_DB is full; otherwise false
Description: Determine if call_DB is full
********************************************************/
bool Is_Full(const CALLS & current)
{
return current.count == current.CAPACITY;
}
/******************************************************
Name: IsEmpty
Precondition: None
Postcondtion: True is return if call_DB is empty; otherwise false
Description: Determine if call_DB is empty
********************************************************/
bool Is_Empty(const CALLS & current)
{
return current.count == 0;
}
/******************************************************
Name: add_DB
Precondition: key contains the value to be added to call_DB
Postcondtion: key has been added to call_DB if call_DB is not full
Description: Adds cell phone number, relays, and minutes to call_DB.
********************************************************/
//void CALLS::Add_DB(const string & key)
//{
//
//
//
//
// if (!Is_Full(*this))
// {
// call_DB[count++].cellPhoneNumber=key;
// }
// else
// {
// double_DB();
// }
//
//
//}
/******************************************************
Name: remove_DB
Precondition: key contains the value to be removed from call_DB
Postcondtion: key has been removed from call_DB if call_DB is not empty
Description: Removes key from call_DB if it is there
********************************************************/
//void CALLS::remove_DB(const string & key)
//{
// cout<<"Inside remove_DB\n";
//
// int i= search_DB(key);
//
//
// while(i!=-1)
// {
// if (i!=-1)
// {
// for(int j=i; j<count-1; j++) //shifting up
// {
// call_DB[j] = call_DB[j+1];
// }
// count--;
// }
//
// else if (count == 0)
// {
// cout<<"Can't delete "<<key<<" because call_DB is empty\n";
// }
//
// i= search_DB(key);
// }
//
//}
/**********************************************************
Name: print_DB
Precondition: none
Postconditon: If call_DB was not empty its contents are printed
Description: This function prints the contents of call_DB to a
user-specified file.
***********************************************************/
void CALLS::print_DB() const
{
cout<<"print_DB has been called";
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
for(int i=0; i<(*this).count; i++)
{
cout<<call_DB[i].cellPhoneNumber<<"\t"<<"\n";
}
}
/******************************************************
Name: double_DB
Precondition: array call_DB needs to be doubled to make room for more records
Postcondtion: array call_DB doubled in size
Description: Doubles call_DB if it is needed
********************************************************/
//void CALLS::double_DB()
//{
// cout<<"Inside double_DB, original capacity = "<<CAPACITY<<endl;
//
// CAPACITY *= 2;
//
// cout<<"New CAPACITY = "<<CAPACITY<<endl;
//
// call_record *call_DB2 = new call_record[CAPACITY];
//
// for(int i=0; i<count;i++)
// {
// call_DB2[i]=call_DB[i];
// }
// count = CAPACITY;
//
// delete [] call_DB;
// call_DB=call_DB2;
//}
/////******************************************************
////Name: ostream & operator<<
////Precondition: A has been initialized
////Postcondtion: Sum of A has been computed and is returned
////Description: Determines the sum of all the element in A
////********************************************************/
ostream & operator<<( ostream & out, CALLS & original)
{
cout<<"Overloaded operator<< has been called\n";
for(int i=0; i<original.count; i++)
{
out<<original.call_DB[i].cellPhoneNumber<<endl;
}
return out;
}
One of the first suggestions I will make is to stop trying to do things like opening a file in your constructor. What happens if your file fails to properly open? Since this is a constructor you have very limited ways of dealing with this failure. Also never use eof() to control a data entry loop, you will usually get one extra line of input if you do. I also don't recommend reading a file in a class constructor, use a separate function to read the file and populate the class variables. The reason for this is because a constructor doesn't really have a way of telling you that it failed (until you start using exceptions) so delay populating the class until after your constructor because normal member functions can return error codes.
And by the way you should always check that your file opens correctly, and take proper action if it doesn't.
Next when you are accessing class member variables in a class member function you don't need to use the *this pointer, except if there is a scope issue. Also If you do use the *this pointer use it consistently, don't switch from (*this).something to this->something.