function trouble!
Oct 11, 2011 at 4:16pm UTC
I am trying to call values from other functions to go into another function and add sales tax to it. For some reason it isn't working. I am calling the values of 3 other functions into AddTax(float CostWithoutTax, char TypeOfPurchase). I have debugged the other functions and *KNOW* their values work but I've exhausted all thoughts. When I call the AddTax function into my Main(); it produces 0 so I know there is something wrong with this function. Any thoughts?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
float AddTax(float CostWithoutTax,char TypeOfPurchase)
{
float hi,hi2,hi3;
float Rent,Ticket,Room,CarRental,Tax,Tax2,Tax3;
hi=GetFlight();
hi2=GetHotelRoom();
hi3=GetCar(CarRental);
if (TypeOfPurchase==AIRFARE)
{
Tax=(hi*.07);
Ticket=Tax;
return (Ticket);
}
if (TypeOfPurchase==HOTEL)
{
Tax2=hi2*.11;
Room=Tax2;
return (Room);
}
if (TypeOfPurchase==CAR)
{
Tax3=hi3*.065;
Rent=Tax3;
return (Rent);
}
}
Oct 11, 2011 at 4:23pm UTC
You're not using 'CostWithoutTax' anywhere. That probably is supposed to factor in somehow, right?
Also you have tons of unnecessary variables there... but that's a stylistic thing and isn't your main problem, so whatever.
Oct 11, 2011 at 5:12pm UTC
I switched it to this, If i put CostWithoutTax=GetFlight(); inside of the if statement, it makes it loop in the main program 3x. Same results. I feel like this is right though and it should be working.. but it doesn.t
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
float AddTax(float CostWithoutTax,char TypeOfPurchase)
{
float hi,hi2,hi3;
float Rent,Ticket,Room,CarRental,Tax,Tax2,Tax3;
hi=GetFlight();
hi2=GetHotelRoom();
hi3=GetCar(CarRental);
const float AirTax=.07;
const float RoomTax=.11;
const float CarTax=.065;
if (TypeOfPurchase==AIRFARE)
{
CostWithoutTax=hi;
Ticket=CostWithoutTax+(AirTax*CostWithoutTax);
return (Ticket);
}
if (TypeOfPurchase==HOTEL)
{
CostWithoutTax=hi2;
Room=CostWithoutTax+(RoomTax*CostWithoutTax);
return (Room);
}
if (TypeOfPurchase==CAR)
{
CostWithoutTax=hi3;
Rent=CostWithoutTax+(CarTax*CostWithoutTax);
return (Rent);
}
Last edited on Oct 11, 2011 at 5:13pm UTC
Oct 11, 2011 at 5:17pm UTC
1- I dont see CostWithoutTax have any role to play in this function
2- Can I see whats going on with the 3 function
1 2 3
hi=GetFlight();
hi2=GetHotelRoom();
hi3=GetCar(CarRental);
This could also be the problem if these function didnt return a value to store in
.
Oct 11, 2011 at 5:24pm UTC
Pretty much all of the functions look like this. The values work though..I'm just really confused because I had thought i had everything correct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
float GetFlight()
{
char Flight;
float Flight2;
const char AMERICAN = 'A' ;
const char AMEAGLE = 'E' ;
const char DELTA = 'D' ;
const float DELTAPRICE=312.00;
const float AMERICANPRICE=320.00;
const float AMEAGLEPRICE=350.00;
// Ask the customer what type of ticket he/she would like to purchase.
cout <<"\t" <<"---------------------------------------------------" <<endl;
cout<<"\t" <<"Airlines Departs Arrives Cost" << endl;
cout<<"\t" <<"Delta Airlines 10:00 am 12:30 pm $312.00" <<endl;
cout<<"\t" <<"American Airlines 09:30 am 01:10 pm $320.00" <<endl;
cout<<"\t" <<"America Eagle 10:40 am 01:10 pm $350.00" <<endl;
cout<<"\t" <<"---------------------------------------------------" <<endl;
cout << "\nWhat type of airline ticket would you like to purchase?\n" ;
cout << "\t" << "Please press " << DELTA << " for the Delta Airlines ticket. Price = $" << DELTAPRICE << endl;
cout << "\t" << "Please press " << AMERICAN << " for the American Airlines ticket. Price = $" << AMERICANPRICE << endl;
cout << "\t" << "Please press " << AMEAGLE << " for the American Eagle Airlines ticket. Price = $" << AMEAGLEPRICE << endl;
//Get airline ticket
cout << "Enter choice: " ;
cin >> Flight;
Flight = toupper(Flight); // convert to uppercase
while (Flight != DELTA && Flight != AMERICAN && Flight != AMEAGLE)
cout << "Invalid choice. Please re-enter: " ;
cin >> Flight;
Flight = toupper(Flight);
}
if (Flight==DELTA)
{
Flight2=DELTAPRICE;
}
if (Flight==AMERICAN)
{
Flight2==AMERICANPRICE;
}
if (Flight==AMEAGLE)
{
Flight2==AMEAGLEPRICE;
}
return (Flight2);
}
Oct 11, 2011 at 5:24pm UTC
CostWithoutTax
is an
input to your function. You are welcome to set it, but your main function (or the function that called this) is not going to see that result.
When you call your function try using:
1 2 3
float Parameter1;
char Parameter2 = AIRFARE;
float Output = AddTax( &Parameter1 , Parameter2);
Instead of passing the
value of Parameter1 into
AddTax
, you are passing the
address of Parameter1 into
AddTax
.
When you use your function use:
1 2 3 4 5 6
float AddTax(float * CostWithoutTax, char TypeOfPurchase)
{
...
*CostWithoutTax = hi;
...
}
This will set the
value at the
address associated with
CostWithoutTax
. Outside of this function you now have the value of
hi
in
Parameter1
Last edited on Oct 11, 2011 at 5:25pm UTC
Oct 11, 2011 at 5:26pm UTC
I pretty much assign a character to a variable and then switch it to a float and then return the float value. by returning the float value..shouldnt the function be hi=GetFlight(); ?
Oct 12, 2011 at 8:31am UTC
I switched it to this, If i put CostWithoutTax=GetFlight(); inside of the if statement, it makes it loop in the main program 3x. Same results. I feel like this is right though and it should be working.. but it doesn.t
If youre doing it this way, I'd agree with
.
also, I dont think I spot any mistake in
GetFlight()
Topic archived. No new replies allowed.