Getting Obscure Numbers For End Result! HELP!

Hi,

I have this program that I wrote to calculate electricity rates for class. The only problem was when I ran it I got a weird number. I started changing some things but I still can't get it to work. If anyone could just take a look at it and tell me how to correct it that would be great. Thanks!

PS: I think that the values in the Calculation function are not carrying over to the Main Function, thus not outputting the right values. Not sure how to go about it then.

#include <iostream>
#include <cmath>
using namespace std;

int Instruction();
int Valid();
double Calculation();
int BillPrint();

int Instruction()
{
cout<<"Welcome to the Bill Calculation Program."<<endl;
cout<<"Step 1:Enter Customer Name"<<endl;
cout<<"Step 2:Enter Account Number"<<endl;
cout<<"Step 3:Input User Code"<<endl;
cout<<"Step 4: Input kwh in residential users field and commerical while the commerical one should use off-peak and peak hours."<<endl;
cout<<"Step 5:Print out the bill."<<endl;

return 0;
}
int Valid(char UCode)
{
if (UCode =='R')

return 1;

else if (UCode =='C')

return 1;

else if (UCode =='I')

return 1;

else

cout<<"Invalid User Code. Restart program."<<endl;
return 0;
}

double Calculation(char UCode, float Rkwh, float Ckwh, float Pkwh, float OPkwh)
{
float Bill;
float OPBillI;
float PBillI;

if(UCode =='R')
{
cout<<"Please enter number of kwh used: ";
cin>>Rkwh;
/* return Rkwh;*/
Bill = (.052 * Rkwh) + 6.00;
/*return Bill;*/
}
else if(UCode =='C')
{
cout<<"Please enter amount of kWh used: ";
cin>>Ckwh;
/*return Ckwh;*/
if(Ckwh<=1000)
Bill=60.00;
/*return Bill;*/
if(Ckwh>1000)
Bill=60.00+((Ckwh-1000)*.045);
/*return Bill;*/
}
else if(UCode =='I')
{
cout<<"Please Enter Peak Hour Usage(8AM to 5PM): ";
cin>>Pkwh;
cout<<"Please Enter Off-Peak Usage(5PM to 8AM): ";
cin>>OPkwh;
/*return Pkwh;*/
if(Pkwh<=1000)
PBillI=76.00;
if(Pkwh>1000)
PBillI=76.00+((Pkwh-1000)*.065);
if(OPkwh<=1000)
OPBillI=40.00;
if(OPkwh>1000)
OPBillI=40.00+((OPkwh-1000)*.028);

Bill= PBillI+OPBillI;

}

else
cout<<"Invalid Entry."<<endl;

return Bill;
return Rkwh;
return Ckwh;
return Pkwh;
return OPkwh;
}

int BillPrint(float Bill, float Rkwh, float Ckwh, float Pkwh, float OPkwh, char UCode, int AccountNum)
{
cout<<"The Random Electric Company"<<endl<<"123 Tasselhoff Road"<<endl<<"Orinda, CA 94563"<<endl<<endl;
cout<<"--------------------------"<<endl;
cout<<"Account Number: "<<AccountNum<<endl;
if(UCode =='R')
{
cout<<"Total kwH used: "<<Rkwh<<endl<<"Amount Due: "<<Bill<<endl;
}
else if(UCode =='C')
{
cout<<"Total kwH used: "<<Rkwh<<endl<<"Amount Due: "<<Bill<<endl;
}
else if(UCode =='I')
{
cout<<"Total Peak Usage: "<<Pkwh<<endl<<"Total Off-Peak Usage: "<<OPkwh<<endl<<"Amount Due: "<<Bill<<endl;
}

else
cout<<"Invalid Code. Restart Program."<<endl;

return 0;

}

int main()
{
int AccountNum;
char UCode;
Instruction();
cout<<"Please enter account number. ";
cin>>AccountNum;

cout<<"Please enter User Code. ";
cin>>UCode;

Valid(UCode);
Calculation();
BillPrint();

system("pause");
cout<<"Program Terminated."<<endl;
return 0;
}
[4301 byte] By [beret9987] at [2007-11-20 11:01:30]
# 1 Re: Getting Obscure Numbers For End Result! HELP!
This:

return Bill;
return Rkwh;
return Ckwh;
return Pkwh;
return OPkwh;

...will not work. This function exits after the first return.

And when you call your 'Calculation(...)' and 'BillPrint(...)' you're not passing any thing to them. I'm surprised it compiles.

And these signatures are wrong:

int Instruction();
int Valid();
double Calculation();
int BillPrint();

If you want them to accept arguments you need to provide the types in the declarations.

I see now why it compiles. Your declarations and subsequent calls are in compliance, but those variables you've declared in the function definitions are meaningless. They know nothing.
Greggle at 2007-11-9 1:24:59 >
# 2 Re: Getting Obscure Numbers For End Result! HELP!
You are correct, the variables are not carrying over to the main function. You must keep in mind that any variabels that you declare in a function will be deleted once they go out of scope(the function ends). You have to declare the variables that hold the data in either a global scope(the top of the program), or in the main loop, and pass them as reference parameters to the fuctions.

for example:

void myfunction()
{
int a=10;
}

int main()
{
myfunction();
cout << a; //a is undefined, not of this scope
}

try something like this

void myfunction(int& var)
{
var=10;
}

int main()
{
int a;
myfunction(a);
cout << a; //a is 10
}

Another thing is that you must say the parameters of the functions in the prototypes of them on the top of the page.

void myfunction(int& var); //correct

void myfunction();// not correct, this is different function than one i defined

void myfunction(int& var)
{
var=10;
}

Also just so you know, the reason that you were getting random numbers as the result is that you were declaring new local variables in the function that is displaying the info, and when you declare local variables, they contain garbage(random numbers from formerly used data blocks).
bovinedragon at 2007-11-9 1:26:00 >