I am trying to right align my numbers to make them look like this:

[TEX]

Total Collected: $ 26572.89
Sales: $ 25068.76
County Sales Tax: $ 501.38
State Sales Tax: $ 1002.75
[/TEX]

The problem is i dont know how to format it in my code. Here is my code that i have so far:

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

int main()
{
    //Declare variables
    const double STATE_SALES_TAX_RATE = 0.04;
    const double COUNTY_SALES_TAX_RATE = 0.02;
    const double SALES_TAX = 0.06;
    string month;
    int year;
    double totalCollected;
    double productSales;
    double countySalesTax;
    double stateSalesTax;
    double totalIncome;
    
    
    cout << "Enter the month: ";
    getline(cin, month);
    
    cout << "Enter the year: ";
    cin >> year;
    
    cout << "Total amount collected at register: ";
    cin >> totalCollected;
    
    productSales = (totalCollected) / 1.06;
    countySalesTax = productSales * COUNTY_SALES_TAX_RATE;
    stateSalesTax = productSales * STATE_SALES_TAX_RATE;
    totalIncome = countySalesTax + stateSalesTax;
    
    //Display the monthly figures
    cout << "\n"<< month << " "<< year <<"\n"; 
    cout << "----------\n";
    cout << fixed << setprecision(2);
    cout << "Total Collected:  "  << setw(12) << "$ " << totalCollected << endl;
    cout << "Sales:            "  << setw(12) << "$ " << productSales << endl;
    cout << "County Sales Tax: "  << setw(12) << "$ " << right<<countySalesTax << endl;
    cout << "State Sales Tax:  "  << setw(12) << "$ " << stateSalesTax << endl;
    cout << "Total Sales Tax:  "  << setw(12) << "$ " << totalIncome   << endl;
    
    system("pause");
    
    return 0;
    
}

It's not showing up above - is it that you want the numbers all aligned at the decimal, like

123.56
 56.66
  3.99

If so, you need to put the setw(12) after the $ output. If you want the $ right up against the numbers, which are decimal point aligned, hmmm, that would be more complex.

Try this approach:

std::string S(double sum)
{
    std::ostringstream s;
    s << '$' << std::fixed << std::setprecision(2)
        << sum;
    return s.str();
}
...
cout << "County Sales Tax: " << setw(12) << S(countySalesTax) << endl;
...

If i want to wirte this code to a file, how do i do that? Im having a hard time figuring out if i need to put the outfile first and then do the infile stuff but then when i run it, it doesn't display the same information as in the original...can someone help?

const double STATE_SALES_TAX_RATE = 0.04;
    const double COUNTY_SALES_TAX_RATE = 0.02;
    const double SALES_TAX = 0.06;
    string month;
    int year;
    double totalCollected;
    double productSales;
    double countySalesTax;
    double stateSalesTax;
    double totalIncome;
    ifstream inFile;
    ofstream outputFile;
    outputFile.open("monthlyTax.txt");
    
    cout << "Enter the month: ";
    getline(cin, month);
    
    cout << "Enter the year: ";
    cin >> year;
    
    cout << "Total amount collected at register: ";
    cin >> totalCollected;
    
    productSales = (totalCollected) / 1.06;
    countySalesTax = productSales * COUNTY_SALES_TAX_RATE;
    stateSalesTax = productSales * STATE_SALES_TAX_RATE;
    totalIncome = countySalesTax + stateSalesTax;
    
    //Display the monthly figures
    outputFile << "\n"<< month << " "<< year <<"\n"; 
    outputFile << "----------\n";
    cout << fixed << setprecision(2);
    outputFile << "Total Collected:   "   << "$ " << setw(8) << totalCollected 
         << endl;
    outputFile << "Sales:             "   << "$ " << setw(8) << productSales 
         << endl;
    outputFile << "County Sales Tax:  "   << "$ " << setw(8) << countySalesTax 
         << endl;
    outputFile << "State Sales Tax:   "   << "$ " << setw(8) << stateSalesTax 
         << endl;
    outputFile << "Total Sales Tax:   "   << "$ " << setw(8) << totalIncome   
         << endl;
    
    system("pause");
    return 0;
    
}

cout << fixed << setprecision(2); formats what is displayed on the screen outputFile << fixed << setprecision(2); formats how it looks in the file

what im having problem understanding is if i should use the output file stuff first then the input file...because the user has to input certain information and then i have to display those results to the screen i think using the inFile

Maybe what i said wasnt clear. I am trying to modify the code i originally posted so that it writes to an output file instead of to teh screen. I'm having trouble figuring out how to do this...

What you've posted above appears to do that. You're taking input from the console (keyboard) and outputting to the file. Don't you see that happening when you run the program?

This article has been dead for over six months. Start a new discussion instead.