Author Topic: Massive "junk" value outputs when using more than one setprecision/setw  (Read 289 times)

Offline window

  • Single posting newcomer
  • *
  • Posts: 3
While using exactly one set precision and inputting 3, 20, 20, and 20 in that order, I get these normal output values.



Code used in this image below

Code: [Select]
#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.



Code used in this second image below

Code: [Select]
#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.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6359
    • My Best Post
Re: Massive "junk" value outputs when using more than one setprecision/setw
« Reply #1 on: October 27, 2018, 04:35:03 pm »
Please read the rules! http://forums.codeblocks.org/index.php/topic,9996.0.html

Code::Blocks works best when you use a project! http://wiki.codeblocks.org/index.php/Creating_a_new_project

Learn the name and version of the Compiler you are using!
Learn how to post build log!
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline window

  • Single posting newcomer
  • *
  • Posts: 3
Re: Massive "junk" value outputs when using more than one setprecision/setw
« Reply #2 on: October 27, 2018, 05:33:16 pm »
Please read the rules! http://forums.codeblocks.org/index.php/topic,9996.0.html

Code::Blocks works best when you use a project! http://wiki.codeblocks.org/index.php/Creating_a_new_project

Learn the name and version of the Compiler you are using!
Learn how to post build log!
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

Tim S.
I'm not quite sure what you're getting at here, could you be more specific? I did read the rules and since the code works in Xcode, I assume it's not a C++ error. The name and version of the compiler is at the end of my post, I don't have an issue compiling, and for this program we may only use .cpp, no projects.

Offline sodev

  • Almost regular
  • **
  • Posts: 175
Re: Massive "junk" value outputs when using more than one setprecision/setw
« Reply #3 on: October 27, 2018, 08:04:09 pm »
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):

Code: [Select]
using namespace std;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.

Code: [Select]
   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.


Code: [Select]
        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:

Code: [Select]
    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.

Offline window

  • Single posting newcomer
  • *
  • Posts: 3
Re: Massive "junk" value outputs when using more than one setprecision/setw
« Reply #4 on: October 27, 2018, 08:46:20 pm »
This causes a compiler error because total is used uninitialized.
Thank you sodev, this resolved the issue. I don't completely understand what a compiler is then, but I guess I'll keep that in mind. And not that it really matters,  but using namespace std, endl, etc is just stuff our professor tells us to use for her course or she docks points. We just learned functions but she doesn't want us using that at all until the next program.