Aligning Decimals Problem in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iToastie
    New Member
    • Sep 2013
    • 1

    Aligning Decimals Problem in C++

    So I have a basic assignment...

    Code:
    // File:     program2.cpp
    // Author:   Name
    // Course:   CSC 135
    // Date:     September 25, 2013
    // 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.
    
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    const double TAXRATE = .064;
    
    int main()
    {
      int acresOwned;
      double valuePerAcre, totalValue, assmntValue, propTax;
    
      // Inputs
      cout << "Enter the number of acres owned: ";
      cin >> acresOwned;
      cout << "Enter the actual value per acre: ";
      cin >> valuePerAcre;
    
      // Calculations
      totalValue = valuePerAcre * acresOwned;
      assmntValue = totalValue * .60;
      propTax = assmntValue / 100 * TAXRATE;
    
      // Outputs
      cout << endl;
      cout << setw(8) << fixed << setprecision(2) << endl;
      cout << "Acres owned: " << acresOwned << endl;
      cout << "Total actual value: $ " << totalValue << endl;
      cout << "Assessment value:  $ " << assmntValue << endl;
      cout << "Property tax: $ " << propTax << endl;
    
      return 0;
    }

    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.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    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.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      I did this:

      Code:
       cout <<  fixed
           << "Acres owned:          x" << acresOwned << endl
           << "Total actual value:  $x " << setw(8) <<setprecision(2) << right  << totalValue << endl
           << "Assessment value:  $  x " << setw(8) << setprecision(2)  <<right << assmntValue << endl
           << "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.

      Comment

      Working...