473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Aligning Decimals Problem in C++

So I have a basic assignment...

Expand|Select|Wrap|Line Numbers
  1. // File:     program2.cpp
  2. // Author:   Name
  3. // Course:   CSC 135
  4. // Date:     September 25, 2013
  5. // Purpose:  The program will ask for the number of acres and actual property value per acre. It will calculate total actual value, assessment value and the property tax.
  6.  
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <cmath>
  10. using namespace std;
  11.  
  12. const double TAXRATE = .064;
  13.  
  14. int main()
  15. {
  16.   int acresOwned;
  17.   double valuePerAcre, totalValue, assmntValue, propTax;
  18.  
  19.   // Inputs
  20.   cout << "Enter the number of acres owned: ";
  21.   cin >> acresOwned;
  22.   cout << "Enter the actual value per acre: ";
  23.   cin >> valuePerAcre;
  24.  
  25.   // Calculations
  26.   totalValue = valuePerAcre * acresOwned;
  27.   assmntValue = totalValue * .60;
  28.   propTax = assmntValue / 100 * TAXRATE;
  29.  
  30.   // Outputs
  31.   cout << endl;
  32.   cout << setw(8) << fixed << setprecision(2) << endl;
  33.   cout << "Acres owned: " << acresOwned << endl;
  34.   cout << "Total actual value: $ " << totalValue << endl;
  35.   cout << "Assessment value:  $ " << assmntValue << endl;
  36.   cout << "Property tax: $ " << propTax << endl;
  37.  
  38.   return 0;
  39. }
  40.  

I'm curious if anyone knows how I can line up the outputs to their decimals? the current setw and setprecision arn't doing anything.
Sep 25 '13 #1
2 4754
Nepomuk
3,112 Expert 2GB
Hi iToastie and welcome to bytes.com!

The modifiers setw and setprecision are valid only for the next stream object to come; in your case this is endl. So, you'll have to put it before every entry if you want it to be used for every entry.
Sep 25 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
I did this:

Expand|Select|Wrap|Line Numbers
  1.  cout <<  fixed
  2.      << "Acres owned:          x" << acresOwned << endl
  3.      << "Total actual value:  $x " << setw(8) <<setprecision(2) << right  << totalValue << endl
  4.      << "Assessment value:  $  x " << setw(8) << setprecision(2)  <<right << assmntValue << endl
  5.      << "Property tax:        $x " << setw(8) << setprecision(2) << right  << propTax << endl;
However, I am using Visual Studio which uses a variable font in the editor. So what you see in the code is not what is displayed. I added an x to each line ad adjusted the display until the x's lined up vertically. Then you data displays lined up on the decimal point.

If you change the editor font to a fixed font like Courier then you don't need to fiddle like I did.
Sep 25 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
0
by: thx | last post by:
I have MySQL 4.0.18 on Windows 2000 and I have a problem with floats and decimals. The table has floats and decimals fields but when I enter non-integers in these fields, they get rounded. So if I...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
0
by: Edwin.Madari | last post by:
for nth square root: use math.sqrt n times for example ... num = math.sqrt(num) ... 5.0 all comparisons work fine for arbitrary floating point numbers... For readability print them with...
0
by: Tommy Nordgren | last post by:
On 3 aug 2008, at 17.16, Edwin.Madari@VerizonWireless.com wrote: Ehum. The OP wants to compute the nth root ( not the nth square root) ------ What is a woman that you forsake her, and the...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello All, why is this code is not showing the result in this format: 0.00 and showing it as only 0 Private Sub btn1_Click Debug.Print(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.