Strange error with setprecision ()
Sep 23, 2012 at 4:59am UTC
OK, so I am having a strange error occur in a small bit of code I'm writing and from what i can tell it shouldn't be happening. The error occurs when trying to force all my QTY items to display a decimal point and the first one doesn't. Here is the code. It only happens on line 57 of the code and i can not figure out why. Please Help.
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 56 57 58 59 60 61 62 63 64 65 66 67 68
// NWPC1.cpp : Defines the entry point for the console application.
// Console App to gain retail info and display a sample invoice
//James M Snook III 09/20/12
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main ()
{
//Set Variables
double nwpc; // number of NW-PC
double sdo; // Number of Second Drive Options
double camera; // Number of Cameras
double software; // Number of Software Bundles
double nwpct; // Total cost of NW-Pc @ $1498.23
double sdot; // Total cost of Second Drive Option @ $84.00
double camerat; // Total Cost of Cameras @ $119.00
double softwaret; // Total Cost of Software @ $49.00
double subtotal; // Total Cost Before Tax
double stax; // Total Tax with a tax rate of 7.65%
double total; // Complete total of everything including tax
string junk;
// Get input
cout << "Enter total Number of NW-PC's being purchased. " ;
cin >> nwpc;
cout << "Enter the Number of Second Drive Options being purchased. " ;
cin >> sdo;
cout << "Enter the Number of Cameras being purchased. " ;
cin >> camera;
cout << "Enter the Number Software Bundles being Purchased. " ;
cin >> software;
// Calculate
nwpct = (nwpc * 1498.23);
sdot = (sdo * 84.00);
camerat = (camera * 119.00);
softwaret = (software * 49.00);
subtotal = (nwpct +sdot + camerat + softwaret);
stax = (subtotal * .0765);
total = (subtotal + stax);
//Output
cout << endl << setw (32) << "*************************" << endl;
cout << endl << setw (28) << "NewWave Computers"
<< endl << setw (25) << "123 Main St"
<< endl << setw (30) << "Somewhere, MO 64444" ;
cout << endl << endl << setw (3) << "Qty" << setw (22) << " Item " << " " << setw (10) << "Cost " ;
cout << endl << setprecision(0) << setw (3) << nwpc << setw (23) << " NW-PC " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << nwpct ;
cout << endl << setprecision(0) << setw (3) << sdo << setw (22) << " Second Drive Option " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << sdot ;
cout << endl << setprecision(0) << setw (3) << camera << setw (22) << " Camera " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << camerat ;
cout << endl << setprecision(0) << setw (3) << software << setw (2) << " Software Bundle " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << softwaret ;
cout << endl << setw(36) << "-----------" ;
cout << endl << setw(25) << "Subtotal " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << subtotal;
cout << endl << setw(25) << "Sales Tax " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << stax;
cout << endl << setw(36) << "-----------" ;
cout << endl << setw(25) << "Total " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << total;
// Pause Screen
cout << endl << endl << "Press any Key then hit Enter to Exit..." ;
cin >> junk;
return (0);
}
Last edited on Sep 23, 2012 at 2:43pm UTC
Sep 23, 2012 at 12:57pm UTC
@jsnookii
Not sure why this way will put a decimal point into the display, but it works.
Show decimal point and a zero..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//Output
cout << endl << setw (32) << "*************************" << endl;
cout << endl << setw (28) << "NewWave Computers"
<< endl << setw (25) << "123 Main St"
<< endl << setw (30) << "Somewhere, MO 64444" ;
cout << endl << endl << setw (4) << "Qty" << setw (12) << "Item" << setw (19) << "Cost" ;
cout << endl << showpoint << setprecision(2) << setw (4) << nwpc << setw (22) << " NW-PC " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << nwpct ;
cout << endl << setprecision(1) << setw (4) << sdo << setw (22) << " Second Drive Option " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << sdot ;
cout << endl << setprecision(1) << setw (4) << camera << setw (22) << " Camera " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << camerat ;
cout << endl << setprecision(1) << setw (4) << software << setw (2) << " Software Bundle " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << softwaret ;
cout << endl << setw(37) << "-----------" ;
cout << endl << setw(26) << "Subtotal " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << subtotal;
cout << endl << setw(26) << "Sales Tax " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << stax;
cout << endl << setw(37) << "-----------" ;
cout << endl << setw(26) << "Total " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << total;
Show decimal point with NO zero..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//Output
cout << endl << setw (32) << "*************************" << endl;
cout << endl << setw (28) << "NewWave Computers"
<< endl << setw (25) << "123 Main St"
<< endl << setw (30) << "Somewhere, MO 64444" ;
cout << endl << endl << setw (4) << "Qty" << setw (12) << "Item" << setw (19) << "Cost" ;
cout << endl << showpoint << setprecision(1) << setw (4) << nwpc << setw (22) << " NW-PC " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << nwpct ;
cout << endl << setprecision(0) << setw (4) << sdo << setw (22) << " Second Drive Option " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << sdot ;
cout << endl << setprecision(0) << setw (4) << camera << setw (22) << " Camera " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << camerat ;
cout << endl << setprecision(0) << setw (4) << software << setw (2) << " Software Bundle " << "$ " << fixed << showpoint << setprecision(2) << setw(8) << softwaret ;
cout << endl << setw(37) << "-----------" ;
cout << endl << setw(26) << "Subtotal " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << subtotal;
cout << endl << setw(26) << "Sales Tax " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << stax;
cout << endl << setw(37) << "-----------" ;
cout << endl << setw(26) << "Total " << " $ " << fixed << showpoint << setprecision(2) << setw(8) << total;
Topic archived. No new replies allowed.