ok i've got a function with a prototype that takes in a char value and checks for a match, if it matches returns bool true. The error comes in how i'm referencing. I need to be able to change the value of the original variable depending on the return of true/false. i'm using the & by ref but not sure if i'm doing it right.
this is the error:
error C2664: 'checkpackage' : cannot convert parameter 1 from 'char *' to 'char &'
A reference that is not to 'const' cannot be bound to a non-lvalue
cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool checkpackage(char &);
int main()
{
fstream infile;
fstream outfile;
char file[9];
char file2[9];
cout << "Please enter the name of the file to read in: ";
cin.getline(file,10);
cout << "Please enter in the name of the file to save to: ";
cin.getline(file2, 10);
infile.open(file, ios::in);
outfile.open(file2, ios::out | ios::trunc);
if (!infile)
{
cout << "No file found.";
return 0;
}
else
{
int Maxhours[13] = {0,744,672,744,720,744,720,744,744,720,744,720,744};
float tot_bill, savings_A, savings_B, savings_C, Tot_hours, overage, over_hours;
const double A_MONTHY = 9.95;
const double B_MONTHY = 14.95;
const double C_MONTHY = 19.95;
const float A_OVERAGE_CHARGE = 2.00;
const float B_OVERAGE_CHARGE = 1.00;
char package, Date4, Date5;
int Date, Date2, Date3;
string cus_No;
infile >> Date >> Date4;
infile >> Date2 >> Date5;
infile >> Date3;
outfile << Date << Date4 << Date2 << Date5 << Date3 << endl << endl << endl;
if (Date >= 1 && Date <= 12)
{
while (!infile.eof())
{
infile >> cus_No;
infile >> package;
infile >> Tot_hours;
if (checkpackage(&package))
{
if (Tot_hours > Maxhours[Date])
{
outfile << "Invalid number reached in Used hours at customer number: " << cus_No << endl;
}
else
{
if (package == 'A' || package == 'a')
{
over_hours = Tot_hours - 10;
if (over_hours > 0)
{
overage = A_OVERAGE_CHARGE * over_hours;
}
else
{
overage = 0;
}
tot_bill = A_MONTHY + overage;
if ((Tot_hours - 20) > 0)
{
savings_B = tot_bill - (B_MONTHY + (B_OVERAGE_CHARGE * (Tot_hours - 20)));
}
else
{
savings_B = tot_bill - B_MONTHY;
}
savings_C = tot_bill - C_MONTHY;
outfile << "Customer number: " << cus_No << endl;
outfile << "Current Package: " << package << endl;
outfile << "Total hours used: " << Tot_hours;
outfile << endl << endl << endl;
outfile << "Monthly charge: \t\t\t\t" << A_MONTHY << endl;
if (over_hours > 0 )
{
outfile << "Overage charge: \t" << over_hours << "*" << A_OVERAGE_CHARGE << "\t\t\t" << overage << endl;
}
outfile << " \t\t\t\t----------" << endl;
outfile << "Total bill: \t\t\t\t" << tot_bill << endl << endl;
if (savings_B > 0)
{
outfile << "You could have saved: $" << savings_B << " by switching to package B" << endl;
}
if (savings_C > 0)
{
outfile << "You could have saved: $" << savings_C << " by switching to package C" << endl;
}
outfile << endl << endl;
outfile << & quot;___________________________________________________________________________
____" << endl;
}
else if (package == 'B' || package == 'b')
{
over_hours = Tot_hours - 20;
if (over_hours > 0)
{
overage = B_OVERAGE_CHARGE * over_hours;
}
else
{
overage = 0;
}
tot_bill = B_MONTHY + overage;
if ((Tot_hours - 10) > 0)
{
savings_A = tot_bill - (A_MONTHY + ((Tot_hours - 10) * A_OVERAGE_CHARGE));
}
else
{
savings_A = tot_bill - A_MONTHY;
}
savings_C = tot_bill - C_MONTHY;
outfile << "Customer number: " << cus_No << endl;
outfile << "Current Package: " << package << endl;
outfile << "Total hours used: " << Tot_hours;
outfile << endl << endl << endl;
outfile << "Monthly charge: \t\t\t\t" << B_MONTHY << endl;
if (over_hours > 0 )
{
outfile << "Overage charge: \t" << over_hours << "*" << B_OVERAGE_CHARGE << "\t\t\t" << overage << endl;
}
outfile << " \t\t\t\t----------" << endl;
outfile << "Total bill: \t\t\t\t" << tot_bill << endl << endl;
if (savings_A > 0)
{
outfile << "You could have saved: $" << savings_A << " by switching to package A" <<endl;
}
if (savings_C > 0)
{
outfile << "You could have saved: $" << savings_C << " by switching to package C" << endl;
}
outfile << endl << endl;
outfile << & quot;___________________________________________________________________________
____" << endl;
}
else if (package == 'C' || package == 'c')
{
tot_bill = C_MONTHY;
outfile << "Customer number: " << cus_No << endl;
outfile << "Current Package: " << package << endl;
outfile << "Total hours used: " << Tot_hours;
outfile << endl << endl << endl;
outfile << "Monthly charge: \t\t\t\t" << C_MONTHY << endl;
outfile << " \t\t\t\t----------" << endl;
outfile << "Total bill: \t\t\t\t" << tot_bill << endl << endl;
if (Tot_hours <= 15)
{
if (Tot_hours <= 10)
{
savings_A = tot_bill- A_MONTHY;
outfile << "You could have saved: $" << savings_A << " by switching to package A" << endl;
}
else
{
savings_A = tot_bill - (A_MONTHY + (Tot_hours - 10 * A_OVERAGE_CHARGE));
outfile << "You could have saved: $" << savings_A << " by switching to package A" << endl;
}
}
else if (Tot_hours > 12 && Tot_hours <= 25)
{
if (Tot_hours <= 20)
{
savings_B = tot_bill - B_MONTHY;
outfile << "You could have saved: $" << savings_B << " by switching to package B" << endl;
}
else
{
savings_B = tot_bill - (B_MONTHY + (Tot_hours - 20 * B_OVERAGE_CHARGE));
outfile << "You could have saved: $" << savings_B << " by switching to package B" << endl;
}
}
outfile << endl << endl;
outfile << & quot;___________________________________________________________________________
____" << endl;
}
}
}
}
}
else
{
cout << "Invalid date specified please check numbers and try again" << endl;
outfile << "Invalid date specified please check numbers and try again" << endl;
return 0;
}
}
return 0;
}
bool checkpackage(char &pckge) <-- error here
{
if (pckge == 'A' || pckge == 'a' || pckge == 'b' || pckge == 'B' || pckge == 'c' || pckge == 'C')
{
if (pckge == 'a')
{
pckge = 'A';
}
else if (pckge == 'b')
{
pckge = 'B';
}
else if (pckge == 'c')
{
pckge = 'C';
}
}
return true;
}
This post has been edited by born2c0de: 1 Sep, 2008 - 05:48 AM