My program I have been working on is supposed to output the following:
* The number of gallons of paint required
* The hours of labor required
* The cost of the paint
* The labor charges
* The total cost of the paint job
However, it displays 0 in every field.. What have I done wrong now?
Your help would be greatly appreciated.
Here is my code:
//Headers
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
void PaintJobEstimator(double gallonprice, double calc)
{
float numBucket=0;
float hours=0;
float bucketCost=0;
float laborCharges=0;
float totalCost=0;
//calculates number of buckets of paint (gallons) needed
numBucket=numBucket+calc*(1/115);
//calculates paint cost
bucketCost=bucketCost+gallonprice*numBucket;
//calculates labor hour
hours=hours+calc*(8/115);
//calculates labor charges
laborCharges=hours*18;
//calculates total cost
totalCost=totalCost+bucketCost+laborCharges;
//Console output
cout << "The number of Gallons of paint required:\t" << setprecision(2) << numBucket << endl;
cout << "The hours of labor required:\t" << setprecision(2) << hours << " hrs" << endl;
cout << "The labor charges:\t$" << setprecision(2) << laborCharges << endl;
cout << "The cost of the paint:\t$" << setprecision(2) << bucketCost << endl;
cout << "The total cost of the paint job:\t$" << setprecision(2) << totalCost << endl;
}
void main ()
{
int rooms;
double calc=0;
double wallspace;
double gallonprice;
cout << "=========================================================\n";
cout << "___________________Paint Job Estimator___________________\n";
cout << "_________________________________________________________\n";
cout << endl;
cout << "Enter the number of rooms: ";
cin >> rooms;
while (rooms<1) //validates rooms
{
cout << "Invalid entry, enter one or more rooms:\t";
cin >> rooms;
}
for (int roomNum=1;
roomNum<=rooms;
roomNum++)
{
cout << "Enter the wall space in square meters for room " << roomNum << ":\t" << endl;
cin >> wallspace;
while (wallspace < 0.01)//validates wallspace
{
cout << "Invalid entry, please re-enter the wall area for room " << roomNum << ":\t";
cin >> wallspace;
}
calc=calc+wallspace;
}//end loop
cout << "\nEnter price of the paint per gallon: ";
cin >> gallonprice;
if (gallonprice <10) //validates price per gallon
{
cout << "Invalid entry, Reenter price at a $10.00 minimum: ";
cin >> gallonprice;
}
PaintJobEstimator(gallonprice,wallspace);
system ("pause");
}
Here is a screenshot of the console:
You're multiplying by zero in some of the calculations. For example, in the following line of code:
numBucket=numBucket+calc*(1/115);
You put 1/115 in parenthesis, which evaluates to zero because of integer division. To achieve the desired effect, try:
numBucket = calc / 115.0f;
Related
Currently working on the following homework problem:
Enter destination city, miles traveled to get there and gallons of gasoline used for any number of trips entered at the keyboard (use ctl+z to stop). Use a function to compute miles per gallon(miles traveled/gallons used). Display the destination city and miles per gallon for each trip entered. Sum the miles traveled and give a count of the number of trips made. Display these at the end of the program.
The problem occurs during the second iteration of the while loop.
When asking the using for input, the output "Enter destination city, ctrl+z to stop:" and 'Enter miles to destination and gallons of gasoline needed, ctrl+z to stop:" are merged into one output.
In other words, the console does not allow the user time to input a city after asking for a destination the second time. Instead, both outputs are displayed back to back and whatever is input is stored into gallons and gasoline resulting in an error.
I have tried adding more likes between the code.
I tried adding a system("pause") between the lines.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
float mpgFunc(float, float);
int main()
{
//variable declaration
string destination;
float milesToDest, gasolineNeeded, MPG, totalMiles = 0.0f;
int count = 0;
//input phase
cout << "Enter destination city, ctrl+z to stop: " << endl;
getline(cin, destination);
cout << "Enter miles to destination and gallons of gasoline needed,
ctrl+z to stop: " << endl;
cin >> milesToDest >> gasolineNeeded;
while (!cin.eof())
{
MPG = mpgFunc(milesToDest, gasolineNeeded); //function call
count = count + 1; //counter
totalMiles = totalMiles + milesToDest; //sum
cout << setprecision(2) << fixed;
cout << "Destination city: " << setw(10) << destination <<
endl;
cout << "Miles per gallon: " << setw(10) << MPG << endl;
/* Problem
cout << "Enter destination city, ctrl+z to stop: " << endl <<
endl;
getline(cin, destination);
cout << "Enter miles to destination and gallons of gasoline
needed, ctrl+z to stop: " << endl;
cin >> milesToDest >> gasolineNeeded;
problem happens here the two outputs are merged into one output */
}
cout << endl;
cout << "Total miles traveled: " << setw(8) << totalMiles << endl;
cout << "Total trips taken: " << setw(8) << count << endl;
system("pause");
return 0;
}//end main
float mpgFunc(float milesToDest, float gasolineNeeded)
{
float MPG;
MPG = milesToDest / gasolineNeeded;
return MPG;
}
I am trying to set the parameters or range for my rands on Friday, Saturday and Sunday so they will only display numbers between minimumSnowfall entered and maximumSnowfall entered. How would one go about doing this? I know how to seed a random number, but have no idea on setting a range. Simple terms if you please(I'm still learning).
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
int main(void)
{
int minimumSnowfall;
int maximumSnowfall;
char skiResortName[81];
cout << "I will forecast how much snowfall there will be on each \nday this weekend. You get to pick the range. " << endl;
cout << "Please enter the minimum amount of snow that \nwill fall in one day in inches: ";
cin >> minimumSnowfall;
cin.ignore(1, '\n');
cout << "Please enter the maximum amount of snow that \nwill fall in one day in inches: ";
cin >> maximumSnowfall;
cin.ignore(1, '\n');
if (minimumSnowfall < maximumSnowfall)
{
cout << "Enter the name of the ski resort: ";
cin.getline(skiResortName, 81);
cout << "\nThe local forecast for snowfall at " << skiResortName << " this weekend: " << endl;
cout << "Friday: " << rand() % 100 << " inches" << endl;
cout << "Saturday: " << rand() % 100 << " inches" << endl;
cout << "Sunday: " << rand() % 100 << " inches" << endl;
}
else
{
cout << "\nThe maximum snowfall entered is less than the minimum snowfall. " << endl;
cout << "No snowfall reported.\n" << endl;
}
return 0;
}
I have a problem with my code, every time I loop it with the answer 'y'(Yes) it loops to infinity?
I'm trying to make a loan calculator and every time the user is done calculating with a transaction and wants to reset, and do another calculation if he enters in a value 'y', and if he enters 'n' the program will end.
Here's my code so far:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
char ans = 'y';
do {
string name;
int Months;
int n;
double LoanAmount, Rate, MonthlyInterest, TotalLoanAmount, MonthlyAmortization, OutstandingBalance;
cout << fixed << showpoint;
cout << "Enter Name of Borrower: ";
getline(cin, name);
cout << "Enter Loan Amount: ";
cin >> LoanAmount;
cout << "Enter Number of Months to Pay: ";
cin >> Months;
cout << "Enter Interest Rate in Percent(%): ";
cin >> Rate;
cout << setprecision(2);
MonthlyInterest = LoanAmount * Rate;
TotalLoanAmount = LoanAmount + (MonthlyInterest * Months);
cout << "Monthly Interest: " << MonthlyInterest << endl
<< "Total Loan Amount with interest: " << TotalLoanAmount << endl;
cout << setw(100)
<< "\n\tSUMMARY OF OUTSTANDING INSTALLMENT" << endl
<< "\tName of Borrower: " << name
<< "\n\nMonth\t\tMonthly Amortization\t\tOutstanding Balance"
<< "\n";
for(n = 1; n <= Months; n++) {
MonthlyAmortization = TotalLoanAmount / Months;
OutstandingBalance = TotalLoanAmount - MonthlyAmortization;
cout << n << "\t\t" << MonthlyAmortization << "\t\t\t" << n - 1 << OutstandingBalance << endl;
}
cout << "\nEnd of Transaction";
cout << "Do you want to compute another transaction?[y/n]?" << endl;
cin >> ans;
}
while(ans == 'y');
}
After your cin>>ans, add these two lines :
cin.clear();
cin.sync();
That usually fixes a lot of the infinite looping problems I get with cin.
Also, I would recommend against initializing ans as 'y' when you declare it. I don't think this is causing you problems but it's an uncessesary thing.
You seem to expect pressing y and enter to register as only 'y'. If you want to get the input of just one character have a look at std::cin.get(char)
So I am trying to learn C++ for a college course and I have to write a program which uses this formula:
Amount = Principal * (1 + Rate/T)^T
Where principal is the balance in savings, rate is the interest rate, and t is the number of times the interest is compounded during a year. According to the book if you type in 4.25 as the interest rate and 12 as the number of times compounded with the principal as 1000.00 then you should get 43.34 as interest and the total amount should be 1043.34. I'm not sure if I am coding it wrong or what but I was wondering if anyone could help me out with any mistakes I might have done! I'm trying to do it on my own for about a day or two now but I have had no luck.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double PRINCIPAL;
double INTEREST_RATE;
double COMPOUND_AMOUNT;
cout << "What is your savings account balance?: " << endl;
cin >> PRINCIPAL;
cout << "What is your annual interest rate?: " << endl;
cin >> INTEREST_RATE;
cout << "How many times has your interest been compounded?: " << endl;
cin >> COMPOUND_AMOUNT;
double amt1 = 1 + (INTEREST_RATE/COMPOUND_AMOUNT);
double AMOUNT = PRINCIPAL * pow(amt1, COMPOUND_AMOUNT);
cout << "Interest Rate: " << INTEREST_RATE << endl;
cout << "Times Compounded: " << COMPOUND_AMOUNT << endl;
cout << "Principal: " << PRINCIPAL << endl;
cout << "Interest: " << INTEREST_RATE * COMPOUND_AMOUNT << endl;
cout << "Amount: " << AMOUNT << endl;
system("pause");
return 0;
}
This is a math error. If you're going to take in interest rates as '4.25' %, you need to divide the interest rate by 100. The code below gave me the amount as 1043.34 when 4.25 is entered as the interest rate.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double PRINCIPAL;
double INTEREST_RATE;
double COMPOUND_AMOUNT;
cout << "What is your savings account balance?: " << endl;
cin >> PRINCIPAL;
cout << "What is your annual interest rate? (in %): " << endl;
cin >> INTEREST_RATE;
INTEREST_RATE /= 100;
cout << "How many times has your interest been compounded?: " << endl;
cin >> COMPOUND_AMOUNT;
double amt1 = 1 + (INTEREST_RATE/COMPOUND_AMOUNT);
double AMOUNT = PRINCIPAL * pow(amt1, COMPOUND_AMOUNT);
cout << "Interest Rate (%): " << INTEREST_RATE * 100 << endl;
cout << "Times Compounded: " << COMPOUND_AMOUNT << endl;
cout << "Principal ($): " << PRINCIPAL << endl;
cout << "Interest ($): " << AMOUNT - PRINCIPAL << endl;
cout << "Amount ($): " << AMOUNT << endl;
system("pause");
return 0;
}
for interest your book is talking about the amount of interest in dollars, i.e. AMOUNT - PRINCIPAL.
I've been teaching myself C++ on and off for a few months and now I'm trying to make a payroll system. Here's my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void wageCompute (int, int);
int main()
{
int loopTimes=0;
int empNum=100, workHours, otHours, rate, bPay, otPay, grossPay;
string empName, empPos;
cout << "PAYROLL FOR THE MONTH OF MARCH" << endl;
cout << "Employees: " << empNum << endl;
while (empNum>loopTimes)
{
cout << "NAME: ";
cin.ignore();
getline (cin, empName);
cout << "\nPOSITION: ";
getline (cin, empPos);
cout << "\nHOURS WORKED: ";
cin >> workHours;
cout << "\nWAGE PER HOUR: ";
cin >> rate;
bPay = workHours*rate;
cout << "YOUR BASE PAY IS: " << bPay << endl << endl;
cout << "HOURS WORKED OVERTIME: ";
cin >> otHours;
otPay = (1.5*rate)*otHours;
cout << "\nOVERTIME PAY: " << otPay << endl;
grossPay = bPay + otPay;
cout << "GROSS PAY: " << grossPay << endl;
wageCompute(bPay, grossPay);
loopTimes++;
}
return EXIT_SUCCESS;
}
void wageCompute(int bPay, int grossPay)
{
double rate, dedInsurance, dedMedical, totDeduct, netPay, tax;
if (bPay<10001)
{
rate = 0.05;
}
else if (bPay<15001)
{
rate = 0.1;
}
else if (bPay<20001)
{
rate = 0.15;
}
else
{
rate = .2;
}
tax = bPay*rate;
dedInsurance = bPay*0.05;
dedMedical = bPay*0.01;
totDeduct = tax + dedInsurance + dedMedical;
cout << "TAX: " << tax << endl;
cout << "SSS DEDUCTION: " << dedInsurance << endl;
cout << "Medicare DEDUCTION: " << dedMedical << endl;
cout << "TOTAL DEDUCTIONS: " << totDeduct << endl;
netPay = grossPay - totDeduct;
cout << "NET PAY: " << netPay << endl;
}
The part where it goes wrong is when I input a certain value for the Hours Worked, Wage per Hour and Hours worked overtime. The program checks the basic pay for the suitable amount of tax it should deduct, what I input was 160 for the hours worked, 100 for the wage per hour, and 10 for overtime. I've tried lessening and increasing it and it worked just fine it seems that it's just these combination of numbers is the part where it goes wrong.
A screenshot of the output:
Your question isn't very clear but I suspect that what you are seeing here is a well known limitation of floating point numbers; numbers that are easy to represent exactly in base 10 don't have an exact representation in base 2. One example : 0.1 in base 10 is 0.0001100110011… repeating in base 2; the accuracy of the approximation depends on how many bits one is willing to use to write it.
An alternative approach is to use integer arithmetic with a known precision, so say you want to calculate to the nearest hundredth of a penny (I'm using UK currency here). Represent £1.01 as 10100 and when your finished val / 10000 is the pounds and (val % 10000) / 100 is the pence. If needed you can implement some more complex rules around rounding for the pence.