setw() alignment, help urgent please

I've been trying to align a mini receipt I made for purchasing a book but am having trouble making it line up when I enter in details. Can you help me figure out how to align this information perfectly so no matter what the user enters, the title aligns with the title below, ISBN aligns with ISBN, quantity, etc. Thanks!

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

   // Variables
    string userDate;
    string bookTitle;
    string ISBN_number;
    int bookQuantity;
    double bookPrice;
    double subtotal;
    double tax;
    char choice;
    
    // Display prompts
    do
    {
        {
            cout << "\t\t\tSerendipity Booksellers Cashier Module\n\n";
            cout << "\t\t\tToday's Date: ";
            cin.ignore();
            getline(cin, userDate);
            cout << "\t\t\tTitle: ";
            getline(cin, bookTitle);
            cout << "\t\t\tISBN: ";
            cin >> ISBN_number;
            cout << "\t\t\tQuantity of Book: ";
            cin >> bookQuantity;
            cout << "\t\t\tPrice: ";
            cin >> bookPrice;
        }
        
        // Give value to subtotal variable
        subtotal = bookPrice * bookQuantity;
        
        // Display receipt
        cout << "\n\t\t\tSerendipity Booksellers Receipt\n";
        cout << "Date: " << userDate << endl;
        cout << "Qty" << setw(10) << "ISBN" << setw(20) << "Title" << setw(30) << "Price" << setw(15) << "Total\n";
        cout << "-----------------------------------------------------------------------------" << endl;
        cout << bookQuantity << " " << setw(10) << ISBN_number << setw(20) << bookTitle << setw(26) << "$" << bookPrice << setw(10) << "$" << subtotal << endl;
    
        cout << "\nSubtotal: $" << subtotal << endl;
        tax = subtotal * 0.06;
        cout << "Tax: $" << fixed << showpoint << setprecision(2) << tax << endl;
        cout << "Total: $" << fixed << showpoint << setprecision(2) << tax + subtotal << endl << endl;
        
        cout << "Another transaction? (Y/N): ";
        cin >> choice;
        
        while (choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n')
        {
            cout << "Another transaction? (Y/N): ";
            cin >> choice;
        }
    } while (choice != 'N' && choice != 'n');
}
Hello darkslayer3,

Usually when I do something like this I will put the headings inside a single pair of quotes and separate the headings with spaces then line 36 is where I use the "setw()"s testing and changing values as needed.

Back in the day when I was taking the class for Assembly, COBOL or for both we had a sheet of paper to work out the output. I do not have that available right now, but I was thinking a spread sheet could be adjusted for the same purpose.

As I recall the test I made with your program the output was close and just needed small adjustments.

I would suggest adding "setw(2)" before the quantity in case the quantity is ever a two digit number.

Hope that helps,

Andy
the only way to do this is by adjusting the setw().

try changing the number'N' of width in setw(N).
Topic archived. No new replies allowed.