Solved

# function help w/ assignment.

Posted on 2003-03-21
Medium Priority
335 Views
Hi, I am learning C++ and need some help w/ functions.
The following are the requirements for my assignment.
Any help will be greatly appreciated. Thanks,  -VT

input file:

Oct_04_parking.dat
1 1.5
2 4.0
2389 24.0
55 10.0
175 15.5
99999

A parking garage charges a \$2.00 minimum fee to park for up to three hours.  The garage charges an additional \$0.50 per hour for each hour or part thereof.  The maximum charge for any given 24-hour period is \$10.00.  Assume that no car parks for longer than 24 hours at a time.  Write a program that will calculate and print (to a file) the parking charges for each customer.

The customer records will come from the file Oct_04_parking.dat which will include the customer number and hours parked for a set of customers.  The final record will be 99999 and will indicate an end of file.  Your program should create an output file called Oct_4_parking_bills.dat  this file should show a neat tabular format of each customer's charge with a total of the day's receipts.

The program should use the function calculateCharges to determine the charges for each customer.   Also, use a function called printTotals to print the total line of you receipts.

Sample output:

Customer            Hours           Charge
---------------------------------------------------------------------------
1                   1.5             2.00
22                  4.0             2.50
2389                24.0            10.00
---------------------------------------------------------------------------
TOTAL               29.6            14.50

#include <iostream>
using std::cout;
using std::cin;
using std::ios;
using std::cerr;
using std::endl;
using std::left;
using std::right;
using std::fixed;
using std::showpoint;

#include <fstream>

using std::ifstream;
using std::ofstream;

#include <iomanip>
using std::setw;
using std::setiosflags;
using std::setprecision;

void printTotal (double hours);

int main ()

{

int customer;
double hours;
// double total;

ofstream outFile("oct_4_parking_bills.dat", ios::out );

ifstream inClientFile( "oct_04_parking.dat", ios::in );

if ( !inClientFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
}

outFile << left << setw( 19 ) << "Customer" << setw( 25 )
<< "Hours" << "Charge" << endl;
outFile     << "----------------------------------------------------" << endl;

while ( customer != 99999 )
{
inClientFile >> customer;
inClientFile >> hours;

outFile << setiosflags (ios::fixed | ios::showpoint);

outFile     << setiosflags (ios::left)
<< setw (20) << customer
<< setiosflags (ios::left)
<< setw (25) << setprecision(1) << hours << endl;
<< printTotal(hours) << setprecision(2) << endl; // return function don't work
}
// need to calculate to Total also.
//
inClientFile >> customer;
inClientFile >> hours;

double totalhours;
double totalcharge;

totalhours = totalhours + hours;
totalcharge = totalcharge + charge;

outFile << "----------------------------------------------------" << endl;
outFile << left << "TOTAL" <<"              " << totalhours << "                    "
<< totalcharge
<< endl;

void printTotal (double hours);
{
if ( hours <= 3 )
hours=2;
return 1;
else
return 2+(hours-3)*.5;

}
return 0;

}

0
Question by:vctsang
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 1

Expert Comment

ID: 8183588
The problem with the code posted is just a simple mistake.  The return type on the function should be double, not void.  Once you have that it should print out fine.  Just need to calculate the totals then.
0

Expert Comment

ID: 8184307
make the return type of printTotal an int and fix your if statement like this:

int printTotal (double hours);

...

int printTotal (double hours);
{
if ( hours <= 3 )
{
hours=2;
return 1;
}
else
return 2+(hours-3)*.5;

}
return 0;

}

and fix all the places wher you put <<left<< to:

<< setiosflags(ios::left) <<

that should be good.
0

LVL 1

Expert Comment

ID: 8184498
e12voltsdac,
2 minor things.  The return type needs to be a float or double since hours could be fractional.  Also, setiosflags(ios::left) is now generally outdated and was the way to do it with the old headers.  Most of the new headers define "<< left <<" to acomplish the same thing.
0

Author Comment

ID: 8195024
I changed the function statement and still getting this error from compiler when I execute it. "--------------------Configuration: assignment - Win32 Debug--------------------
parking.obj : error LNK2001: unresolved external symbol "int __cdecl printTotal(double)" (?printTotal@@YAHN@Z)
Debug/assignment.exe : fatal error LNK1120: 1 unresolved externals

assignment.exe - 2 error(s), 0 warning(s)"

Thanks,

-VT
0

LVL 1

Expert Comment

ID: 8195561
That means that you have the prototype "int printTotal(double hours)" declared.  However, when you try linking (a step in the compilation process) it is unable to find a function with that signature.  Make sure that all the types match between the prototype and the actual function.
0

Author Comment

ID: 8204963
I spent some more time and playing around w/ it and it seems to be working but my last record suppose to be 99999 and it did not have any input for hours/2nd column.  I tried w/ !=99999, <=99999 with while loop but is printing
the vaule of last column 15.5. Please check output at the bottom.

Thanks,

-VT

#include <iostream>
using std::cout;
using std::cin;
using std::ios;
using std::cerr;
using std::endl;
using std::left;
using std::right;
using std::fixed;
using std::showpoint;

#include <fstream>

using std::ifstream;
using std::ofstream;

#include <iomanip>
using std::setw;
using std::setiosflags;
using std::setprecision;

#include <cmath>

double printTotal(double hours);

int main ()

{

int customer;
double hours;
// double charge;

ofstream outFile("oct_4_parking_bills.dat", ios::out );

ifstream inClientFile( "oct_04_parking.dat", ios::in );

if ( !inClientFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
}

outFile << left << setw( 19 ) << "Customer" << setw( 25 )
<< "Hours" << "Charge" << endl;
outFile     << "----------------------------------------------------" << endl;

double totalhours;
double totalcharge;
totalhours = 0;
totalcharge = 0;

while ( customer != 99999 )
{
inClientFile >> customer;
inClientFile >> hours;

outFile << setiosflags (ios::fixed | ios::showpoint);

outFile     << setiosflags (ios::left)
<< setw (20) << customer
<< setiosflags (ios::left)
<< setw (25) << setprecision(1) << hours
<< setprecision(2) << printTotal(hours) << endl;

totalhours = totalhours + hours;
totalcharge = totalcharge + printTotal(hours);
}

outFile << "----------------------------------------------------" << endl;
outFile << left << "TOTAL" <<"               " << setprecision(1) << totalhours << "                     "
<< setprecision(2) << totalcharge
<< endl;

return 0;
}

double printTotal (double hours)
{
if ( hours <= 3 )
return 2;

if ( hours > 19 )
return 10;
else
{
hours = ceil ( hours );
return 2+(hours-3)*.5;
}
}

input file;
1 1.5
2 4.0
2389 24.0
55 10.0
175 15.5
99999

output file;
Customer           Hours                    Charge
----------------------------------------------------
1                   1.5                      2.00
2                   4.0                      2.50
2389                24.0                     10.00
55                  10.0                     5.50
175                 15.5                     8.50
99999               15.5                     8.50
----------------------------------------------------
TOTAL               70.5                     37.00

0

Author Comment

ID: 8205023
I can change the while loop like this and it takes out the 99999 but how can I put a statement that is the last record will be 99999.

while ( inClientFile >> customer >> hours )
0

LVL 1

Accepted Solution

jtrunek earned 2000 total points
ID: 8205136
When I tried running it, i lost some of the records, and the 9999 stayed in there.  Heres what I suggest.  Keep the loop condition as customer !=99999, but do a priming read first, then move the other read to the end of the loop like this:

inClientFile >> customer;
inClientFile >> hours;

while ( customer != 99999 )
{
outFile << setiosflags (ios::fixed | ios::showpoint);

outFile     << setiosflags (ios::left)
<< setw (20) << customer
<< setiosflags (ios::left)
<< setw (25) << setprecision(1) << hours
<< setprecision(2) << printTotal(hours) << endl;

totalhours = totalhours + hours;
totalcharge = totalcharge + printTotal(hours);

inClientFile >> customer;
inClientFile >> hours;
}

By doing it this way, nothing will be printed if the first record happens to be 9999, otherwise, since the read is at the end of the loop, it will pick up the 9999 but when it evaluates the loop condition, the loop will terminate since the condition is false, thus never processing that record.
0

Author Comment

ID: 8304471
ITUNEK,  THANKS FOR YOUR TIME &  HELP.  I TOOK YOUR SUGGESTIONS AND PLAYED AROUND W/ THE COMPILER AND FINALLY COMPLIED AND RAN IT W/ NO ISSUE.  -VT
0

Expert Comment

ID: 8401706
Dear expert(s),

A request has been made to close this Q in CS:
http://www.experts-exchange.com/Community_Support/Q_20597273.html

Without a response in 72 hrs, a moderator will finalize this question by:

- Accepting the comment from jtrunek as the answer

Thank you.

modulo

Community Support Moderator
Experts Exchange
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there isâ€¦
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why tâ€¦
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month10 days, 20 hours left to enroll