Question

My project is to create this output using setw(), setfill(), setiosflags():

Enter KWH used 993

        C O M P S C I  Electric
------------------------------------------------
Kilowatts Used  993
------------------------------------------------

Base Rate    993 @ $  0.0475            $  47.17
Surcharge                               $   4.72
Citytax                                 $   1.42
______

Pay this amount                         $  53.31

After May 20th Pay                      $  55.44

I can get this to work using my code, but only if Kilowatts used is 2 digits. If it is more digits, my right justify does not work. I can edit the code so it justifies correctly for 3 digits, 4 digits, and so on. When my code will be test, I do not know what Kilowatts would be inputted, so I need code that works for Kilowatts with any length of digits.

    /* 
     * File:   main.cpp
     * Author: -------------------------
     *
     * Created on ----------------------
     */

    #include <cstdlib>
    #include <iomanip>
    #include <iostream>

    using namespace std;
    /*
     * 
     */
    int main() {
        float br; // base rate
        float surcharge; // surcharge
        float taxrate; // tax rate
        float late; // late rate

        float kwh; // kilowatts used
        float bc; // base cost
        float scc; // surcharge cost
        float ctc; // city tax cost

        br = .0475; // kilowatt rate
        surcharge = .1; // surcharge rate
        taxrate = .03; // tax rate
        late = .04; //rate if payment is late

        // cout.width(38);
        cout << "Kilowatts used last month: ";
        cin >> kwh; // get kwh used from user

        cout << "\n";

        cout << "\n";

        cout << "        C O M P S C I Electric        " << "\n";

        cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(2); // set ios flags
        cout << setfill('-') << setw(38) << "\n"; // display dashes in line
        cout << setiosflags(ios::left) << "Kilowatts Used" << setfill(' ') << setw(23) << resetiosflags(ios::left) << kwh << "\n"; // output kwh formally
        cout << setfill('-') << setw(38); // separate

        cout << "\n" << "\n";
        // calculations
        bc = br * kwh;
        scc = surcharge * kwh;
        ctc = taxrate * kwh;
        cout << left << "Base Rate    " << kwh << " @ $0.0475 " << setfill(' ') << setw(4) << right << "$" << bc << "\n"; //output base rate, right justify cost
        cout << left << "Surcharge" << setw(24) << right << "$" << scc << "\n"; // output surcharge cost, right justify cost
        return 0;
    }

I don't understand the need for setw() when I use resetiosflags(ios::left) to right justify. I tried replacing setiosflags(ios::left) and resetiosflags(ios::left) with the right and left;

I am a c++ beginner using netbeans c++ to code and g++ to compile. I really don't know much about formatting console output much, so I would appreciate any help. I don't necessarily have to use the 3 functions specified at the top, as long as a different way works consisently with any float length. Thanks!

Was it helpful?

Solution

setw(n) sets the width of the next output while setiosflags(ios::left) and setiosflags(ios::right) sets the justification for that output. I prefer to use std::left and std::right instead of setiosflags because it looks cleaner to me.

When you were outputting the base rate like this:

cout << left << "Base Rate    " << kwh << " @ $0.0475 " << setfill(' ') << setw(4) << right << "$" << bc << "\n"; //output base rate, right justify cost

The part of this line that says setw(4) was applying a width of 4 ONLY to the dollar sign, not the dollar sign AND variable bc. This is why you were getting different results for different inputs. No width was being specified for the variable output itself. Every time you want to output something at a specific width you must call setw before it. Also, it's a good idea to separate lengthy lines of code like this into separate lines for readability.

I corrected this code to:

/* 
 * Output Base Rate 
 */
cout << setfill(' ');
cout << left << "Base Rate   ";
cout << right << setw(8) << kwh; 
cout << " @ $" << right;

// Reset precision to 4 to output kilowatt rate, then reset back to 2 for currency
cout << setprecision(4);
cout << setw(6) << br;
cout << setprecision(2);

cout << setw(10) << "$";
cout << setw(7) << bc << "\n"; 

I also find it useful to change the fill character to _ instead of a space when attempting to debug a console application output. The setw function is very important in console output especially when you are trying to output everything in a fixed width. For example, if you want to output:

Surcharge                               $   4.72

There are three parts to this:

  • Surcharge text
  • The fill between that and the dollar sign
  • The output of the amount itself

This can be accomplished by:

cout << left << "Surcharge";
cout << right << setw(31) << "$";
cout << setw(7) << scc << "\n"; 

The determined that 31 was the proper amount of width for the dollar because my output width is 49 (including the new line char). I took the number of characters in "Surcharge" (9) the dollar sign (1) and width I wanted my currency output (7) - (4 digits plus the decimal plus the precision) and, totaled that (17) added one extra for the new line (\n) and subtracted it from 49 (31).

I have retooled your code a little bit and it matches your project's output at the top. I have put some comments in there to make things a little more clear.

Here it is:

float br; // base rate
float surcharge; // surcharge
float taxrate; // tax rate
float late; // late rate

float kwh; // kilowatts used
float bc; // base cost
float scc; // surcharge cost
float ctc; // city tax cost

br = .0475; // kilowatt rate
surcharge = .1; // surcharge rate
taxrate = .03; // tax rate
late = .04; //rate if payment is late

// cout.width(38);
cout << "Kilowatts used last month: ";
cin >> kwh; // get kwh used from user

cout << "\n";

cout << "\n";

cout << "            C O M P S C I Electric             " << "\n";

// Set float precision and formats
cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); 

// Display first serparator
cout << setfill('-') << setw(48) << "\n"; 

// Display Kilowatts input
cout << left << "Kilowatts Used" << setfill(' ') << setw(8) << right << kwh << "\n"; 

// Display second separator
cout << setfill('-') << setw(48); // separate

cout << "\n" << "\n";

// do calculations
bc = br * kwh;  
scc = surcharge * kwh;
ctc = taxrate * kwh;

/* 
* Output Base Rate 
*/
cout << setfill(' ');
cout << left << "Base Rate   ";
cout << right << setw(8) << kwh; 
cout << " @ $" << right;

// Reset precision to 4 to output kilowatt rate, then reset back to 2 for currency
cout << setprecision(4);
cout << setw(6) << br;
cout << setprecision(2);

cout << setw(10) << "$";
cout << setw(7) << bc << "\n"; 

/*
 * Output Surcharge 
 */
cout << left << "Surcharge";
cout << right << setw(31) << "$";
cout << setw(7) << scc << "\n"; 

return 0;

Hope this helps.

Tom

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top