Need help regarding an infinite loop - c++

I am currently working on a homework problem that asks me to Write a program that displays a weekly payroll report.A loop in the program should ask the user :
employee number
gross pay
state tax
federal tax,
FICA withholdings. I am getting the correct output, however my professors grader keeps telling giving me the error statement:
Format Ok. Build Ok. Link Ok. Run Timed out. Runtime 9020 milliseconds. Your program is waiting for input or in an endless loop.
I am having some trouble finding this loop and would appreciate if anyone could point it out to me.
I have tried changing the main while(employeeNumber !=0) loop to include a do/while statement that repeats if (totalWithholdings > grossPay)
int main()
{
int employeeNumber;
double grossPay = 0.0,
grossPayTotal = 0.0,
stateTax = 0.0,
stateTaxTotal = 0.0,
federalTax = 0.0,
federalTaxTotal = 0.0,
ficaWithholdings = 0.0,
ficaWithholdingsTotal = 0.0,
netPay = 0.0,
netPayTotal = 0.0,
totalWithHoldings = 0.0;
cout << "Enter the following information:\n" << endl;
cout << "Employee Number(0 to quit) :\n";
cin >> employeeNumber;
while (employeeNumber < 0)
{
cout << "Employee number may not be less than zero.\n";
cout << "Re-enter employee Number (0 to quit): ";
cin >> employeeNumber;
}
while (employeeNumber != 0)
{
cout << "Gross pay :";
cin >> grossPay;
while (grossPay < 0)
{
cout << "Gross pay may not be less than zero.\n";
cout << "Re-enter Gross pay: ";
cin >> grossPay;
}
cout << "Federal Withholding :";
cin >> federalTax;
while (federalTax < 0)
{
cout << "Federal witholding may not be less than zero.\n";
cout << "Re-enter Federal Withholding: ";
cin >> federalTax;
}
cout << "\nState Withholding :";
cin >> stateTax;
while (stateTax < 0)
{
cout << "\nState witholding may not be less than zero.\n";
cout << "\nRe-enter State Withholding: ";
cin >> stateTax;
}
cout << "FICA Withholding : ";
cin >> ficaWithholdings;
while (ficaWithholdings < 0)
{
cout << "FICA witholding may not be less than zero.\n";
cout << "Re-enter FICA Withholding: ";
cin >> ficaWithholdings;
}
totalWithHoldings = (federalTax + stateTax + ficaWithholdings);
if (totalWithHoldings > grossPay)
{
cout << "\nERROR: Withholdings cannot exceed gross pay.\n"
<< "\nPlease re-enter the data for this employee.\n";
cin >> employeeNumber;
}
else
{
cout << "Processing the Next employee:\n"
<< "Employee Number(0 to quit) :\n";
cin >> employeeNumber;
}
grossPayTotal = grossPayTotal + grossPay;
federalTaxTotal = federalTaxTotal + federalTax;
stateTaxTotal = stateTaxTotal + stateTax;
ficaWithholdingsTotal = ficaWithholdingsTotal + ficaWithholdings;
netPay = grossPay - totalWithHoldings;
netPayTotal = netPayTotal + netPay;
}
cout << "Total Gross Pay : $" << setw(4) << setprecision(2)
<< fixed << grossPayTotal << endl;
cout << "Total Federal Tax : $" << setw(4) << setprecision(2)
<< fixed << federalTaxTotal << endl;
cout << "Total State Tax : $" << setw(4) << setprecision(2)
<< fixed << stateTaxTotal << endl;
cout << "Total FICA : $" << setw(4) << setprecision(2)
<< fixed << ficaWithholdingsTotal << endl;
cout << "Total Net Pay : $" << setw(4) << setprecision(2)
<< fixed << netPayTotal << endl;
return 0;
}

The problem occurs when you type such symbols which can not be interpreted as double. For example, if you input gross pay "100,50" you input comma but you need to input point: "100.50". If you input comma than gross pay is set to 100 and ",50" left in the std::cin stream. ",50" can not be converted to double in the next input (cin >> federalTax;). As result, federalTax is not changed (if you use c++03) or set to 0 (if you use c++11). Details are in the article. So, (for c++03) each place where you input data (std::cin) does not change corresponding variable, ",50" is still in input stream (so, no waiting of the next user input), and at the end of each loop employeeNumber also is not changed, and than condition of while (employeeNumber != 0) of course is true, where an endless loop is formed. You can check each input using the next example:
cout << "Gross pay :";
while (!(cin >> grossPay)) {
std::cin.clear(); // clear all error state flags
std::string inputLine;
std::getline(std::cin, inputLine); // "remove" invalid data from std::cin
cout << "Please enter a valid gross pay" << endl;
cout << "Gross pay :";
}

Related

expected 'while' or expected '}' Answered before yes, but I can't fix it myself

I have already checked all the other questions but I just can't fix it.. I am a nuub at coding.
I don't know why it says it needs a while or where to put it, and it gives the wrong answer for the LOCS function also is there anything i can do about the default pointer warning. this is just a start i will be extending this later so it would be a big help and i have tried while and closing brackets everywhere lol
Btw if anyone can tell me how I can use the input as decision as you can see I am using 1 and 2, but If I could use permanent and casual that would be great.
// Calculate an employee's weekly salary
// Use a do while loop, and an if else statement to have user input data and display the correct values
#include <iostream>
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
do {
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
Status = 0;
while (Status < 1 || Status > 2);
if (Status = 1)
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status = 2) {
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
}
#include <iostream>
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
do {
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
} while (Status < 1 || Status > 2); // add while after do block with }
if (Status == 1)// use '==' equality check not '=' .
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status == 2) { // use '==' equality check not '=' .
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
I have made the suitable changes :
Use == to check equality instead of = operator!
Syntax of do while : do { //code... } while(condition).
The while must follow the do after closing the block!
Your Mistake
the main mistake is you have not close the do while loop, ask in comment for more clarification!
PS. I recommend you first copy my code and then run, and then analyse the issue!
Use this code this will work
#include
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
while(1){
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
Status = 0;
while (Status < 1 || Status > 2);
if (Status = 1)
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status = 2) {
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
}

Uninitialized local variable used?

having some problems with a class exercise
#include <iostream>
#include <string>
using namespace std;
int main()
{
int employeeNumber, grossPay, stateTax, federalTax, ficaHold;
int totalGross, totalState, totalFederal, totalFica, totalPay = 0;
do // do-while loop for employee number
{
cout << "Enter the employee number: " << endl;
cout << "(Enter 0 to quit.)" << endl;
cin >> employeeNumber;
} while (employeeNumber < 0);
while (employeeNumber != 0)
{
do // gross pay greater than state tax + federal tax + FICA
{
do // entry and validation for gross pay
{
cout << "How much gross pay ? ";
cin >> grossPay;
} while (grossPay < 0);
do //entry and validation for state tax
{
cout << "How much state tax ? ";
cin >> stateTax;
} while (stateTax < 0 || stateTax > grossPay);
do // entry and validation federal tax
{
cout << "How much federal tax ? ";
cin >> federalTax;
} while (federalTax < 0 || federalTax > grossPay);
do // entry and validation for FICA holdings amount
{
cout << "How much FICA withholdings ? ";
cin >> ficaHold;
} while (ficaHold < 0 || ficaHold > grossPay);
if (grossPay < stateTax + federalTax + ficaHold) // Message to verify taxes are not greater than pay
cout << "State tax, federal tax, and FICA holdings cannot be greater than gross pay. Please re-enter these values." << endl;
} while (grossPay < stateTax + federalTax + ficaHold);
totalGross += grossPay;
totalState += stateTax;
totalFederal += federalTax;
totalFica += ficaHold;
totalPay = totalGross - (totalState + totalFederal + totalFica);
do // do-while loop for employee number
{
cout << "Enter the employee number: " << endl;
cout << "(Enter 0 to quit.)" << endl;
cin >> employeeNumber;
} while (employeeNumber < 0);
}
cout << endl << endl << "The total gross pay is: $" << totalGross << endl;
cout << "The total state tax is :" << totalState << endl;
cout << "The total federal tax :" << totalFederal << endl;
cout << "The total FICA withholdings :" << totalFica << endl;
cout << "Net pay :" << totalPay << endl << endl;
return 0;
}
getting some errors for the variables on lines 95-98, eg "uninitialized local variable 'totalState'" and not really sure what to do, i already gave those variables value in the declaration before any loops, and im not sure if i can move them before anything while keeping the goal of the program
The first time you use totalState, you're both reading and writing: totalState += stateTax;. The problem is that you've never initialized totalState, so there's no guarantee what the value will be when you try to read it.
For the record, you have other uninitialized variables as well: totalGross, totalFederal, and totalFica.

C++: interestEarned coming back wrong

I'm learning C++ and this is an assignment I have to do, it's complete but my interestEarned is not coming back correctly but the bankBalance is correct so I'm just not displaying interestEarned correctly. "Total Interest Earned: $65.50" is incorrect is supposed to display "$120.50" as my teacher said. What am I doing wrong?
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// Variables
int numOfCustomers, numOfMonths = 0;
double bankBalance, depositAmount, withdrawnAmount, interestRate, interestEarned = 0.0;
const int MIN = 0;
// Asking for Number of Customers
cout << "Enter the number of customers: ";
cin >> numOfCustomers;
// Making sure the input was not 0 or lower
while (numOfCustomers <= MIN) {
cout << "==>Number of customers must be at least 1. Try again: ";
cin >> numOfCustomers;
}
// Validating each Customer
for (int c = 1; c < (numOfCustomers + 1); c++) {
// Start of each Customer
cout << "\nCUSTOMER " << c << ":\n";
// Asking for Number of Months --
cout << "Enter the number of months the account has been opened: ";
cin >> numOfMonths;
// Making sure the input was not 0 or lower
while (numOfMonths <= MIN) {
cout << "==>Number of months must be at least 1. Try again: ";
cin >> numOfMonths;
}
// Asking for Starting Balance --
cout << "Enter the starting balance: $";
cin >> bankBalance;
// Making sure the input was not 0 or lower
while (bankBalance < MIN) {
cout << "==>Starting balance can't be negative. Try again: $";
cin >> bankBalance;
}
// Asking for Monthly Interest Rate --
cout << "Enter the monthly interest rate as a decimal (i.e. 0.05 = 5%): ";
cin >> interestRate;
// Making sure the input was not 0 or lower
while (interestRate < MIN) {
cout << "==>Monthly interest rate can't be a nagative. Try again: ";
cin >> interestRate;
}
// Validating each Month
for (int m = 1; m < (numOfMonths + 1); m++) {
// Deposit Amount
cout << "\nEnter deposit amount for Month " << m << ": $";
cin >> depositAmount;
bankBalance = bankBalance + depositAmount;
// Withdrawn Amount
cout << "Enter withdrawn amount for Month " << m << ": $";
cin >> withdrawnAmount;
bankBalance = bankBalance - withdrawnAmount;
// Calculating Interest Earned
interestEarned = bankBalance * interestRate;
// Complete bankBalance
bankBalance = bankBalance + interestEarned;
}
// Account Summary
cout << "\nACCOUNT SUMMARY" << endl;
cout << fixed << setprecision(2);
cout << "Number Months Active: " << numOfMonths << endl;
cout << "Ending Balance: $" << bankBalance << endl;
cout << "Total Interest Earned: $" << interestEarned << endl;
}
system("pause");
return 0;
}
Use another variable that is initialized to zero before the loop. In the loop, add the value of interestEarned to it. - #Peter

Code WORKS Math DOESN'T

My code compiles nicely, but the math formulas that I am using aren't providing the right outcome. I need to calculate the balance, withdrawn, and interest for all 3 months. I am also required to validate user's input. For these purposes I am using nested loops. Please let me know if you spot my mistake. Thank you lovely people!
cout << "Please enter the starting balance: ";
cin >> startBalance;
cout << "Please enter the annual interest rate: ";
cin >> annualInterest;
for (int month = 1; month <= 3; month++) {
do {
cout << setprecision(5);
cout << "Please enter the total amount deposited on month " << month << ": ";
cin >> balance;
if (balance <0) {
goodChoice = false;
cout << "\n\t***ERROR " << balance << " ***";
cout << "*** Choice must be positive***\n" << endl;
}
else {
goodChoice = true;
}
startBalance += balance; //need to calculate the balance for all 3 months
} while (!goodChoice);
do {
cout << setprecision(5);
cout << "Please enter the total amount withdrawn on " << month << ": ";
cin >> withdrawn;
if ( (withdrawn <0) || (withdrawn > startBalance) ) {
goodChoice = false;
cout << "***ERROR " << withdrawn << " ***";
cout << "*** Choice must be positive or greater than the balance***" << endl;
}
else {
goodChoice = true;
}
finalWithdrawn += withdrawn; // the total amount of withdrawn
finalBalance = startBalance - withdrawn;
monthInterest = ((startBalance + finalBalance) / 2) * (annualInterest / 12);
totalInterest += monthInterest; //total interest for all 3 months
endBalance = monthInterest + finalBalance;
} while (!goodChoice);
}
cout << "Total Deposit: " << endBalance << endl;
cout << "Total Withdrawn: " << finalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << endBalance << endl;
You have a lot of extra variables defined which aren't needed. Also, your interest rate may have been in percentage instead of a decimal number, i.e. 10% = 0.1. Also, your monthInterest is taking an average then applying interest. I wrote this up and it seems to work.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float totalDeposit = 0.0f;
float totalWithdrawn = 0.0f;
float totalInterest = 0.0f;
float totalBalance = 0.0f;
float interestRate = 0.0f;
setprecision(5); //?
cout << "Enter starting balance: ";
cin >> totalBalance;
cout << "Enter annual interest rate (%): ";
cin >> interestRate;
// Convert to monthly and non-percent; i.e. 10% = 0.1 = 10 / 100
interestRate = interestRate / 100.0f / 12.0f;
for (int month = 1; month <= 3; month++)
{
float deposit = -1.0; // Default to an error state
while (deposit < 0.0)
{
cout << "Enter total deposited in month " << month << ": ";
cin >> deposit;
if (deposit < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
float withdrawn = -1.0f; // Default to an error state
while (withdrawn < 0.0f)
{
cout << "Enter total withdrawn in month " << month << ": ";
cin >> withdrawn;
if (withdrawn < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
totalDeposit += deposit;
totalWithdrawn += withdrawn;
totalBalance = totalBalance + deposit - withdrawn;
totalBalance += totalBalance * interestRate;
totalInterest += totalBalance * interestRate;
}
cout << "Total Deposit: " << totalDeposit << endl;
cout << "Total Withdrawn: " << totalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << totalBalance << endl;
int wait; // Pause so console window doesn't close. Delete this line.
cin >> wait;
return 0;
}

C++ For Loop Problem?

Ok so i'm working on this project and its a travel expense program. Basically it just has some functions that gets info from the user. I'm having a problem with my for loop. The running total is messing up. The numbers end up like 2 or 4 numbers off of what there supposed to be. Here's the code(I know it's not neat or anything i will clean that up later)
#include <iostream>
#include <fstream>
using namespace std;
int getDays(int);
double getDepartureTime();
double getArrivalTime(double);
double airFees(double);
double carRentalFees(double);
double getMilesDriven(double);
double getParkingTotal(double,double);
double getParkingSpent(double,double);
double getTaxiFees(double,double);
double employeeHotelExpense(double,double);
double getHotelExpense(double,double);
double getMealExpenses(double,double);
void timeEquivalent();
double breakFastFee = 0;
int main()
{
int days=0, amount=0, departure_conference=0, departure_home=0,time = 0;
double airFee=0,taxiFeesAllowed,parkingAllowed = 0,employeeHotelExpense = 0,employeeTaxiFees = 0, milesDriven=0,
parkingFees=0, taxiFees=0, yes=0,arrivalTime = 0;
double carRentalFee = 0, hotel_expenses=0,departureTime = 0, meals=0,employeeMealExpenses = 0, parkingSpent = 0,allowableHotelExpense = 0
,allowedMealTotal = 0,mealsSpent = 0;
char employee[40];
//timeEquivalent();
//cout << "What Time Did You Arrive " <<endl;
//cin >>time;
days = getDays(days);
timeEquivalent();
departureTime = getDepartureTime();
arrivalTime = getArrivalTime(arrivalTime);
airFee = airFees(airFee);
carRentalFee = carRentalFees(carRentalFee);
milesDriven = getMilesDriven(milesDriven);
parkingAllowed = 6 * days;
parkingSpent = getParkingSpent(parkingSpent,days);
taxiFeesAllowed = days * 10;
employeeTaxiFees = getTaxiFees(taxiFees,days);
allowableHotelExpense = 90 * days;
employeeHotelExpense = getHotelExpense(employeeHotelExpense, days);
employeeMealExpenses = getMealExpenses(departureTime,arrivalTime);
cout << employeeMealExpenses <<endl;
return 0;
}
int getDays(int days)
{
cout << " How many Days did you stay on the trip " <<endl;
cin >> days;
while(days < 0)
{
cout <<"Please enter a value greater than 0 :D " <<endl;
cin >> days;
}
return days;
}
double getDepartureTime()
{
double departureTime;
cout << "Please Refer To The Menu Above and enter the time of departure in military\n";
cout << "Time. For example if you departed at 7:30 enter 0730\n\n";
cin >> departureTime;
return departureTime;
}
double getArrivalTime(double arrivalTime)
{
cout << "Please refer to the menu above and enter the time you arrived back home in\n";
cout <<" military format\n";
cin >> arrivalTime;
return arrivalTime;
}
double airFees(double airfee)
{
cout << " How Much Were Your Air Fees " <<endl;
cin >> airfee;
while(airfee < 0)
{
cout <<" Please enter a value greater than 0 :D " <<endl;
cin >> airfee;
}
return airfee;
}
double carRentalFees(double carRentalFee)
{
cout << " How Much were Your Car Rental Fees " <<endl;
cin >> carRentalFee;
while(carRentalFee < 0)
{
cout <<"Please enter a value of 0 or greater :D " <<endl;
cin >> carRentalFee;
}
return carRentalFee;
}
double getMilesDriven(double milesDriven)
{
const double mileRate = 0.27;
cout << " How many miles did you drive, please enter 0 if a private vehicle was not used " <<endl;
cin >> milesDriven;
while(milesDriven < 0)
{
cout << " Please Enter 0 or Greater:)"<<endl;
cin >> milesDriven;
}
return mileRate * milesDriven;
}
double getParkingSpent(double parkingSpent, double days)
{
cout << " How Much Did You Spend on Parking " <<endl;
cin >> parkingSpent;
while(parkingSpent < 0)
{
cout << "Please Enter an Amount of 0 or Greater "<<endl;
cin >> parkingSpent;
}
return parkingSpent*days;
}
double getTaxiFees(double taxiFees,double days)
{
cout << " Please Enter The Amount of Taxi Fees Please " <<endl;
cin >> taxiFees;
while(taxiFees < 0)
{
cout << "Please Enter an Amount of 0 or Greater "<<endl;
cin >> taxiFees;
}
return taxiFees * days;
}
double getHotelExpense(double employeeHotelExpense,double days)
{
cout << " How Much Were Your Hotel Expenses " <<endl;
cin >> employeeHotelExpense;
while(employeeHotelExpense < 0)
{
cout << "Please Enter a Amount of 0 or Greater "<<endl;
cin >> employeeHotelExpense;
}
return employeeHotelExpense * days;
}
double getMealExpenses(double departureTime,double arrivalTime)
{
static double breakFastFee = 0 ;
static double lunchFee = 0 ;
static double dinnerFee = 0 ;
int numberOfDays = 2 ;
double total = 0;
for(int days =1;days <=numberOfDays;days++)
{
if ( days < numberOfDays && departureTime > 000 && departureTime < 700)
{
cout << "Please Enter Your breakfast cost"<<endl;
cin >> breakFastFee;
cout << " Please Enter Your Lunch Cost " <<endl;
cin >>lunchFee;
cout << "Please Enter Your Dinner Cost " <<endl;
cin >> dinnerFee;
}
if (days < numberOfDays && departureTime > 700 && departureTime <=1200)
{
cout << "Please Enter Your Lunch Cost"<<endl;
cin >> lunchFee;
cout << "Please Enter Your Dinner cost "<<endl;
cin >> dinnerFee;
}
if(days < numberOfDays && departureTime > 1201 && departureTime <= 1800)
{
cout << "Enter The Cost of Dinner " <<endl;
cin >> dinnerFee;
}
if(days == numberOfDays && arrivalTime > 800 && arrivalTime<=1300)
{
cout <<"Enter The Cost of Breakfast " <<endl;
cin >> breakFastFee;
}
if(days == numberOfDays && arrivalTime > 1301 && arrivalTime <= 1900)
{
cout << "Enter The Cost of Breakfast "<<endl;
cin >> breakFastFee;
cout << " Enter The Cost of Lunch " <<endl;
cin >> lunchFee;
}
if(days == numberOfDays && arrivalTime > 1901)
{
cout << "Enter The Cost of Breakfast " <<endl;
cin >> breakFastFee;
cout << " Enter The Cost of Lunch " <<endl;
cin >> lunchFee;
cout << "Enter The Cost of Dinner " <<endl;
cin >> dinnerFee;
}
total+=breakFastFee + lunchFee + dinnerFee;
}
return total;
}
void timeEquivalent()
{
cout <<"Regular Time " << "\t\t" <<"Military Time \n";
cout <<"************" << "\t\t" <<"***************"<<endl;
cout <<"Midnight " << "\t\t" <<"0000 \n";
cout <<"1:00a.m. " << "\t\t" <<"0100 \n";
cout <<"2:00a.m. " << "\t\t" <<"0200 \n";
cout <<"3:00a.m. " << "\t\t" <<"0300 \n";
cout <<"4:00a.m. " << "\t\t" <<"0400 \n";
cout <<"5:00a.m. " << "\t\t" <<"0500 \n";
cout <<"6:00a.m. " << "\t\t" <<"0600 \n";
cout <<"7:00a.m. " << "\t\t" <<"0700 \n";
cout <<"8:00a.m. " << "\t\t" <<"0800 \n";
cout <<"9:00a.m. " << "\t\t" <<"0900 \n";
cout <<"10:00a.m." << "\t\t" <<"1000 \n";
cout <<"11:00a.m." << "\t\t" <<"1100 \n";
cout <<"12:00p.m." << "\t\t" <<"1200 \n";
cout <<"1:00p.m. " << "\t\t" <<"1300 \n";
cout <<"2:00p.m. " << "\t\t" <<"1400 \n";
cout <<"3:00p.m. " << "\t\t" <<"1500 \n";
cout <<"4:00p.m. " << "\t\t" <<"1600 \n";
cout <<"5:00p.m. " << "\t\t" <<"1700 \n";
cout <<"6:00p.m. " << "\t\t" <<"1800 \n";
cout <<"7:00p.m. " << "\t\t" <<"1900 \n";
cout <<"8:00p.m. " << "\t\t" <<"2000 \n";
cout <<"9:00p.m. " << "\t\t" <<"2100 \n";
cout <<"10:00p.m." << "\t\t" <<"2200 \n";
cout <<"11:00p.m." << "\t\t" <<"2300 \n";
cout <<"Midnight " << "\t\t" <<"0000 \n";
}
can somebody tell me whats going wrong. If you test it out use 0600 for departure time and 0900 for arrivaltime.
There are multiple problems in this code. One, in a function like getDays that returns the number of days, you don't need to pass the number of days into the method as a parameter.
Also, since you want a value for days that is greater than 0, you should be checking whether days <= 0 in the while loop. Your current condition, days < 0, will be false if days is set to 0.
getDays would be better written as:
int getDays()
{
int days = 0;
cout << " How many Days did you stay on the trip " <<endl;
cin >> days;
while(days <= 0)
{
cout <<"Please enter a value greater than 0 :D " <<endl;
cin >> days;
}
return days;
}
Also, in getMealExpenses, there seems to be no reason to declare breakFastFee, lunchFee, and dinnerFee as static. This may be the cause of the problem you asked about, since they never get re-initialized to 0 after the first call to getMealExpenses.
Finally, neatly-formatted code is more likely to get helpful responses, because it's easier to read :-)
I think its becouse you are not resetting the breakFastFee, lunchFee, dinnerFee variable to 0 before each execution. So when total+=breakFastFee + lunchFee + dinnerFee; hits it will add values from the previues iteration.