Travel Expenses-Weird Result

This is a discussion on Travel Expenses-Weird Result within the C++ Programming forums, part of the General Programming Boards category; For some reason this prog is giving me some weird numbers in the final result, such as: "\$-965465464560000.0000" and so ...

1. Travel Expenses-Weird Result

For some reason this prog is giving me some weird numbers in the final result, such as: "\$-965465464560000.0000" and so forth. I've gone through it a million times and cant seem to figure out why its giving these results......could you help me please? its kind of long but bare with me.

Code:
```#include <iostream>
#include <iomanip>

using namespace std;

//file prototypes----------------
int days_on_trip();
int time_of_departure_arrival();
void airfare (double&, double&);
void car_rental(double&, double&);
void miles_driven(double&, double&);
void parking(int, double&, double&, double&);
void taxi(int, double&, double&, double&);
void conference(double&, double&);
void hotel_ex(int, double&, double&, double&);
void meals(int, double, double, double&, double&, double&);
void resultDisplay(int, double&, double&, double&);

//-------------------------------

int main()
{
int days;
int time_dep;           //time departed
int time_arr;           //time arrived
double costTotal, alwnceTotal, alwnceBelow = 0;

days = days_on_trip();
time_dep = time_of_departure_arrival();
time_arr = time_dep;
// = time_of_departure_arrival();
//time_of_departure_arrival();//call time of arrival/departure
airfare(costTotal, alwnceTotal);
car_rental(costTotal, alwnceTotal);
miles_driven(costTotal, alwnceTotal);
parking(days, costTotal, alwnceTotal, alwnceTotal);
taxi(days, costTotal, alwnceTotal, alwnceTotal);
conference(costTotal, alwnceTotal);
hotel_ex(days, costTotal, alwnceTotal, alwnceBelow);
meals(days, time_dep, time_arr, alwnceTotal, costTotal, alwnceBelow);
resultDisplay(days, costTotal, alwnceTotal, alwnceBelow);
return 0;
}
//--------------------------------------------------------------------------------------------------------
//function call - total # of days spent on trip
int days_on_trip()
{
int day;
cout << "How many days were spent during the trip: ";
cin >> day;

while(day <= 0)
{
cout << "Invalid Input, you must have spent at least one day on the trip.\n";
cout << "How many days were spent during the trip: ";
cin >> day;
cout << endl;
}

return day;
}
//----------------------------------------------------------------------------------------------
int time_of_departure_arrival()
{
int time_dep1;
int dep1;
int time_arr1;
int arr1;
cout << "Enter the time of departure for first day of trip in 24-hour time (HH.MM): ";
cin >> time_dep1;
cout << "\nEnter the time of arrival home on last day of trip in 24-hour time (HH.MM): ";
cin >> time_arr1;

while(time_dep1 < 0000 || time_arr1 < 0000 || time_dep1 > 2400 || time_arr1 > 2400 || time_dep1%100 > 60 || time_arr1%100 > 60)
{
cout << "Invalid inputs, make sure inputs are possitve and correctly formatted.\n";
cout << "Enter time of departre for first day of trip in 24-hour time (HH.MM): ";
cin >> time_dep1;
cout << "\nEnter the time of arrival home on last day of trip in 24-hour time (HH.MM): ";
cin >> time_arr1;
cout << endl;
}
arr1 = time_arr1 / 100;
dep1 = time_dep1 / 100;

return time_dep1;
return time_arr1;
}

//--------------------------------------------------------
//rnd-trip airfare
void airfare (double& exp, double& alwd)
{
double airfare;
cout << "What was your total expenses of airfare: \$";
cin >> airfare;

while (airfare < 0)
{
cout << "Invalid input of a negative.\n";
cout << "What was your total expenses of airfare: \$";
cin >> airfare;
cout << endl;
}

exp+=airfare;
alwd+=airfare;
}
//--------------------------------------------------------------------------------------------------------------------------
//car_rental
void car_rental(double& exp, double& alwd)
{
double cost;
cout << "Enter the cost for car rentals: \$";
cin >> cost;
cout << endl;
while(cost < 0)
{
cout << "Invalid input, enter a postitve value or zero.\n";
cout << "Enter the cost for car rentals: \$";
cin >> cost;

}

exp+=cost;
alwd+=cost;
}

//-------------------------------------------------------------------------------------------

void miles_driven(double& exp, double& alwd)
{
double expense;
double miles, fee;
char choice;

cout << "Was a private car used to drive? Enter Y/N: ";
cin >> choice;

while((choice != 'y') && (choice != 'Y') && (choice!= 'N') && (choice != 'n'))
{
cout << "Invalid Input, enter Y/N: ";
cin  >> choice;
cout << endl;
}
if((choice == 'N') || (choice == 'n'))
{
expense = 0;
}
if((choice == 'Y') || (choice == 'y'))
{
cout << "Enter miles driven: ";
cin >> miles;

while(miles < 0)
{
cout << "Invalid negative input\n";
cout << "Enter miles driven: ";
cin >> miles;

}
fee = miles * 0.27f;
exp+=fee;
alwd+=fee;
}
}
// parking-------------------------------------------------------------------------------------------------------------------------------------------------
void parking(int days, double& exp, double& alwd, double& collected)
{
double parkExp; // amount of money used for parking
const double park_spendLimit = 6.00;

for (int counter = 1; counter <= days; counter++)
{
cout << "For parking, how much did you spend on day " << counter << "? ";
cin >> parkExp;

while(parkExp < 0)
{
cout << "Invalid negative number input. Re-enter amount: ";
cin >> parkExp;
}

if (parkExp >= park_spendLimit)
{
exp+=parkExp;
alwd+=park_spendLimit;
}

if (parkExp < park_spendLimit)
{
alwd+=parkExp;
exp+=parkExp;
collected+= (park_spendLimit - parkExp);
}

}

}
// Taxi--------------------------------------------------------------------------------------------------------------------------------------------------------
void taxi (int days, double& exp, double& alwd, double& collected)
{
char T_use;
double taxiMoney; // amount of money used on taxis
const double taxi_spendLimit = 10.00;

cout<<"Did you use a taxi while on the trip? Press y for yes and n for no."<<endl;
cin>>T_use;
while((T_use != 'y') && (T_use != 'Y') && (T_use!= 'N') && (T_use != 'n'))
{
cout << "Invalid Input, enter Y/N: ";
cin  >> T_use;
cout << endl;
}
if (T_use == 'y' || T_use == 'Y')
{
for (int counter = 1; counter <= days; counter++)
{
cout << "For the taxi, how much did you spend on day " << counter << "? ";
cin >> taxiMoney;

while(taxiMoney < 0)
{
cout << "No negative numbers allowed! Re-enter the amount: ";
cin >> taxiMoney;
}

if (taxiMoney >= taxi_spendLimit)
{
exp+=taxiMoney;
alwd+=taxi_spendLimit;
}

if (taxiMoney < taxi_spendLimit)
{
alwd+=taxiMoney;
exp+=taxiMoney;
collected+= (taxi_spendLimit - taxiMoney);
}
}
}
else if (T_use == 'n' || T_use == 'N')
taxiMoney = 0;
}

// conference ------------------------------------------------------------------------------------------------------------------------------------------
void conference(double& exp, double& alwd)
{
double conf;
cout<<"What were the conference fees for the trip?"<<endl;
cin>> conf;
while(conf < 0)
{
cout << "Invalid negative input\n";
cout << "Enter total for conference fees: ";
cin >> conf;
cout << endl;
}
exp+=conf;
alwd+=conf;

}
//-------------------------------------------------------------------------------
void hotel_ex(int days, double& exp, double& alwd, double& collected)
{
int hotel_night = days - 1;
double hotel_fee;
const double spendLimit = 90.00;

for (int counter = 1; counter <= hotel_night; counter++)
{
cout << "For the hotel, how much did you spend on night " << counter << "? ";
cin >> hotel_fee;

while(hotel_fee < 0)
{
cout << "Invalid negative input! Please re-enter the amount: ";
cin >> hotel_fee;
cout << endl;
}

if (hotel_fee >= spendLimit)
{
exp+=hotel_fee;
alwd+=spendLimit;
}
if (hotel_fee < spendLimit)
{
alwd+=hotel_fee;
exp+=hotel_fee;
collected+=(spendLimit - hotel_fee);
}
}
}

//-----------------------------------------------------------------------------------------------------------------------------------

//meals
void meals(int days1, double time_dep, double time_arr, double& alwd, double& exp, double& collected)
{
int  count = 1;
double spentTotal = 0;
double lunch, brkfst, dnr;

while (count <= days1)
{
//1stday
if (count == 1)
{
if (time_dep < 7)
{
//breakfast
cout << "how much did your breakfast cost on day " << count << ":";
cin >> brkfst;
while(brkfst < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> brkfst;
}

if (brkfst >= 9)
{
exp+=brkfst;
alwd+=9;
}

if (brkfst < 9)
{
alwd+=brkfst;
exp+=brkfst;
collected+=(9 - brkfst);
}

//lunch
cout << "how much did your lunch cost on day " << count << ":";
cin >> lunch;
while(lunch < 0)
{
cout << "No negative numbers allowed. Re-enter the amount:";
cin >> lunch;
}

if (lunch >= 12)
{
exp+=lunch;
alwd+=12;
}

if (lunch < 12)
{
alwd+=lunch;
exp+=lunch;
collected+=(9 - lunch);
}

//dinner
cout << "how much did your dinner cost on day " << count << ": ";
cin >> dnr;
while(dnr < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> dnr;
}

if (dnr >= 16)
{
exp+=dnr;
alwd+=16;
}

if (dnr < 16)
{
alwd+=dnr;
exp+=dnr;
collected+=(16 - dnr);
}
count++;
}

else if ( time_dep < 12 )
{
//lunch
cout << "how much did your lunch cost on day" << count << ": ";
cin >> lunch;
while(lunch < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> lunch;
}

if (lunch >= 12)
{
exp+=lunch;
alwd+=12;
}

if (lunch < 12)
{
alwd+=lunch;
exp+=lunch;
collected+=(9 - lunch);
}

//dinner
cout << "how much did your dinner cost on day" << count << ": ";
cin >> dnr;

while(dnr < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> dnr;
}

if ( dnr >= 16)
{
exp+=dnr;
alwd+=16;
}

if ( dnr < 16)
{
alwd+=dnr;
exp+=dnr;
collected+=(16 - dnr);
}
count++;
}

else if ( time_dep < 18 )
{
//dinner
cout << " how much did your dinner cost on day " << count << ": ";
cin >> dnr;

while( dnr < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> dnr;
}

if ( dnr >= 16)
{
exp+=dnr;
alwd+=16;
}

if ( dnr < 16)
{
alwd+=dnr;
exp+=dnr;
collected+=(16 - dnr);
}

count++;

}

}

if (count != 1 || count != days1) //adding the  meals
{
//1 day rule
if( days1== 1 )
break;

//breakfast
cout << "how much did your breakfast cost on day  " << count << ": ";
cin >> brkfst;

while( brkfst < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> brkfst;
}

if ( brkfst >= 9)
{
exp+=brkfst;
alwd+=9;
}

if ( brkfst < 9)
{
alwd+=brkfst;
exp+=brkfst;
collected+=(9 - brkfst);
}

//lunch
cout << "how much did your lunch cost on day" << count << ": ";
cin >> lunch;

while(lunch < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> lunch;
}

if (lunch >= 12)
{
exp+=lunch;
alwd+=12;
}

if (lunch < 12)
{
alwd+=lunch;
exp+=lunch;
collected+=(9 - lunch);
}

//dinner
cout << " how much did your dinner cost on day " << count << ": ";
cin >> dnr;

while( dnr < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> dnr;
}

if ( dnr >= 16)
{
exp+=dnr;
alwd+=16;
}

if ( dnr < 16)
{
alwd+=dnr;
exp+=dnr;
collected+=(16 - dnr);
}
count++;
}

if (count == days1) //last day rule
{
if ( time_arr> 19 )
{
//breakfast
cout << " how much did your breakfast cost on day " << count << ": ";
cin >> brkfst;

while( brkfst < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> brkfst;
}

if ( brkfst >= 9)
{
exp+=brkfst;
alwd+=9;
}

if ( brkfst < 9)
{
alwd+=brkfst;
exp+=brkfst;
collected+=(9 - brkfst);
}

//lunch
cout << " how much did your lunch cost on day " << count << ": ";
cin >> lunch;

while(lunch < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> lunch;
}

if (lunch >= 12)
{
exp+=lunch;
alwd+=12;
}

if (lunch < 12)
{
alwd+=lunch;
exp+=lunch;
collected+=(9 - lunch);
}

//dinner
cout << "how much did your dinner cost on day " << count << ": ";
cin >> dnr;

while(dnr < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> dnr;
}

if (dnr >= 16)
{
exp+=dnr;
alwd+=16;
}

if (dnr < 16)
{
alwd+=dnr;
exp+=dnr;
collected+=(16 - dnr);
}

count++;
}

else if ( time_arr > 13 )
{

//breakfast
cout << " how much did your breakfast cost on day " << count << ": ";
cin >> brkfst;

while( brkfst < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> brkfst;
}

if ( brkfst >= 9)
{
exp+=brkfst;
alwd+=9;
}

if ( brkfst < 9)
{
alwd+=brkfst;
exp+=brkfst;
collected+=(9 - brkfst);
}

//lunch
cout << " how much did your lunch cost on day " << count << ": ";
cin >> lunch;

while(lunch < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> lunch;
}

if (lunch >= 12)
{
exp+=lunch;
alwd+=12;
}

if (lunch < 12)
{
alwd+=lunch;
exp+=lunch;
collected+=(9 - lunch);
}

count++;
}

else if ( time_arr > 8 )
{
//breakfast
cout << " how much did your breakfast cost on day " << count << ": ";
cin >> brkfst;

while( brkfst < 0)
{
cout << "No negative numbers allowed. Re-enter the amount: ";
cin >> brkfst;
}

if ( brkfst >= 9)
{
exp+=brkfst;
alwd+=9;
}

if ( brkfst < 9)
{
alwd+=brkfst;
exp+=brkfst;
collected+=(9 - brkfst);
}
count++;
}
}
}

}
//---------------------------------------------------------------------------------
//display
void resultDisplay(int days, double& ttl, double& alwd, double& collected)
{
double owed;

cout << setprecision(2) << fixed << endl;
cout << "--------------------------------------------------\n\n";
cout << "The results for the " << days << " day trip:\n";
cout << "Total cost of the trip is \$" << ttl << endl;
cout << "--------------------------------------------------\n\n";

cout << setprecision(2) << fixed << endl;
cout << "Total allowance for the trip is \$" << alwd << endl; //total allowance

if(ttl > alwd)          //you owe the company
{
owed = ttl - alwd;
cout << "Total expense is beyond the amount allowed," << endl;
cout << setprecision(2) << fixed << showpoint << endl;
cout << "so you owe the company \$" << owed << endl;

}

else if (ttl == alwd)
{
cout << "you dont owe anything.\n\n";
if (collected > 0)
cout << "Because you remained under the daily allowance," << endl;
cout << setprecision(2) << fixed << showpoint << endl;
cout << "you saved the company \$" << collected << endl;
cout << "--------------------------------------------------------------\n\n";
}

else
cout << "ERROR!!!\n";

}```

2. you pass to airfare uninitialized vars, containing garbage. inside you are adding to this garbage something... the result is still garbage

3. Originally Posted by vart
you pass to airfare uninitialized vars, containing garbage. inside you are adding to this garbage something... the result is still garbage
ok.....so i initialized it:
Code:
` double costTotal, alwnceTotal, alwnceBelow = 0;`
but it still outputs garbage tho.....

4. > double costTotal, alwnceTotal, alwnceBelow = 0;
Erm, nope - that's just one.

double costTotal = 0, alwnceTotal = 0, alwnceBelow = 0;

Also, your meals() function is simply too long to make any sense of it at all.
There is no way to test that in any reasonable manner to make sure that it's working well.

Try splitting out some of the functionality to sub-functions.

5. Originally Posted by Salem
> double costTotal, alwnceTotal, alwnceBelow = 0;
Erm, nope - that's just one.

double costTotal = 0, alwnceTotal = 0, alwnceBelow = 0;

Also, your meals() function is simply too long to make any sense of it at all.
There is no way to test that in any reasonable manner to make sure that it's working well.

Try splitting out some of the functionality to sub-functions.

oh wow! thanks! it worked. cant believe i missed that. i clearly need to go over my notes lol. thanks again! =]