This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 1 year ago.
Hi so im creating a program to have the user input 5 candidates and votes then the program spits back out the winner and percent of each candidate but i cant seem to figure out why the double varible dont show as a number like 40.5 or something just 0 and 100. I am still learning how this works
string candidate[5];
int votes[5];
int total;
double percent[5];
string winner;
cout << "Enter the first candidate: ";
cin >> candidate[0];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[0];
cout << endl << "Enter the second candidate: ";
cin >> candidate[1];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[1];
cout << endl << "Enter the third candidate: ";
cin >> candidate[2];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[2];
cout << endl <<"Enter the fourth candidate: ";
cin >> candidate[3];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[3];
cout << endl << "Enter the fifth candidate: ";
cin >> candidate[4];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[4];
total = votes [0] + votes[1] + votes[2] + votes[3] + votes[4];
percent[0] = (votes[0] / total * 100);
percent[1] = (votes[1] / total * 100);
percent[2] = (votes[2] / total * 100);
percent[3] = (votes[3] / total * 100);
percent[4] = (votes[4] / total * 100);
if (votes[0] > votes[1] && votes[2] && votes[3] && votes[4])
winner = candidate[0];
if (votes[1] > votes[0] && votes[2] && votes[3] && votes[4])
winner = candidate[1];
if (votes[2] > votes[1] && votes[0] && votes[3] && votes[4])
winner = candidate[2];
if (votes[3] > votes[1] && votes[2] && votes[0] && votes[4])
winner = candidate[3];
if (votes[4] > votes[1] && votes[2] && votes[3] && votes[0])
winner = candidate[4];
cout << "Candidate" << setw(5) << "Votes" << setw(5) << "Percent" << endl;
cout << "------------------------------------------------" << endl;
cout << candidate[0] << setw(5) << votes[0] << setw(5) << percent[0] << endl;
cout << candidate[1] << setw(5) << votes[1] << setw(5) << percent[1] << endl;
cout << candidate[2] << setw(5) << votes[2] << setw(5) << percent[2] << endl;
cout << candidate[3] << setw(5) << votes[3] << setw(5) << percent[3] << endl;
cout << candidate[4] << setw(5) << votes[4] << setw(5) << percent[4] << endl;
cout << "Winner: " << winner << endl;
cout << "Total Votes: " << total;
return 0;}
All of the variables in the calculation on the right hand side of your assignment are ints, so the result is an int.
You would have to convert the operands to doubles to get a double result.
percent[0] = (double(votes[0]) / double(total) * 100);
Related
I'm doing an assignment for class where I need to output my program to look something like this. Right now I'm stuck on how to output the years like that.
I've added a counter for years and asked for current year as an input?
Can someone lend me a hand?
Town A Town B
8.5% 0.3%
Year 5000 8000
-------------------------------------------
2016 5425 8024
2017 5886 8048
2018 6386 8072
2019 6929 8096
2020 7518 8121
2021 8157 8145
cout << "What year is it? ";
cin >> curYear;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
{
cout << "Error: Values must be positive, please try again." << endl;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
cout << endl;
}
years = 0;
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
years++;
curYear++;
townA = finalA;
townB = finalB;
}
cout << "\n\n" << endl;
cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
cout << setw(3) << growthA << "%" << setw(14) << growthB << "%" << endl;
cout << setw(4) << townA << setw(16) << townB << endl;
cout << "Year" << endl;
cout << curYear << endl;
cout << "--------------------------------------------" << endl;
cout << endl;
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
return 0;
}
I'm a beginner who has some questions relating to my homework for school.
With my current code I'm stuck in a neverending loop which I'm assuming is because my condition is never actually met (Pop A being greater than Pop B) and I'm unsure how to move on. I'm also not sure how to properly increment/calculate the years needed for Town A to surpass Town B but this is what I've got so far.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int townA;
int townB;
double growthA;
double growthB;
double finalA;
double finalB;
int years = 0;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
{
cout << "Error: Values must be positive, please try again." << endl;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
cout << endl;
}
years = 0;
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
}
cout << "\n\n" << endl; // Teacher required us to output it to the screen incase anyone is wondering why I have this block
cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
cout << setw(3) << growthA << "%" << setw(10) << growthB << "%" << endl;
cout << setw(3) << townA << setw(7) << townB << endl;
cout << "Year" << endl;
cout << "--------------------------------------------" << endl;
return 0;
}
You are not updating the values of townA and townB in the loop. Hence, you never get out of the loop.
Also, you need to increment years in the loop. Use:
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
// Update the values of the key variables.
++years;
townA = finalA;
townB = finalB;
}
// This needs to be moved from the loop.
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
I am having trouble with my code, I have read other forums this site about this problem but they don't really relate to my situation. I am new to C++, I barely have about 3 weeks into the course. I do not know why my break statement isn't working, to me it is in the loop. What am I missing?
float a[60][7];
int row;
cout << "How many students would you like to grade?" << endl;
cin >> studentNum;
cout << "Enter grades for the first student" << endl;
row = 0;
n = studentNum - 2;
while (row < studentNum)
{
a[row][0]=k;
a[row][0] = row;
cout << "Enter grade for test 1: ";
cin >> a[row][1];
cout << "Enter grade for test 2: ";
cin >> a[row][2];
cout << "Enter grade for test 3: ";
cin >> a[row][3];
cout << "Enter grade for test 4: ";
cin >> a[row][4];
a[row][5] = (a[row][1] + a[row][2] + a[row][3] + a[row][4]) / 4;
a[row][6] = a[row][1] * 0.2 + a[row][2] * 0.3 + a[row][3] * 0.3 + a[row][4] * 0.2;
row++;
}
if (row == n)
{
cout << "Enter Grades for the last Student" << endl;
cout << "Enter grade for test 1: ";
cin >> a[row][1];
cout << "Enter grade for test 2: ";
cin >> a[row][2];
cout << "Enter grade for test 3: ";
cin >> a[row][3];
cout << "Enter grade for test 4: ";
cin >> a[row][4];
break;
}
else
{
cout << "Enter grades for the next student" << endl;
row = row + 1;
}
cout << "Stdnt" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << endl;
printarray(a, studentNum);
cin.get();
return 0;
Something like this?
float a[60][7];
int row = 0;
std::cout << "How many students would you like to grade?" << std::endl;
std::cin >> studentNum;
n = studentNum - 2;
std::cout << "Enter grades for the first student" << std::endl;
while (row <= n)
{
a[row][0] = k;
a[row][0] = row;
std::cout << "Enter grade for test 1: ";
std::cin >> a[row][1];
std::cout << "Enter grade for test 2: ";
std::cin >> a[row][2];
std::cout << "Enter grade for test 3: ";
std::cin >> a[row][3];
std::cout << "Enter grade for test 4: ";
std::cin >> a[row][4];
a[row][5] = (a[row][1] + a[row][2] + a[row][3] + a[row][4]) / 4;
a[row][6] = a[row][1] * 0.2 + a[row][2] * 0.3 + a[row][3] * 0.3 + a[row][4] * 0.2;
if (row==n)
{
std::cout << "Enter Grades for the last Student" << std::endl;
std::cout << "Enter grade for test 1: ";
std::cin >> a[row][1];
std::cout << "Enter grade for test 2: ";
std::cin >> a[row][2];
std::cout << "Enter grade for test 3: ";
std::cin >> a[row][3];
std::cout << "Enter grade for test 4: ";
std::cin >> a[row][4];
}
else
{
std::cout << "Enter grades for the next student" << std::endl;
}
row++;
}
std::cout << "Student" << "\t" << "Grade1" << "\t" << "Grade2" << "\t" << "Grade3" << "\t" << "Grade4" << "\t" << "Avg1" << "\t" << "Avg2" << std::endl;
printarray(a, studentNum);
std::cin.get();
return 0;
P.S. edit with fixed while
I have to write a program that simulates an ice cream cone vendor. The user inputs the number of cones, and for each cone, the user inputs the number of scoops, then the flavor(a single character) for each scoop. At the end, the total price is listed. For the pricing, 1 scoop costs 2.00, 2 scoops costs 3.00 and each scoop after 2 costs .75.
I'm having trouble with the pricing. The correct price is displayed if the user only wants one cone.
/*
* icecream.cpp
*
* Created on: Sep 14, 2014
* Author:
*/
#include <iostream>
#include <string>
using namespace std;
void welcome() {
cout << "Bob and Jackie's Ice Cream\n";
cout << "1 scoop - $1.50\n";
cout << "2 scoops - $2.50;\n";
cout << "Each scoop after 2 - $.50\n";
cout << "Ice Cream Flavors: Only one input character for each flavor.\n";
}
bool checkscoops(int scoops) {
int maxscoops = 5;
if ((scoops > maxscoops) || (scoops < 1))
return false;
else
return true;
}
bool checkcones(int cones) {
int maxcones = 10;
if ((cones > maxcones) || cones < 1)
return false;
else
return true;
}
int price(int cones, int numberofscoops) {
float cost = 0.00;
{
if (numberofscoops == 5) {
cost = cost + 5 + (.75 * 3);
}
if (numberofscoops == 4) {
cost = cost + 5 + (.75 * 2);
}
if (numberofscoops == 3) {
cost = cost + 5.75;
}
if (numberofscoops == 2) {
cost = cost + 5.00;
}
if (numberofscoops == 1) {
cost = cost + 2.00;
}
}
cout << "Total price is: " << cost << endl;
}
int buildcone(int numcones) {
char flav1, flav2, flav3, flav4, flav5;
int numberofscoops;
for (int i = 1; i <= numcones; i++) {
cout << "Enter the amount of scoops you wish to purchase. (5 max): ";
cin >> numberofscoops;
checkscoops(numberofscoops);
while (checkscoops(numberofscoops) == false) {
cout << "You are not allowed to buy more than 5 scoops and you "
"cannot buy less than one scoop. Please try again.\n";
cout << "How many scoops would you like?(5 max): ";
cin >> numberofscoops;
checkcones(numberofscoops);
}
cout << "You are buying " << numberofscoops
<< " scoops of ice cream.\n";
if (numberofscoops == 5) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << "Enter flavor 5: ";
cin >> flav5;
cout << " ( " << flav1 << " )/" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " ( " << flav5 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 4) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 3) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 2) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 1) {
cout << "Enter a flavor: ";
cin >> flav1;
cout << " ( " << flav1 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
}
price(numcones, numberofscoops);
}
int main() {
int numberofcones;
int numberofscoops;
welcome();
cout << "How many cones would you like?(10 max) ";
cin >> numberofcones;
checkcones(numberofcones);
while (checkcones(numberofcones) == false) {
cout << "You are not allowed to buy more than 10 cones and you cannot "
"buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
checkcones(numberofcones);
}
cout << "You are buying " << numberofcones << " ice cream cones.\n";
buildcone(numberofcones);
}
Start by changing the return value of price() to float, or the function won't be able to return the proper cost. Also, since cones is not used to compute the cost of the purchase, we don't it as a parameter:
float price(int numberofscoops)
{
float total_cost = 0.0f;
if (numberofscoops == 1) {
total_cost = 2.0f;
}
else if (numberofscoops == 2) {
total_cost = 3.0f;
}
else if (numberofscoops > 2) {
total_cost = 5.0f + ((numberofscoops-2) * 0.75f);
}
return total_cost;
}
You code could have other problems, but I think these changes will let you continue to debug and fix the code on your own.
Your while() loop is flawed. Comment your call to checkcones() as shown below. You're already calling checkcones() as the conditional in your while(), no need to evaluate again as this will sent you into a perma-loop. You've got two of these while() statements that I could see, you'll want to comment out both.
while ( checkcones( numberofcones ) == false )
{
cout << "You are not allowed to buy more than 10 cones and you cannot buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
// THIS LINE IS THE PROBLEM :)
// checkcones(numberofcones);
}
After this fix, your program begins to work but the pricing fails. You should be able to figure that out with the answer given above.
I would also see if you can figure out how to implement a c++ class with members and methods as your current approach is very "c" like. Happy coding! :)
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.