Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

function help w/ assignment.

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
vctsang
Asked:
vctsang
1 Solution
 
jtrunekCommented:
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
 
e12voltsdacCommented:
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
 
jtrunekCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

Thanks,

-VT
0
 
jtrunekCommented:
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
 
vctsangAuthor Commented:
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
 
vctsangAuthor Commented:
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
 
jtrunekCommented:
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
 
vctsangAuthor Commented:
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
 
moduloCommented:
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

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now