You guys are so helpful over here, that I thought I come ask you guys about how to go about this project. So here is what I have to do:
Write a telephone billing program. You will have to prompt for a phone rates file, the format of the file fields is as follows:
city name weekday rates
where city name is a char string up to 15 characters long,
the weekday rates consist of two doubles, the first double is the charge for the first minute, the second double is the charge for each additional minute
for example the first line in the phone rate file may be:
Sudbury .19 .09
the name of the city is Sudbury, the first minute of the call is 19¢ and each additional minute costs 9¢ during the weekday rate times
the weekday rates apply on Monday thru Friday from the hours 8am to 5pm
the evening rate has a discount of 35% from the weekday rates and applies Monday thru Friday and Sunday evening from 5pm to 11pm
the night and weekend rates have a discount of 60% from the weekday rates and applies from Monday to Sunday from 11pm to 8am, all day Saturday and Sunday 8am to 5pm.
after the phone rates file is read in, then prompt and read in the telephone calls file which has the following format:
city name; initial time of call; duration of call;
where city name is the name of the city
initial time of call is of the form day of the week hour:minute followed by am or pm e.g.
Monday 3:40am
or
Friday 12:35pm
duration of call is an integer representing the minutes of the call
example line from a calls file:
Rockport Tuesday 8:30am 13
the call was to the city of Rockport, the call started at Tuesday 8:30 am and lasted for 13 minutes.
compute the charge for each call and if you cross time boundaries you must charge different rates, for example if someone places a call on Wednesday 4:55pm ad the call lasts 10 minutes then 5 minutes of the call was during the weekday hours(charge for the first minute plus 4 times the additional minutes charge) and 5 minutes was during the evening minutes(charge for the first minute plus 4 times the additional minutes charge), then add both charges together, if you come across a city that is not in the phone rates file, disregard the entire line of data
prompt for a billing file where you will print out the cost of each call and at the end the total charge for the month, use the following format:
city name takes the first 20 columns left justified, day of the week takes the next 10 columns left justified, initial time of call takes the next 10 columns where the ‘:’ is placed in the 3rd column of the field, the call duration takes the next 5 columns right justified and the cost takes the next 10 columns right justified with a leading $, the last line totals the cost of all the calls, N.B. rounded up to the next higher penny for any fraction of a penny:
example:
Brockton Monday 9:30 PM 5 $0.64
FallRiver Tuesday 7:00 AM 7 $1.03
Sudbury Wednesday 12:00 PM 3 $0.37
Brockton Friday 4:55 PM 15 $2.10
Falmouth Saturday 10:30 AM 20 $2.71
Total $6.85
To test your program you can use the files phonerates.txt and calls.txt, but your program must handle any legitimate data files. Be sure to design your program in a modular fashion i.e. use plenty of functions and hardly any global variables.
And here are the contents of the text (txt) files:
NOTE : We are to assume that we will only have two text files to read, and read them in in the order given in the lab. The data will also always be set up to look like they do above.
I really haven't had a chance to start coding this. I just wanted to see if my thinking for getting the data from the file is correct or not.
So what I'm thinking is I'll first create a function that will obtain the name of the file, which will include the whole path. (I'm only in level one computer science right now so I don't know how to prompt just for the file name without the path.)
Next I'll create a function to open an input file, and then create two getData functions to read in the different types of data into dynamic arrays.
To open both the files, I will loop these functions, so I do not have to write code to prompt for a name, open, clear, and close the files more than once.
I'm very sorry for the double post, but I really need some help. I just started coding my project and for every function that I add, I test the code to eliminate possible coding errors (for when the program is complete). So far, everything in my code works except for my void getData() function. My program just keeps crashing. If anyone could show me what I am doing wrong, it would be greatly appreciated.
Below is the code that I have started for my billing program:
Code:
//******************************************************************************
//
// Project #2 - Telephone Billing Program
//
// Purpose: A program that computes phone billing information and writes it to
// a text file. Phone rates and phone call information is obtained by
// reading in two text files (phonerates.txt and calls.txt).
//
// Date Created: 05-05-07
//
// Date Modified: 05-05-07
//
//******************************************************************************
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#define cls system ("cls")
#define pause system ("pause")
using namespace std;
//
// Function prototypes begin here...
//
void obtainFileName (string &fN);
void openFileIn (ifstream &in, const string &fN);
void errReport (const string &fN, int errNo);
void getData1 (ifstream &in, string cName [], double fMinC [], double rMinC []);
int main ()
{
// Data declaration and definition and declaration of file handles
ifstream read;
string fileName,
cityName [9];
double firstMinCharge [9] = {0},
regMinCharge [9] = {0};
// Function calls start here
obtainFileName (fileName);
openFileIn (read, fileName);
getData1 (read, cityName, firstMinCharge, regMinCharge);
read.clear();
read.close();
// check to see that data was read in properly
cout << "Phone Bill Information from Phonerates File" << endl
<< "-----------------------------------------" << endl
<< endl;
for (int i = 0; i < 9; i++)
{
cout << setw(20) << cityName [i] << " " << setw(4)
<< firstMinCharge [i] << setw(4) << regMinCharge [i]
<< endl << endl;
}// end of for loop
// Ending the program
return 0;
}// end of main
//********************************************************************
//
// Function Name: obtainFileName
//
// Purpose: To get the name of the input or output file
//
// Input Parameters: none
//
// Output Parameters: fileName
//
// Return Value: none
//
//********************************************************************
void obtainFileName (string &fN)
{
cout << "Enter the file name including the path of the file: ";
getline( cin, fN );
cls;
return;
}// obtainFileName
//********************************************************************
//
// Function Name: openFileIn
//
// Purpose: To connect a file handle to an input file
//
// Input Parameters: fileName for input file
//
// Output Parameters: in - ifstream object for input file
//
// Return Value: none
//
//********************************************************************
void openFileIn (ifstream &in, const string &fN)
{
in.open(fN.c_str());
if (!in.is_open())
{
errReport(fN, 3);
}
return;
}// openFileIn
//********************************************************************
//
// Function Name: errReport
//
// Purpose: To tell user program could not open the file and
// terminates the program
//
// Input Parameters: fileName, errNo
//
// Output Parameters: none
//
// Return Value: none
//
//********************************************************************
void errReport (const string &fN, int errNo)
{
cerr << "Could not open file: " << fN
<< "\n\nProgram will now terminate!\n\n" << '\n';
pause;
cout << '\n';
exit (errNo);
}// errReport
//********************************************************************
//
// Function Name: getData1
//
// Purpose: To tell user program could not open the file and
// terminates the program
//
// Input Parameters: none
//
// Output Parameters: in - ifstream object for input file
// cityName [], firstMinCharge [], regMinCharge []
//
// Return Value: none
//
//********************************************************************
void getData1 (ifstream &in, string cName [], double fMinC [], double rMinC [])
{
int i = 0; // local variable
in >> cName [i] >> fMinC [i] >> rMinC [i++];
in >> ws;
while (!in.fail())
{
in >> cName [i] >> fMinC [i] >> rMinC [i++];
in >> ws;
}// end of while loop
return;
}// getData1
The file I am trying to read in is the phonerates file. It is the first dark red text in my first post.
It compiles, but fails at execution. After entering the path of the file, an error window will appear and nothing prints to the screen from the arrays.
I know that it has something to do with my getData() function, because I have tested everything else successfully.
Try this: remove the while loop in getdata and just read one line from the file. If you can do this and the data is getting back to the main program safely, the problem is likely that you are reading past the end of file somehow. If you cannot do this, the problem may be that you could not open the file in the first place or that your arrays are messed up somehow.
Try this: remove the while loop in getdata and just read one line from the file. If you can do this and the data is getting back to the main program safely, the problem is likely that you are reading past the end of file somehow. If you cannot do this, the problem may be that you could not open the file in the first place or that your arrays are messed up somehow.
When I get rid of the while loop, the first three data items on the first line print to the screen, but not in the right order. The city name, and first double should also be on the first line before the last double. However, my program didn't crash.
NM I didnt see the file example above, and forgot ws is not a variable, I really, really hate the >> notavariable operations (not your fault, but c++ guys made a mistake here)
Brockton Monday 9:30 PM 5 $0.64
name = brockton, ok
first double = 9, ok
second double = 30, ok (??) is this what you wanted?
ws eats a space, whatever??
next name = pm, not ok?
next double = 5, not ok?
next double = 0.64, not ok?
ws eats end of line, ok?
...
the entire read section seems to need work. Should you read the time as a single thing (possibly put into one of the time structures) ?
I was wondering if that could be it, so instead of chaining my arrays, I read each one in separately. When I did that, they printed properly to the screen. Then to test to make sure all the data would print properly, I put the three lines into a for loop (as I know how much data there is in my example text file). This also worked. Next I replaced the for loop with my original while loop, keeping all the data read ins separate and that worked as well.
For some reason my program did not like me doing this:
in >> cName [i] >> fMinC [i] >> rMinC [i++];
I am not sure why though, because I have done it this way in another program I wrote and it works fine.
Modified Project code (The code in red is what I changed to make the program work):
Code:
//******************************************************************************
//
// Project #2 - Telephone Billing Program
//
// Purpose: A program that computes phone billing information and writes it to
// a text file. Phone rates and phone call information is obtained by
// reading in two text files (phonerates.txt and calls.txt).
//
// Date Created: 05-05-07
//
// Date Modified: 05-05-07
//
//******************************************************************************
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#define cls system ("cls")
#define pause system ("pause")
using namespace std;
//
// Function prototypes begin here...
//
void obtainFileName (string &fN);
void openFileIn (ifstream &in, const string &fN);
void errReport (const string &fN, int errNo);
int getData1 (ifstream &in, string cName [], double fMinC [], double rMinC []);
int main ()
{
// Data declaration and definition and declaration of file handles
ifstream read;
int count;
string fileName,
cityName [9];
double firstMinCharge [9] = {0},
regMinCharge [9] = {0};
// Function calls start here
obtainFileName (fileName);
openFileIn (read, fileName);
count = getData1 (read, cityName, firstMinCharge, regMinCharge);
read.clear();
read.close();
//---------------------------------------------------------------------------
// check to see that data was read in properly
cout << "Phone Bill Information from Phonerates File" << endl
<< "-------------------------------------------" << endl << endl;
for (int i = 0; i < 9; i++)
{
cout << cityName[i] << " " << firstMinCharge [i] << " "
<< regMinCharge [i] << endl << endl;
}// end of for loop
//---------------------------------------------------------------------------
// Check for empty file
if (!count)
{
cout << "Input file " << fileName << " is empty."
<< "\n\nProgram will now terminate.\n" << '\n';
exit (0);
} // end of if-statement for empty file check
// Ending the program
return 0;
}// end of main
//********************************************************************
//
// Function Name: obtainFileName
//
// Purpose: To get the name of the input or output file
//
// Input Parameters: none
//
// Output Parameters: fileName
//
// Return Value: none
//
//********************************************************************
void obtainFileName (string &fN)
{
cout << "Enter the file name including the path of the file: ";
getline( cin, fN );
cls;
return;
}// obtainFileName
//********************************************************************
//
// Function Name: openFileIn
//
// Purpose: To connect a file handle to an input file
//
// Input Parameters: fileName for input file
//
// Output Parameters: in - ifstream object for input file
//
// Return Value: none
//
//********************************************************************
void openFileIn (ifstream &in, const string &fN)
{
in.open(fN.c_str());
if (!in.is_open())
{
errReport(fN, 3);
}
return;
}// openFileIn
//********************************************************************
//
// Function Name: errReport
//
// Purpose: To tell user program could not open the file and
// terminates the program
//
// Input Parameters: fileName, errNo
//
// Output Parameters: none
//
// Return Value: none
//
//********************************************************************
void errReport (const string &fN, int errNo)
{
cerr << "Could not open file: " << fN
<< "\n\nProgram will now terminate!\n\n" << '\n';
pause;
cout << '\n';
exit (errNo);
}// errReport
//********************************************************************
//
// Function Name: getData1
//
// Purpose: To tell user program could not open the file and
// terminates the program
//
// Input Parameters: none
//
// Output Parameters: in - ifstream object for input file
// cityName [], firstMinCharge [], regMinCharge []
//
// Return Value: none
//
//********************************************************************
int getData1 (ifstream &in, string cName [], double fMinC [], double rMinC [])
{
int i = 0;
in >> cName [i];
in >> fMinC [i];
in >> rMinC [i++];
in >> ws;
while (!in.fail())
{
in >> cName [i];
in >> fMinC [i];
in >> rMinC [i++];
in >> ws;
} // end of for loop
return (i);
}// getData1
This is the getData() function of the other lab I wrote:
Code:
//********************************************************************
//
// Function Name: getData
//
// Purpose: To read in the information from the input file
//
// Input Parameters: in - ifstream object a handle for the input file
//
// Output Parameters: in - ifstream object updated, month,
// array itemDescription, array startQty,
// array numSold, array itemUnit
//
// Return Value: --i
//
//********************************************************************
int getData (ifstream &in, string &m, string iD [], int sQ [],
int nS [], string iU [])
{
int i = 0;// local function variable for count in main function
in >> m;
in >> ws;
getline(in, iD [i]);
in >> sQ [i] >> iU [i] >> nS [i] >> iU [i++];
while (!in.fail())
{
in >> ws;
getline(in, iD [i]);
in >> sQ [i] >> iU [i] >> nS [i] >> iU [i++];
}// end of while statement
return (--i);
}// getData
EDIT:
ws is a stream manipulator that eats whitespace characters
Wrong file. Right now I am reading the phonerates file in. ;)
I haven't written my getData() function for the calls file yet. :)
I am kind of leaning towards a compiler bug, because it was freaking out with the friend key word in a classes lab I was working on (an earlier post I made).
However, my teacher said I had to use the VS 6.0 compiler cause the ones in school work. However, the labs aren't open long enough to actually be able to work on my project, so I am stuck with mine. :(
He said downloading service pack 6 should have fixed the bugs in my compiler, but it did absolutely nothing.
AFAIK, VC 6.0 is no longer supported for quite some time. SP6 fixed some bugs but many others were left unfixed.
So I've noticed. :(
Alrighty, now I am starting work on my next file read (call.txt from first post), and this one looks quite a bit trickier than the first. Is it possible to read in data into an array of structs? For example, for the initial time of the call (the third piece of data on one text line), I would create this struct:
Code:
struct time
{
int hour;
char colon;
int minute;
string meridian;
};
and then define an array for this struct in main:
Code:
struct time table [11];
Could I then read in the whole initial time from the text file by doing this?:
Code:
in >> table [i];
And how exactly would you pass the struct by reference to a function so that its data could be changed?
There's like only one page on structs in my textbook, and I couldn't find anything online, so I'm not really sure how they are used, although this would clean up all the arrays I would have to make if I didn't use one. :)
If this doesn't work, could I make arrays inside the struct and code each array
like this in my getData() function?
Code:
in >> time.hour >> time.colon >> time.minute >> time.meridian;
Note: My previous codes I have posted have my first getData() function, but not the one I am asking the question about right now (my second getData() function). However, my second one will be similar to the first.
EDIT:
This is my test attempt with structures using getData1(), but my program keeps crashing. If anyone could tell me what I am doing wrong it would be greatly appreciated. :) :
Code:
//******************************************************************************
//
// Project #2 - Telephone Billing Program
//
// Purpose: A program that computes phone billing information and writes it to
// a text file. Phone rates and phone call information is obtained by
// reading in two text files (phonerates.txt and calls.txt).
//
// Date Created: 05-05-07
//
// Date Modified: 05-07-07
//
//******************************************************************************
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#define cls system ("cls")
#define pause system ("pause")
using namespace std;
//
// Global variables begin here
//
double const EVENING_DISCOUNT = 0.35; // 35% off weekday charge rates
double const NIGHT_AND_WEEKEND_DISCOUNT = 0.60; // 60% off weekday charge rates
//
// struct declaration
//
struct File1
{
string cityName [9];
double firstMinCharge [9];
double regMinCharge [9];
};
//
// Function prototypes begin here...
//
void obtainFileName (string &fN, int l);
void openFileIn (ifstream &in, const string &fN);
void errReport (const string &fN, int errNo);
int getData1 (ifstream &in, File1 &rates);
int main ()
{
//
// Data declaration and definition and declaration of file handles
//
ifstream read;
string fileName;
File1 ratesFile; // holds the data read in by the phonerates file
int count; // checks for empty file
//
// Function calls start here
//
obtainFileName (fileName, loop);
openFileIn (read, fileName);
count = getData1 (read, ratesFile);
// Check for empty file
if (!count)
{
cout << "Input file " << fileName << " is empty."
<< "\n\nProgram will now terminate.\n" << '\n';
exit (0);
} // end of if-statement for empty file check
read.clear();
read.close();
//---------------------------------------------------------------------------
// check to see that data was read in properly
cout << "Phone Bill Information from Phonerates File" << endl
<< "-------------------------------------------" << endl << endl;
for (int i = 0; i < 9; i++)
{
cout << ratesFile.cityName[i] << " " <<ratesFile.firstMinCharge[i]
<< " " << ratesFile.regMinCharge[i] << endl << endl;
} // end of for loop
// Ending the program
return 0;
} // end of main
//********************************************************************
//
// Function Name: obtainFileName
//
// Purpose: To get the name of the input or output file
//
// Input Parameters: loop
//
// Output Parameters: fileName
//
// Return Value: none
//
//********************************************************************
void obtainFileName (string &fN)
{
cout << "Enter the name and path of the phonerates text"
<< " file: ";
getline( cin, fN );
cls;
return;
} // obtainFileName
//********************************************************************
//
// Function Name: openFileIn
//
// Purpose: To connect a file handle to an input file
//
// Input Parameters: fileName for input file
//
// Output Parameters: in - ifstream object for input file
//
// Return Value: none
//
//********************************************************************
void openFileIn (ifstream &in, const string &fN)
{
in.open(fN.c_str());
if (!in.is_open())
{
errReport(fN, 3);
}
return;
} // openFileIn
//********************************************************************
//
// Function Name: errReport
//
// Purpose: To tell user program could not open the file and
// terminates the program
//
// Input Parameters: fileName, errNo
//
// Output Parameters: none
//
// Return Value: none
//
//********************************************************************
void errReport (const string &fN, int errNo)
{
cerr << "Could not open file: " << fN
<< "\n\nProgram will now terminate!\n\n" << '\n';
pause;
cout << '\n';
exit (errNo);
} // errReport
//********************************************************************
//
// Function Name: getData1
//
// Purpose: To store the data from the phonerates text file into
// dynamic arrays
//
// Input Parameters: none
//
// Output Parameters: in - ifstream object for input file
// cityName [], firstMinCharge [], regMinCharge []
//
// Return Value: i
//
//********************************************************************
int getData1 (ifstream &in, File1 &rates)
{
int i = 0;
in >> rates.cityName[i];
in >> rates.firstMinCharge[i];
in >> rates.regMinCharge[i++];
in >> ws;
while (!in.fail())
{
in >> rates.cityName[i];
in >> rates.firstMinCharge[i];
in >> rates.regMinCharge[i++];
in >> ws;
}
return (--i);
} // getData1
The problem is that you include a string object in a struct. You need to replace the string object with a char array, or make the struct a class. How is the string store in the file exactly? I assume it's some kind of a char array. In that case, you can't just write those bytes into meridien. You need to convert them to a string first.
The problem is that you include a string object in a struct. You need to replace the string object with a char array, or make the struct a class. How is the string store in the file exactly? I assume it's some kind of a char array. In that case, you can't just write those bytes into meridien. You need to convert them to a string first.
Awe man, strings are incompatible with structs? That means I'll have to make an array of structs instead. Thanks for the help.
it career
Why are you not using VC++ debugger, which line is it crashing?
I'm not very good with the debugger so I wasn't sure what was causing the problem, but after I entered the path and opened the input file, nothing would print to the screen and then an error message window popped up. Just like the error that I took of screen shot of earlier in this thread.
Bookmarks