Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,184 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,116 people online right now. Registration is fast and FREE... Join Now!




error with by ref and by val

 
Reply to this topicStart new topic

error with by ref and by val, using & vs *

rgfirefly24
31 Aug, 2008 - 02:53 PM
Post #1

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
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
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Error With By Ref And By Val
31 Aug, 2008 - 04:15 PM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
You've declared checkpackage as taking a reference. This means that the compiler will internally convert the passed argument to a pointer, so that it can be changed within the function; there is no need to pass the address of package to checkpackage. Just call it like this: if (checkpackage(package)) {}

Also, stylistically, I don't like the fact that checkpackage modifies the value passed in, as this is a side effect and not readily obvious. I would prefer to use the toupper(char c) function in the ctype library (#include <cctype>) to convert it as you pass it in. I'd do it something like this:

cpp

if (checkpackage(package = toupper(package))) { ... }

bool checkpackage(const char package)
{
static const char validPackages[] = { 'A', 'B', 'C' };
int i;
for (i = 0; i < sizeof(validPackages) / sizeof(validPackages[0]); ++i)
if (package == validPackages[i])
return true;
return false;
}

User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Error With By Ref And By Val
31 Aug, 2008 - 05:09 PM
Post #3

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
QUOTE(JackOfAllTrades @ 31 Aug, 2008 - 05:15 PM) *

You've declared checkpackage as taking a reference. This means that the compiler will internally convert the passed argument to a pointer, so that it can be changed within the function; there is no need to pass the address of package to checkpackage. Just call it like this: if (checkpackage(package)) {}

Also, stylistically, I don't like the fact that checkpackage modifies the value passed in, as this is a side effect and not readily obvious. I would prefer to use the toupper(char c) function in the ctype library (#include <cctype>) to convert it as you pass it in. I'd do it something like this:

cpp

if (checkpackage(package = toupper(package))) { ... }

bool checkpackage(const char package)
{
static const char validPackages[] = { 'A', 'B', 'C' };
int i;
for (i = 0; i < sizeof(validPackages) / sizeof(validPackages[0]); ++i)
if (package == validPackages[i])
return true;
return false;
}



that would work, but i have to change the original value inside the function, its required by the teacher and it has to be done using &
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Error With By Ref And By Val
31 Aug, 2008 - 05:31 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well Jack is right for the most part, just pass the variable name to your function, no appending of the & on the front. if (checkpackage(package)) {} after the call "package" will be altered by the function. You are passing by reference here since your function takes it in as char &.

If you don't believe us, print out the value of "package" variable before you call your function "checkpackage" and then afterwards. Make sure you pass it a lowercase value like 'a' and you will see that after your call the "package" variable will be 'A'.

smile.gif
User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Error With By Ref And By Val
31 Aug, 2008 - 06:55 PM
Post #5

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
ohh, i believe you guys, but the way that jack did it made it seem to me that he was having me make the change before sending it to the function. BTW, thanks taking off the & in the function call worked.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 03:58AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month