0

I'm still a beginner at coding but I am really confused about how setw() works. I am trying to align my outputs but I am unable to do so. I've looked at cplusplus website but obviously I still cant figure it out.

Maybe someone can provide an example or look at my actual code and output?

/*A retail company must file a monthly sales tax report listing the sales for the month and the amount of
sales tax collected. Write a program that asks for the month, the year, and the total amount collected
at the cash register (that is, sales plus sales tax). Assume the state sales tax is 4 percent and the county
sales tax is 2 percent.*/

#include <iostream>
#include <iomanip>
using namespace std;

double calcStateTax(double inp) {
    inp *= 0.04;
    return inp;
}

double calcCountyTax(double inp){
    inp *= 0.02;
    return inp;
}

double calcSales(double inp){
    inp = inp - (calcStateTax(inp) + calcCountyTax(inp));
    return inp;
}

void outPut(string month, int year, double tot){

    cout << "Month: " << month << " " << year << endl;
    cout << "--------------------------\n";
    cout << "Total Collected: $" << fixed << setprecision(2) << setw(20) << tot << endl;
    cout << "Sales: $" << setw(20) << fixed << setprecision(2) << setw(20) << calcSales(tot) << endl;
    cout << "County Sales Tax: $" << setw(20) << fixed << setprecision(2) << setw(20) << calcCountyTax(tot) << endl;
    cout << "State Sales Tax: $" << setw(20) << fixed << setprecision(2) << setw(20) << calcStateTax(tot) << endl;
    cout << "Total Sales Tax: $" << setw(20) << fixed << setprecision(2) << setw(20) << (calcStateTax(tot)+calcCountyTax(tot)) << endl;

}

int main() {
    string month;
    int year;
    double totAmount;

    cout << "Please enter the month: ";
    cin >> month;
    cout << "\nPlease enter the year: ";
    cin >> year;
    cout << "\nPlease enter total money collected including tax money: ";
    cin >> totAmount;
    cout << endl;

    outPut(month, year, totAmount);


    return 0;
}

Output:

Please enter the month: June

Please enter the year: 2010

Please enter total money collected including tax money: 15000

Month: June 2010
--------------------------
Total Collected: $            15000.00
Sales: $            14100.00
County Sales Tax: $              300.00
State Sales Tax: $              600.00
Total Sales Tax: $              900.00
Program ended with exit code: 0

As you can see.. the numbers don't align properly. How do I use it setw()? Or is there a better function I can use for what I am trying to do?

Please enter the year: 2010

Please enter total money collected including tax money: 15000

                                           Month: june 2010
                       --------------------------
                                Total Collected: $  15000.00
                                          Sales: $  14100.00
                               County Sales Tax: $    300.00
                                State Sales Tax: $    600.00
                                Total Sales Tax: $    900.00
Panthy
  • 1,605
  • 4
  • 19
  • 27
  • What do you expect it *to* do? `setw(20)` makes sure the next thing fed to the stream will occupy 20 characters. `"____________15000.00"` is 20 characters worth of text (spaces included). – Colin Basnett Mar 05 '16 at 00:49
  • I honestly don't know what it does, I thought that if I set all my outputs to the same character length, it would align them automatically – Panthy Mar 05 '16 at 00:51
  • I'll give you a tip you can apply to almost every problem you'll encounter that will save you hours upon hours of your time: [read the documentation](http://en.cppreference.com/w/cpp/io/manip/setw). – Colin Basnett Mar 05 '16 at 00:52
  • The stuff to the right of `setw` will only line up if the stuff to the left does too. – Mark Ransom Mar 05 '16 at 01:00
  • I think to fix my code, I have to setw all of the outputs in each cout – Panthy Mar 05 '16 at 01:00

0 Answers0