While using exactly one set precision and inputting 3, 20, 20, and 20 in that order, I get these normal output values.
(https://i.postimg.cc/yYGqnKDf/Codeblocks-board.png)
Code used in this image below
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main ( )
{
const double DISCOUNT = 0.2,
FREE_SHIPPING_MIN = 150,
SHIPPING = 8.5,
TAX_RATE = 0.0825;
double total,
price,
discounted_total,
amount_owed;
int items_purchased;
ofstream fout;
fout.open( "prog4_002out.txt" );
if ( !fout )
{
cout << endl << endl
<< " ***Program Terminated.*** " << endl
<< "Output file failed to open." << endl;
system( "PAUSE>NUL" );
return 2;
}
cout << "Enter the number of items purchased: ";
cin >> items_purchased;
cout << endl;
if ( items_purchased == 0 )
cout << "Come back again soon!";
else
{
for ( int i = 0 ; i < items_purchased ; ++i )
{
cout << "Enter the item price: ";
cin >> price;
total = total + price;
}
discounted_total = total - total * DISCOUNT;
if ( discounted_total >= FREE_SHIPPING_MIN )
amount_owed = discounted_total + discounted_total * TAX_RATE;
else
amount_owed = discounted_total + SHIPPING
+ discounted_total * TAX_RATE;
fout << "Author's Name:" << endl
<< "10/31/18" << endl << endl
<< "Total Purchases: $" << setw(6) << fixed << setprecision( 2 ) << total
<< endl
<< "Discounted Total: " << setw(6) << discounted_total << endl
<< "Tax Rate: " << setw(8) << fixed << setprecision (4) << TAX_RATE << endl
<< "Tax: " << setw(6) << fixed << setprecision (2) << TAX_RATE * discounted_total << endl
<< "Shipping: " << setw(6) << SHIPPING << endl << endl
<< "Total Amount Due: $" << setw(6) << amount_owed << endl;
cout << endl << endl << "Author's Name:"
<< endl
<< "10/31/18" << endl << endl
<< "Total Purchases: $" << setw(6) << fixed << setprecision( 2 ) << total
<< endl
<< "Discounted Total: " << setw(6) << discounted_total << endl
<< "Tax Rate: " << setw(8) << fixed << setprecision( 4 ) << TAX_RATE << endl
<< "Tax: " << setw(6) << fixed << setprecision( 2 ) << TAX_RATE * discounted_total << endl
<< "Shipping: " << setw(6) << SHIPPING << endl << endl
<< "Total Amount Due: $" << setw(6) << amount_owed << endl << endl
<< "A copy for your records can be found in prog4_002out.txt."
<< endl;
}
fout.close ( );
system( "PAUSE>NUL" );
return 0;
}
However, when adding anymore than one setprecision, or adding even one setw, I get massive junk output values like this. Same input values as above, same code aside from the additional setprecisions/setw.
(https://i.postimg.cc/pV11MZPr/Codeblocks-board-2.png)
Code used in this second image below
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main ( )
{
const double DISCOUNT = 0.2,
FREE_SHIPPING_MIN = 150,
SHIPPING = 8.5,
TAX_RATE = 0.0825;
double total,
price,
discounted_total,
amount_owed;
int items_purchased;
ofstream fout;
fout.open( "prog4_002out.txt" );
if ( !fout )
{
cout << endl << endl
<< " ***Program Terminated.*** " << endl
<< "Output file failed to open." << endl;
system( "PAUSE>NUL" );
return 2;
}
cout << "Enter the number of items purchased: ";
cin >> items_purchased;
cout << endl;
if ( items_purchased == 0 )
cout << "Come back again soon!";
else
{
for ( int i = 0 ; i < items_purchased ; ++i )
{
cout << "Enter the item price: ";
cin >> price;
total = total + price;
}
discounted_total = total - total * DISCOUNT;
if ( discounted_total >= FREE_SHIPPING_MIN )
amount_owed = discounted_total + discounted_total * TAX_RATE;
else
amount_owed = discounted_total + SHIPPING
+ discounted_total * TAX_RATE;
fout << "Author's Name:" << endl
<< "10/31/18" << endl << endl
<< "Total Purchases: $" << setw(6) << fixed << setprecision( 2 ) << total
<< endl
<< "Discounted Total: " << setw(6) << discounted_total << endl
<< "Tax Rate: " << setw(8) << fixed << setprecision (4) << TAX_RATE << endl
<< "Tax: " << setw(6) << fixed << setprecision (2) << TAX_RATE * discounted_total << endl
<< "Shipping: " << setw(6) << SHIPPING << endl << endl
<< "Total Amount Due: $" << setw(6) << amount_owed << endl;
cout << endl << endl << "Author's Name:"
<< endl
<< "10/31/18" << endl << endl
<< "Total Purchases: $" << setw(6) << fixed << setprecision( 2 ) << total
<< endl
<< "Discounted Total: " << setw(6) << discounted_total << endl
<< "Tax Rate: " << setw(8) << fixed << setprecision( 4 ) << TAX_RATE << endl
<< "Tax: " << setw(6) << fixed << setprecision( 2 ) << TAX_RATE * discounted_total << endl
<< "Shipping: " << setw(6) << SHIPPING << endl << endl
<< "Total Amount Due: $" << setw(6) << amount_owed << endl << endl
<< "A copy for your records can be found in prog4_002out.txt."
<< endl;
}
fout.close ( );
system( "PAUSE>NUL" );
return 0;
}
The only changes are the additional setprecision and setw in the fout/cout blocks. My partner (this is a school project as you might have guessed) said he isn't having this issue with the same code on his computer, but he's also on a Mac using Xcode. Is this an issue with the compiler? With some setting I have? etc? I'm not sure how to troubleshoot this.
I'm using Windows 7 and the latest version of Codeblocks (17.12). I tried rebooting my PC as well as uninstalling and reinstalling Codeblocks, and neither issue fixed the problem obviously. When I uninstalled and reinstalled, my settings carried over from the last install though, so I'm wondering if that's the issue.
This is not a "How to do my Homework" forum, this forum is for CodeBlocks related questions and not for "Programming-Noobs" questions. And your question totally falls into the second category, but apparently you lack enough understanding to realize that, so let me explain some things.
CodeBlocks IS NOT a compiler. This is the most important thing to remember. CodeBlocks is an IDE, basically it is a nice text editor that can USE a compiler. A very important aspect of CodeBlocks is that it uses a Workspace with Projects to operate, without using a Project weird things can happen, you should tell that the person who said to use CodeBlocks without a Project. A Project is not a magical thing that somehow interfers with your program, its just how CodeBlocks works.
Since you are not using a Project CodeBlocks is using more or less a random compiler it found (i don't know the details), because this is a homework project, you probably use CodeBlocks with the bundled GCC compiler. So this is probably your compiler, GCC, version unknown. I don't know what Xcode uses, GCC or clang, but i used MSVC 14.1 with your program, details follow.
Because we are totally in the off-topic area already, lets take a look at your program (btw. both code versions are the same):
This is where i usually stop reading, homework already failed. Namespace std contains very generic and common names, the name is so short, it is not justified to pull all this into global scope.
if ( items_purchased == 0 )
cout << "Come back again soon!";
else
items_purchased is signed, it can also be negative, you don't handle that case properly.
fout << "Author's Name:" << endl
<< "10/31/18" << endl << endl
<< "Total Purchases: $" << setw(6) << fixed << setprecision( 2 ) << total
<< endl
<< "Discounted Total: " << setw(6) << discounted_total << endl
<< "Tax Rate: " << setw(8) << fixed << setprecision (4) << TAX_RATE << endl
<< "Tax: " << setw(6) << fixed << setprecision (2) << TAX_RATE * discounted_total << endl
<< "Shipping: " << setw(6) << SHIPPING << endl << endl
<< "Total Amount Due: $" << setw(6) << amount_owed << endl;
cout << endl << endl << "Author's Name:"
<< endl
<< "10/31/18" << endl << endl
<< "Total Purchases: $" << setw(6) << fixed << setprecision( 2 ) << total
<< endl
<< "Discounted Total: " << setw(6) << discounted_total << endl
<< "Tax Rate: " << setw(8) << fixed << setprecision( 4 ) << TAX_RATE << endl
<< "Tax: " << setw(6) << fixed << setprecision( 2 ) << TAX_RATE * discounted_total << endl
<< "Shipping: " << setw(6) << SHIPPING << endl << endl
<< "Total Amount Due: $" << setw(6) << amount_owed << endl << endl
<< "A copy for your records can be found in prog4_002out.txt."
<< endl;
Do not use std::endl all over the place, this unnecessarily flushes the stream all the time, use a plain \n and flush only at the last operation. This is also totally copy-paste madness, both outputs are the same, just the console output adds an extra line. Extract this into a function that gets a std::ostream as parameter and output the extra line for the console outside of that function.
TL;DR
Your code does not compile in MSVC 14.1. The problem is this:
double total,
price,
discounted_total,
amount_owed;
...
for ( int i = 0 ; i < items_purchased ; ++i )
{
cout << "Enter the item price: ";
cin >> price;
total = total + price;
}
This causes a compiler error because total is used uninitialized (looks like MSVC uses stricter rules now, im pretty sure previous versions only emit a warning). Apparently it doesn't cause a compiler error with your compiler but only a warning. This pushes you in the area of undefined behavior. The compiler is free to do total random stuff of its liking. If you are lucky and compile in debug mode the compiler initializes the variables with 0 for you and it works correctly. Or it puts in random crap and you get random results.
Your code is broken, not your compiler.