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