I have to create a program to calculate charges for airfare. It's a simple program so far and I am not done adding to it, but every time I run it the result turns out to be 0. Is there something missing in my code? I am a beginner and I would appreciate any advice on improving my code. Thank you.
#include <iostream>
using namespace std;
void main () {
int distance = 0;
int num_bags= 0;
int num_meals= 0;
double distance_price = distance * 0.15;
double bag_price = num_bags * 25.00;
double meal_price = num_meals * 10.00;
double total_airfare = 0.00;
cout << "CorsairAir Fare Calculator" << endl;
cout << "Enter the distance being travelled: " << endl;
cin >> distance;
cout << "Enter number of bags checked: " <<endl;
cin >> num_bags;
cout << "Enter the number of meals ordered: " << endl;
cin >> num_meals;
total_airfare = (distance_price + bag_price + meal_price);
cout << total_airfare;
}
Your confusion is completely understandable - the piece you're missing is that when you assign a variable, you're assigning the left side to the result of the right side at that moment in time. It's not like algebra, where you say f(x) = x + 5 and f(x) is always whatever x + 5 is.
So, you assign double distance_price = distance * 0.15 when distance is 0 (which you just initialized). distance_price remains 0 even after you ask for input and change distance.
Do your price calculations after you ask for input, and everything will work just fine.
You are calculating the distance_price bag_price meal_price with default values i.e. 0 not with the value which you took from user.
Below code works fine and you won't see the issue.
#include <iostream>
using namespace std;
// My compiler did not allow void main so used int main
int main () {
int distance = 0;
int num_bags= 0;
int num_meals= 0;
double distance_price ;
double bag_price ;
double meal_price;
double total_airfare;
cout << "CorsairAir Fare Calculator" << endl;
cout << "Enter the distance being travelled: " << endl;
cin >> distance;
cout << "Enter number of bags checked: " <<endl;
cin >> num_bags;
cout << "Enter the number of meals ordered: " << endl;
cin >> num_meals;
distance_price = distance * 0.15;
bag_price = num_bags * 25.00;
meal_price = num_meals * 10.00;
total_airfare = 0.00;
total_airfare = distance_price + bag_price + meal_price;
cout << total_airfare;
return 0;
}
Result
CorsairAir Fare Calculator
Enter the distance being travelled:
200
Enter number of bags checked:
2
Enter the number of meals ordered:
2
100
Related
I'm writing a program for my C++ class I've complete the program. but it won't compile. I'm new to programming so I don't really know what I'm doing wrong. If there is anyone on here that can point me in the right direction. Please help me!
Prompt Description:
Write a C++ program to calculate free estimate for carpet and furniture cleaning of residential and business customers. The program continues until end of file is reached.
Fro residential customers, specify and update number of small couches ($50 each), large couches ($80 each), rooms ($70 each) and steps ($5 each) until exit is selected. the bill is calculated based on the number of items. If the amount is more than 500 dollars, a discount of 10% is applied to the bill. Then the customer is offered to select from an installment of 1,2,3, or 4 or press 0 to exit. Based on an installment option, the bill is increased slightlenter code herey, and each installment amount is calculated.
For business customers, ask the user to enter the amount of square footage and then the bill is calculated at .45 per square foot.
Here is the code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int exit = 0;
int smallcouches = 1;
int largecouches = 2;
int rooms = 3;
int steps = 4;
const float SMALL_COUCH = 50.0;
const float LARGE_COUCH = 80.0;
const float ROOMS = 70.0;
const float STEPS = 5.00;
const float PER_SQUARE_FOOT = 0.45f;
const float DISCOUNT_QUALIFIED = 500.0;
const float DISCOUNT = 0.10f;
const int ONE = 1;
const int TWO = 2;
const int THREE = 3;
const int FOUR = 4;
const float RATEONE = 0.0f;
const float RATETWO = 0.0535f;
const float RATETHREE = 0.055f;
const float RATEFOUR = 0.0575f;
float billAmount;
float ResidentialEstimate(float SMALL_COUCH, float LARGE_COUCH, float ROOMS, float STEPS, float DISCOUNT_QUALIFIED);
void GetResidentialItems(int& smallcouches, int& largecouches, int& rooms, int& steps);
void InstallmentPlan(float billAmount);
void BusinessEstimate(float PER_SQUARE_FOOT);
int main()
{
cout << fixed << showpoint << setprecision(2);
int customerType, residential_customer, business_customer;
char B, b, R, r;
residential_customer = R || r;
business_customer = B || b;
cout << "Enter customer type: R, r (Residential) or B, b (Business): ";
cin >> customerType; //Enter customer type
cout << endl;
while (cin) //While there is input to read
{
if (customerType == R || customerType == r) //if residential customer
{
ResidentialEstimate(SMALL_COUCH, LARGE_COUCH, ROOMS, STEPS, DISCOUNT_QUALIFIED); // call function ResidentialEstimate
InstallmentPlan(billAmount); // all function Installmentplan
}
else if (customerType == B || customerType == b) //else if business customer
{
BusinessEstimate(PER_SQUARE_FOOT); //call function BusinessEstimate
}
cout << "Enter customer type: R, r (Residential) or B, b (Business): ";
cin >> customerType; // Enter cutomer type
cout << endl;
}
return 0;
}
float ResidentialEstimate(float SMALL_COUCH, float LARGE_COUCH, float ROOMS, float STEPS, float DISCOUNT_QUALIFIED)
{
GetResidentialItems(smallcouches, largecouches, rooms, steps); //Call function GetResidentialItems to get items to clean
billAmount = (SMALL_COUCH + LARGE_COUCH + ROOMS + STEPS); //Calculate the bill amount
if (billAmount > 500) //if bill amount is more than 500 dollars
{
DISCOUNT_QUALIFIED = billAmount * 0.10f;
billAmount = billAmount - DISCOUNT_QUALIFIED; //Apply a discount of 10% to the bill amount
}
return billAmount; //return bill Amount
}
void GetResidentialItems(int& smallcouches, int& largecouches, int& rooms, int& steps)
{
int count;
int choice = smallcouches || largecouches || rooms || steps;
//Ask user to select an item to update or press 0 to exit
cout << "0. Exit, 1. Small Couches, 2. Large Couches, 3. Rooms, 4. Steps " << endl;
cout << "Enter one of the above choices: ";
cin >> choice;
cout << endl;
while (choice > 0) //while user hasn't pressed 0
{
choice = count;
cout << "Please enter the number of " << choice; //ask the user to enter a number from the item selected
cin >> count;
cout << endl;
//Show the current selections and numbers
cout << "Current selections: " << count << " Small Couches, " << count << " Large Couches, " << count << " Rooms, " << count << " Steps.";
//Ask user to select an item to update or press 0 to exit
choice = 0;
count = 0;
cout << "0. Exit, 1. Small Couches, 2. Large Couches, 3. Rooms, 4. Steps " << endl;
cout << "Enter one of the above choices: ";
cin >> choice;
cout << endl;
}
}
void InstallmentPlan(float billAmount)
{
int num;
int installment = 0;
int bill = 0;
//Ask user to select number of installments or 0 to exit
cout << "Please enter the desired number of instalments (1, 2, 3, or 4) or 0 to exit : ";
cin >> num;
cout << endl;
while (num > 0) //while user hasn't pressed 0
{
//calculate the installments
if (num == 1)
installment = billAmount;
else if (num == 2)
{
bill = billAmount * 0.0535f;
installment = bill / num;
}
else if (num == 3)
{
bill = billAmount * 0.055f;
installment = bill / num;
}
else if (num == 4)
{
bill = billAmount * 0.0575f;
installment = bill / num;
}
cout << "With " << num << " installment your bill of " << billAmount << " will be worth " << bill << "." << endl;
cout << "Each installment will be worth " << installment << endl;
//Ask user to select number of installments or 0 to exit
cout << "Please enter the desired number of instalments (1, 2, 3, or 4) or 0 to exit : ";
cin >> num;
cout << endl;
}
}
void BusinessEstimate(float squarefootage)
{
//Ask user for the square footage
cout << " Enter the approximate square footage: ";
cin >> squarefootage;
cout << endl;
//Calculate the bill amount
billAmount = squarefootage * PER_SQUARE_FOOT;
cout << "Your free Business Customer Estimate for " << squarefootage << "square footage = " << billAmount;
}
Every time I run the program, using the exact same values (25 for diameter and 5 for depth), I am getting different values for water_price and I'm not sure why.
Some of the outcomes:
$6.62256e+07 is the total cost.
$0 is the total cost.
$2.43411e-27 is the total cost.
I don't know if I am dealing with values in memory not playing well with each other, not flushing or what.
Why are the outcomes different every time I run this program?
#include <iostream>
#define PI 3.1416
#define WATER_COST 1.80
using std::cout;
using std::cin;
using std::endl;
int main() {
float pool_diameter, pool_depth;
float pool_radius = pool_diameter / 2;
float pool_volume_sq_inches = (PI * pool_radius * pool_radius * pool_depth) * 1728;
float pool_gallons = pool_volume_sq_inches / 231;
float water_price = (pool_gallons / 748) * WATER_COST;
cout << "Enter the pool diameter: ";
cin >> pool_diameter;
cout << "\nEnter the pool depth: ";
cin >> pool_depth;
cout << "\n$" << water_price << " is the total cost." << endl;
return 0;
}
See how we need to declare the variables to begin with, then when you ask for input it will be stored in those variables, and then you can continue on with the calculations you need.
#include <iostream>
#define PI 3.1416
#define WATER_COST 1.80
using std::cout;
using std::cin;
using std::endl;
int main() {
float pool_diameter = 0.0;
float pool_depth = 0.0;
cout << "Enter the pool diameter: ";
cin >> pool_diameter;
cout << "\nEnter the pool depth: ";
cin >> pool_depth;
float pool_radius = pool_diameter / 2;
float pool_volume_sq_inches = (PI * pool_radius * pool_radius * pool_depth) * 1728;
float pool_gallons = pool_volume_sq_inches / 231;
float water_price = (pool_gallons / 748) * WATER_COST;
cout << "\n$" << water_price << " is the total cost." << endl;
return 0;
}
You may want to get the inputs soon after the declaration.
float pool_diameter, pool_depth;
cout << "Enter the pool diameter: ";
cin >> pool_diameter;
cout << "\nEnter the pool depth: ";
cin >> pool_depth;
Rest code would work the way it is.
A good practice would be to initialize your variables like Omid-CompSci has answered here.
Statement float pool_radius = pool_diameter / 2; is executed before
cin >> pool_diameter;. Default value(garbage value) is used every time to calculate the pool_radius that is reason for different values in different runs.
change the order.
I'm currently working on a set of code where we had to rework the code from all being written in the main and breaking it up into two subfunctions and the main. I've broken it up, but I am having trouble reading in one of my subfunctions. I never learned parameter passing in depth because my professor only briefly touched on it.
The error I'm getting is "Expression preceding parentheses of apparent call must have (pointer-to-) function type."
This is the line of code I'm having an issue with:....
type = selectCarpet(type, unitPrice);
unitPrice = oneRoom(pricePerSqYd, count, ftLength, ftWidth, ftSq, ydSq, squareYd, materialCost, totalCost, unitPrice);
and this is the function:
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
using namespace std;
const double BEST = 6.99,
MEDIUM = 4.59,
BASIC = 3.50,
INSTALL = 129.99;
const int NINE = 9;
int selectCarpet(int type, int unitPrice);
double oneRoom(double pricePerSqYd, int count, double ftLength, double ftWidth, int numRooms, double ftSq, double ydSq, int squareYd, double materialCost, double unitPrice, double totalCost);
int main()
{
double ftLength, // room length in feet
ftWidth, // room width in feet
ftSq, // square footage
ydSq, // square yard
materialCost, // carpet material cost
totalCost, // material cost plus install
grandTotal,
unitPrice;
int squareYd, // square yards, round off
type, // carpet type
count,
numRooms;
type = 0;
unitPrice = 0;
double pricePerSqYd,
oneRoom;
type = selectCarpet(type, unitPrice);
totalCost = 0;
cout << "\nEnter number of rooms: ";
cin >> numRooms;
unitPrice = oneRoom(pricePerSqYd, count, ftLength, ftWidth, ftSq, ydSq, squareYd, materialCost, totalCost, unitPrice);
// step 11
grandTotal = 0;
grandTotal += totalCost;
cout << "\n\nThe grand total price is "
<< grandTotal << endl;
// step 13
do
{
cout << "\n\t\t*** CARPET INSTALLATION ***\n\n";
cout << "Select carpet type:\n"
<< "1 - Best Quality, Unit Price $6.99\n"
<< "2 - Medium Quality, unit price $4.59\n"
<< "3 - Basic Quality, Unit price $3.50\n"
<< "4 - exit\n"
<< "Enter your choice --> ";
cin >> type;
} while (type != 1 && type != 2 && type != 3 && type != 4);
return 0;
}
int selectCarpet(int type, int unitPrice)
{
do
{
cout << "\n\t\t*** CARPET INSTALLATION ***\n\n";
cout << "Select carpet type:\n"
<< "1 - Best Quality, Unit Price $6.99\n"
<< "2 - Medium Quality, unit price $4.59\n"
<< "3 - Basic Quality, Unit price $3.50\n"
<< "4 - exit\n"
<< "Enter your choice --> ";
cin >> type;
} while (type != 1 && type != 2 && type != 3 && type != 4);
while (type != 4)
{
// step 2
if (type == 1) unitPrice = BEST;
else if (type == 2) unitPrice = MEDIUM;
else if (type == 3) unitPrice = BASIC;
}
return unitPrice;
}
double oneRoom(double pricePerSqYd, int count, double ftLength, double ftWidth, int numRooms, double ftSq, double ydSq, int squareYd, double materialCost, double unitPrice, double totalCost)
{
for (count = 0; count < numRooms; count++)
{
cout << "Enter room length in feet: ";
cin >> ftLength;
cout << "Enter room diwth in feet: ";
cin >> ftWidth;
// step 5
ftSq = ftLength * ftWidth;
// step 6
ydSq = ftSq / NINE;
// step 7
squareYd = int(ydSq + .5);
// step 8
materialCost = squareYd * unitPrice;
// step 9
totalCost = materialCost + INSTALL;
// step 10
cout << setiosflags(ios::fixed | ios::showpoint)
<< setprecision(2);
cout << "\n\nSquare footage of the room is: " << ftSq
<< "\nSquare yard of the room is:\t" << ydSq
<< "\nSquare yards priced for: " << squareYd
<< "\nMaterial Cost:\t$" << materialCost
<< "\nInstallation\t$" << INSTALL
<< "\nTotal Cost\t$" << totalCost
<< endl << endl;
}
return pricePerSqYd;
}
any help is appreciated as I have almost no idea what I am doing here. Thank you.
This declaration within main():
double pricePerSqYd,
oneRoom;
shadows the declaration of your function outside of main():
double oneRoom(..., ...);
Name lookup finds the variable first, but you can't call a double. Hence the error. Just rename one or the other.
I am coding a program to find the maximum of an equation over an interval given by the user. When I compile the code instead of outputting the maximum it gives me this
Please enter the first number of the interval to be checked:
Please enter the last number of the interval to be checked:
Please enter the desired initial step size:
sh: PAUSE: command not found
I figure the problem has to do with my loops, but I'm not sure how to rectify the situation.
Here's my code
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, delta, x, y;
int max = 0;
cout <<"Please enter the first number of the interval to be checked: " << endl;
cin >> a;
cout << "Please enter the last number of the interval to be checked: " << endl;
cin >> b;
cout << "Please enter the desired initial step size: " << endl;
cin >> delta;
for(x = a; x <= b; x = x+delta)
{
y = pow(x, 2)-7*x-18;
if (y > max)
{
max = y;
cout <<"The maximum over the interval from " << a <<" to " << b <<" is " << max;
}
else
{
delta= delta/2;
}
if (delta < pow( 10, -6))
{
break;
}
}
return 0;
}
Looking at your code it seems like that the value of delta should be in float as you are recursively dividing it by 2. Your pow (10,-2) doesn't do any useful comparison for that matter. even after everything it doesn't throw me that pause error. I ran it on VS 12 (C++11).
I am trying to make a program which does a very basic calculation, but for some reason i can't get the code right. It is supposed to calculate the miles per gallon for one trip. You can then add this info multiple times (for different trips) and for each time it should calculate the total miles per gallon (i.e. the average miles per gallon of all the trips). This is the code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int counter = 1;
double milePerRe, milePerTo = 0, x, y;
cout << "Enter the miles used (-1 to quit): ";
cin >> x;
cout << "Enter gallons: ";
cin >> y;
while (x != -1)
{
milePerRe = x/y;
milePerTo += milePerRe;
milePerTo /= counter;
cout << "MPG this tankful: " << setprecision( 6 ) << fixed << milePerRe;
cout << "\nTotal MPG: " << setprecision( 6 ) << fixed << milePerTo << endl << endl;
counter++;
cout << "Enter the miles used (-1 to quit): ";
cin >> x;
if (x != -1)
{
cout << "Enter gallons: ";
cin >> y;
}
}
system("pause");
return 0;
}
When I run the program and say I enter 10 on the miles and 1 on the number of gallons the first time and the second time, everything works fine. Then if i do it again a third time the calculations begin to become incorrect.
You can not calculate average of averages the way you do it. In your code you are dividing by the counter EACH iteration, while you should divide it only at the end.
Best way to do what you need is something like this:
...
double totalMiles = 0;
double totalGallons = 0;
...
while (x != -1)
{
milePerRe = x/y;
totalMiles += x;
totalGallons += y;
milesPerTo = totalMiles / totalGallons;
...
However, if your task was to explicitly calculate the average of trips (not the average of miles/gallons), you would need to introduce another variable, like this:
...
double currentMilesPerTo;
...
while (x != -1)
{
milePerRe = x/y;
milePerTo += milePerRe;
currentMilesPerTo = milePerTo/counter;
....
cout << "\nTotal MPG: " << currentMilesPerTo;
...
the value of x and y are not being updated properly i guess.after every iteration try to make x and y to zero.
hope it works this way
TNQ