I'm having some issues using setprecision. I don't understand how it works completely. I searched the problem and was able to extrapolate some code that should've worked. I don't understand why it's not. Thank you for your help, I'm still kind of new at this.
//monthly paycheck.cpp
//Paycheck Calculator
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
//Constants
const double
FEDERAL_TAX = 0.15, //Federal Tax
STATE_TAX = 0.035, //State Tax
SSA_TAX = 0.085, //Social Security & Medicare
HEALTH_INSURANCE = 75; //Health Insurance
//Variables
int year;
double grossAmount;
string employeeName, month;
// Initialize variables with input
cout << "Hello, what's your first name? ";
cin >> employeeName;
cout << "What is your gross amount? ";
cin >> grossAmount;
cout << "Please enter the month and year: ";
cin >> month >> year;
// Output
cout << "***********************************" << endl;
cout << "Paycheck" << endl;
cout << "Month: " << month << "\tYear: " << year << endl;
cout << "Employee Name: " << employeeName << endl;
cout << "***********************************" << endl;
cout << setprecision(5) << fixed;
cout << "Gross Amount: $" << grossAmount << endl;
cout << "Federal Tax: $" << FEDERAL_TAX*grossAmount << endl;
cout << "State Tax: $" << STATE_TAX*grossAmount << endl;
cout << "Social Sec / Medicare: $" << SSA_TAX*grossAmount << endl;
cout << "Health Insurance: $" << HEALTH_INSURANCE << endl << endl;
cout << "Net Amount: $" << fixed << grossAmount-grossAmount*(FEDERAL_TAX+STATE_TAX+SSA_TAX)-HEALTH_INSURANCE << endl << endl;
system("PAUSE");
return 0;
}
If you want to format floats to display with 2 decimal places in C++ streams, you could easily:
float a = 5.1258f;
std::cout << std::fixed << std::setprecision(2) << a << std::endl;
See std::fixed and std::setprecision
Use stream manipulators:
std::cout.fixed;
std::cout.precision(Number_of_digits_after_the_decimal_point);
Related
I am having trouble with my code not reading the whole string name.
I have tried using getline(cin, movieName) as well as cin.ignore(), but I can't get it to work properly.
I am taking a beginner's course to this class, but my professor does not answer their emails.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string movieName;
const float adultTicketPrice = 12;
const float childTicketPrice = 6;
const float percentTheaterKeeps = .20;
int adultTicketsSold, childTicketsSold;
float grossProfit, netProfit, amountPaidToDistributor;
getline(cin, movieName);
cout << fixed << showpoint << setprecision(2);
cout << "What movie played tonight?\t ";
cin >> movieName;
cout << "How many adult tickets were sold? ";
cin >> adultTicketsSold;
cout << "How many child tickets were sold? ";
cin >> childTicketsSold;
grossProfit = adultTicketPrice * adultTicketsSold + childTicketPrice * childTicketsSold;
netProfit = grossProfit * percentTheaterKeeps;
amountPaidToDistributor = grossProfit - netProfit;
cout << setw(10) << left << "Movie Name";
cout << setw(23) << right << movieName << endl;
cout << setw(10) << left << "Adult Tickets Sold";
cout << setw(16) << right << adultTicketsSold << endl;
cout << setw(10) << left << "Child tickets sold";
cout << setw(16) << right << childTicketsSold << endl;
cout << setw(10) << left << "Gross Box Office Profit";
cout << setw(11) << right << grossProfit << endl;
cout << setw(10) << left << "Net Box Office Profit";
cout << setw(13) << right << netProfit << endl;
cout << setw(10) << left << "Amount Paid to distributor";
cout << setw(8) << right << amountPaidToDistributor << endl << endl;
return 0;
}
You need to use std::getline(std::cin, movieName) when you want to get input for movieName.
Presumably you're seeing a blank line when you start your program, and pressing enter. This sets movieName to '\n'. Remove that line, and switch std::cin >> movieName; with std::getline(std::cin, movieName). (Remove the std::s if you're using a using declaration)
So I'm writing a basic application and for some reason when I run the program a bunch of numbers pop up before my intended output. It was working fine until I added the "std::cout" lines to have the outputs only be 2 decimals. The general gist of the application is a program acts as a self-checkout register at a store and lets the user buy 2 items. And yes I know the code probably looks really bad, I'm still super new to C++.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float price1;
float number1;
float price2;
float number2;
float priceofitemplustax1;
float priceofitemplustax2;
float total;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2;
std::cout << total;
cout << endl << "Please scan your first item." <<endl;
cin.get();
cout << endl << "How many of that item are you buying? "<<endl;
cin >> number1;
cout << endl << "How much is that item?"<<endl;
cin >> price1;
priceofitemplustax1 = (number1 * price1) * 1.0875;
cout << endl << "So you want " << number1 << " of this item? Adding tax that will be " << priceofitemplustax1 << "."<<endl;
cin.get();
cout << endl << "Please scan your second item."<<endl;
cin.get();
cout << endl << "How many of that item are you buying? "<<endl;
cin >> number2;
cout << endl << "How much is that item?"<<endl;
cin >> price2;
priceofitemplustax2 = (number2 * price2) * 1.0875;
cout << endl << "So you want " << number2 << " of this item? Adding tax that will be " << priceofitemplustax2 << "."<<endl;
cin.get();
total = priceofitemplustax1 + priceofitemplustax2;
cout << endl << "So your final total for this shopping trip including tax is " << total << "."<<endl;
cin.get();
cout << endl << "Your reciept will print below."<<endl;
cin.get();
cout << setw(14) << right << "Number of Item" << setw(10) << right << "Price" << setw(20) << "Price plus tax" << endl;
cout << setw(14) << right << number1 << setw(10) << right << price1 << setw(20) << priceofitemplustax1 << endl;
cout << setw(14) << right << number2 << setw(10) << right << price2 << setw(20) << priceofitemplustax2 << endl;
cout << endl;
cout << endl;
cout << setw(8) << right << "Total is" << setw(10) << total << price2 << endl;
cin.get();
}
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2; std::cout << total;
here you write 5 floats
The lines
std::cout << std::fixed; // sets a format
std::cout << std::setprecision(2); // sets a format
set the streams output format.
The lines
std::cout << price1; // outputs a number
std::cout << price2; // outputs a number
std::cout << priceofitemplustax1; // outputs a number
std::cout << priceofitemplustax2; // outputs a number
std::cout << total; // outputs a number
print the variables to the stream.
Just remove the variable output lines. Do not accept this answer - Credit goes to manni66
I've been working on this homework assignment for a while and am about ready to pull my hair out.
I need help rounding a float to the tenths place while still showing a 0 in the hundredths place and nothing I do seems to do that.
i.e. 2.47 = 2.50
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float MPH;
float seconds;
const float MPH2MPS = (1609.00 / 3600.00);
float a;
cout << " Acceleration calculator" << endl;
cout << "" << endl;
cout << "Please enter the velocity in miles per hour: ";
cin >> MPH;
cout << "" << endl;
cout << "Please enter the time in secounds: ";
cin >> seconds;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;
a = MPH2MPS * ( MPH / seconds );
cout << showpoint << fixed << setprecision(2);
cout << "The acceleration required by a vehicle to reach" << endl;
cout << "" << endl;
cout << "a velocity of " << MPH << " miles per hour in " << seconds << " seconds" << endl;
cout << "" << endl;
cout << "is " << setprecision(1) << a << " meters per second" << endl;
cout << "" << endl;
cout << "" << endl;
system("pause");
return 0;
Any ideas?
Reading #asterite's answer here:
cout << "is " << static_cast<double>(std::round(a * 10)) / 10 << " meters per second" << endl;
Note the round function only works if you #include <cmath>
Update:
Modified math.h with cmath, thanks to #user4581301
assignment at school asks me to find the present value using double, and void. i was able to write my code up to a certain degree but the result is not what i was expecting.. i ended up separating the present value into different section so at the end i'd multiply the amount given with the rest.. any tips on how to make the code actually work the way its supposed to?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double payment,year_term, interest;
double sum;
double Power;
double presentv;
double present;
cout << "Hello, how are you doing?" << endl;
cout << "Please insert a payment amount" << endl;
cin >> payment;
cout << " amount inserted: " << payment << endl;
cout << "Enter number of years" << endl;
cin >> year_term;
cout << " number of years: " << year_term << endl;
cout << "Enter interest rate" << endl;
cin >> interest;
cout << " the interest is: " << interest << "%" << endl;
Presentv = ((1 - (pow((1 + interest),year_term))))/interest;
cout << " the value: " << Presentv << endl;
presentva = payment * Presentv;
cout << " the present value is: " << presentva << endl;
}
it says in the instruction "you may assume the cash to be equal or greater than the total bill". How do I declare a value for cash and make it greater than or equal to the total bill? Is my declaration of the variables for "change" "totalBill" are correct? I really need some help and like I said this is my first time.
#include <iostream>
#include <iomanip>
using namespace std;
const double CHILDPRICE = 10.00;
const double ADULTPRICE = 20.50;
int main ()
{
double adultTotal, childTotal, totalBill, change;
int childTix, adultTix, cash;
cout << "\n Chesapeake Amusement Park" << endl << endl;
cout << " Enter children tickets... ";
cin >> childTix;
cout << " Enter adult tickets...... ";
cin >> adultTix;
childTotal = CHILDPRICE * childTix;
adultTotal = ADULTPRICE * adultTix;
totalBill = childTotal + adultTotal;
change = cash - totalBill;
//The following is a sample of how you can format the output
//childTix is an integer variable
//CHILDPRICE is a double const
//childTotal is a double variable
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\n\n Chesapeake Amusement Park";
cout << "\n -------------------------";
cout << "\n\n Tickets Price Total\n";
cout << " Children " << setw(3) << childTix
<< setw(14) << CHILDPRICE
<< setw(11) << childTotal;
cout << "\n Adults " << setw(3) << adultTix
<< setw(14) << ADULTPRICE
<< setw(11) << adultTotal;
cout << "\n ";
cout << "\n ";
cout << "\n Total Bill " << setw(11) << totalBill;
//Place remaining input/output statements here
cout << "\n ";
cout << "\n ";
cout << "\n Cash recieved...... ";
cin >> cash;
cout << "\n ";
cout << "\n ";
cout << "\n Change " << setw(4) << change;
return 0;
}