Making decimal points lined up

Hey guys! I'm trying to line up the decimal point results at the end. Also I'm trying to figure out how to line up the question and numbers when asking for the values in console, I'm assuming I would need "\t" in between the couts. Finally my Tax Rate % is not showing me the proper output when results are shown under TAX.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>

using namespace std;

#define TAX_RATE .0875

int main()
{

   //declare constant unit prices
   const double TVUnitPrice = 500.00;
   const double DVDUnitPrice = 380.00;
   const double REMOTEUnitPrice = 35.20;
   const double CDUnitPrice = 74.50;
   const double AUDIOUnitPrice = 1500.00;

   //declare number of units
   double tvPrice;
   double dvdPrice;
   double remotePrice;
   double cdPrice;
   double audioPrice;
   double subTotal;

   //declare number of units
   int tvUnits;
   int dvdUnits;
   int remoteUnits;
   int cdUnits;
   int audioUnits;

   cout << "How many TVs were sold?\t";
      cin >> tvUnits;

      cout << "How many DVD players were sold?\t";
      cin >> dvdUnits;
   
      cout << "How many Remote Controller units were sold?\t";
      cin >> remoteUnits;

      cout << "How many CD Players were sold?\t";
      cin >> cdUnits;
   
      cout << "How many AV Processors were sold?\t";
      cin >> audioUnits;

      tvPrice = tvUnits*TVUnitPrice;
      dvdPrice = dvdUnits*DVDUnitPrice;
      remotePrice = remoteUnits*REMOTEUnitPrice;
      cdPrice = cdUnits*CDUnitPrice;
      audioPrice = audioUnits*AUDIOUnitPrice;
      cout << fixed << setprecision(2);

      cout << "QTY\tDescription\t\tUnit Price\tTotal Price" << endl;
      cout << tvUnits << "\t" << "TV" << "\t\t\t" << TVUnitPrice << "\t\t" << tvPrice << endl;
      cout << dvdUnits << "\t" << "DVD" << "\t\t\t" << DVDUnitPrice << "\t\t" << dvdPrice << endl;
      cout << remoteUnits << "\t" << "REMOTE CONTROLLER" << "\t" << REMOTEUnitPrice << "\t\t" << remotePrice << endl;
      cout << cdUnits << "\t" << "CD PLAYER" << "\t\t" << CDUnitPrice << "\t\t" << cdPrice << endl;
      cout << audioUnits << "\t" << "AV PROCESSOR" << "\t\t" << AUDIOUnitPrice << "\t\t" << audioPrice << endl;

      subTotal = tvPrice + dvdPrice + remotePrice + cdPrice + audioPrice;

      cout << "\t\t\t" << "SUBTOTAL\t\t" << subTotal << endl;
      cout << "\t\t\t" << "TAX\t\t\t" << TAX_RATE << endl;
      cout << "\t\t\t" << "TOTAL\t\t\t" << (subTotal*TAX_RATE) << endl;

      return 0;   
}
Last edited on
Edit your final code bracket to be [/code]
setw(width) may be what you seek.
you can also try 'right' (works like 'fixed').
How would you change it so that it will be modified as such that it can handle 0 sales number for any product, and when the sale is 0 unit for a product, it doesn't print any sales data for that product.
In my opinion, lining up variable sized strings should usually be done with the strings themselves, which might include producing the table of strings as a preparation step. That way you have a lot more flexibility.

In more restricted cases you can get away with using the formatted stream output options over floating point values.

Here is a quick hack to demonstrate options.

Notice that adding a number with a greater number of non-fractional digits breaks the formatted function if the value we use with std::setw() is not sufficiently prescient.

The string methods, in comparison, have no issue either way.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>


// double xs[] =
std::vector <double> xs =
{
    3.14159265,
   22.6,
   -7.43,
  174,
    1.26,
    0.8
};


void fixed_iomanip_method( std::size_t max_non_fractional_digits )
{
  // This method assumes a maximum non-fractional width of 3, + 1 for '.', + 6 fractional places
  std::cout << "\nfixed iomanip method: " << max_non_fractional_digits << " + 1 + 6\n";

  for (auto x : xs)
  {
    std::cout << std::fixed << std::setw( max_non_fractional_digits + 7 ) << std::setprecision( 6 ) << x << "\n";
  }
}


template <class ToString>
void variable_strings_method( const char* message, ToString&& to_string )
{
  // This method will pad the left of the converted strings to align the decimal point, if any
  // The shape of the decimal->string conversion depends on your conversion function.
  std::cout << "\nvariable strings method " << message << "\n";

  std::vector <std::pair <std::string, std::size_t> > sns;
  std::size_t                                         max_n = 0;

  for (auto x : xs)
  {
    auto s = to_string( x );
    auto n = (s.find('.') == s.npos) ? s.size() : s.find('.');
    max_n = std::max( max_n, n );
    sns.emplace_back( std::make_pair( s, n ) );
  }

  for (const auto& [s, n] : sns)
  {
    std::cout << std::string( max_n - n, ' ' ) << s << "\n";
  }
}


std::string to_string_and_strip_trailing_zeros( double x )
{
  auto s = std::to_string( x );
  auto n = s.find_last_not_of( ".0" );
  return s.substr( 0, n + 1 );
}


int main()
{
  fixed_iomanip_method( 3 );
  variable_strings_method( "(standard)", (std::string (*)( double ))std::to_string );
  variable_strings_method( "(strip trailing zeros)", to_string_and_strip_trailing_zeros );
  
  xs.emplace_back( 1234.5678 );
  
  fixed_iomanip_method( 3 );
  fixed_iomanip_method( 4 );
  variable_strings_method( "(standard)", (std::string (*)( double ))std::to_string );
  variable_strings_method( "(strip trailing zeros)", to_string_and_strip_trailing_zeros );
}

I originally coded this with just an array of doubles, but then added lines 71..76 and the additional argument to fixed_iomanip_method() to line 20, all to demonstrate the issues.

[edit]
Here is the output of the above program:

fixed iomanip method: 3 + 1 + 6
  3.141593
 22.600000
 -7.430000
174.000000
  1.260000
  0.800000

variable strings method (standard)
  3.141593
 22.600000
 -7.430000
174.000000
  1.260000
  0.800000

variable strings method (strip trailing zeros)
  3.141593
 22.6
 -7.43
174
  1.26
  0.8

fixed iomanip method: 3 + 1 + 6
  3.141593
 22.600000
 -7.430000
174.000000
  1.260000
  0.800000
1234.567800

fixed iomanip method: 4 + 1 + 6
   3.141593
  22.600000
  -7.430000
 174.000000
   1.260000
   0.800000
1234.567800

variable strings method (standard)
   3.141593
  22.600000
  -7.430000
 174.000000
   1.260000
   0.800000
1234.567800

variable strings method (strip trailing zeros)
   3.141593
  22.6
  -7.43
 174
   1.26
   0.8
1234.5678


Hope this helps.
Last edited on
Hey Thanks for the help Duthomas I figured it out

̶]̶T̶h̶e̶ ̶o̶n̶l̶y̶ ̶p̶r̶o̶b̶l̶e̶m̶ ̶n̶o̶w̶ ̶i̶s̶ ̶t̶h̶a̶t̶ ̶t̶h̶e̶ ̶o̶u̶t̶p̶u̶t̶ ̶d̶i̶s̶p̶l̶a̶y̶s̶ ̶2̶ ̶d̶i̶f̶f̶e̶r̶e̶n̶t̶ ̶s̶i̶g̶ ̶f̶i̶g̶s̶,̶ ̶a̶d̶d̶i̶n̶g̶ ̶.̶0̶0̶ ̶a̶t̶ ̶t̶h̶e̶ ̶e̶n̶d̶.̶ ̶I̶ ̶b̶e̶l̶i̶e̶v̶e̶ ̶i̶t̶s̶ ̶b̶e̶c̶a̶u̶s̶e̶ ̶t̶h̶e̶r̶e̶ ̶a̶r̶e̶ ̶n̶o̶ ̶c̶e̶n̶t̶s̶/̶d̶e̶c̶i̶m̶a̶l̶s̶ ̶f̶o̶r̶ ̶t̶h̶e̶ ̶f̶i̶r̶s̶t̶ ̶2̶,̶ ̶t̶r̶i̶e̶d̶ ̶t̶w̶e̶a̶k̶i̶n̶g̶ ̶s̶e̶t̶p̶r̶e̶c̶i̶s̶i̶o̶n̶ ̶b̶u̶t̶ ̶i̶t̶ ̶d̶o̶s̶e̶n̶'̶t̶ ̶w̶o̶r̶k̶.̶

fixed. sorry
Last edited on
You figured it out... except it still isn’t working right?

Had you bothered executing the demonstration code I provided, you would see that I addressed the trailing zeros issue.

Funny that.



I’m not trying to be mean. But really, ask for help, ignore said help, then complain that you are still unable to do something that the help specifically mentions... And all you had to do was click the “Edit and Run” button.

What would your reaction be if someone did that to you?



If I have missed something specific that you need and cannot figure it out yourself, post again and we’ll address it.

Hope this helps.
No worries. :O)
std::put_money method (works well only if the locale of interest is supported):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>

int main()
{
    std::cout.imbue( std::locale("en_US.utf8") ); // note: locale names are implementation specific 
    std::cout << std::showbase ; // required to show the currency symbol

    for( int cents : { 12345678, 0, -2345, 999999, -86 } )
    {
         std::cout << std::setw(12) << std::put_money(cents) 
                   << std::setw(32) << std::put_money(cents,true) << '\n' ;
    }  
}

http://coliru.stacked-crooked.com/a/e3a0bc383c7ca08d
https://rextester.com/YWIRU24457
Topic archived. No new replies allowed.