Hello !

I have a script, that i m running on visual C++ compiler everything is alright with it, working properly but in the result, I can see on one digit after decimal, not 2, i wanna display two digit i.e 31.02 ;

Code is;

# include <iostream.h>
# include <string.h>
# include <math.h>
# include <conio.h>
# include <stdlib.h>

//using std::string;

int main()

{
      char Continue;
      do
      {
          system("cls");
          float hval,istval,isp,ival,gtot;
          int a=1;
          float isTotval=0.00;

          cout<<"How many sales items do you have? : ";
          cin>>hval;
          for (int i=0;i<hval;i++)
          {
              cout<<"Enter in the value of sale item "<<a<<" : $";
              cin>>ival;
              a=a+1;
              isTotval=isTotval+ival;
          } 
          cout <<"\n";
          cout <<"\n";
          cout <<"\n";  

          cout <<"Enter in the sales tax percentage <Enter 10 for 10%>: ";
          cin>>istval;
          isp=(istval*10/100)*hval;
          gtot = isTotval+isp;

          cout<<"\n";
          cout<<"\n";
          cout<<"\n"; 
          cout<<"********************************************************"<<endl;
          cout<<"*************** S A L E S R E C E I P T ****************"<<endl;
          cout<<"********************************************************"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"**"<<"  Total Sales             $     "<<isTotval<<"                  **"<<endl;
          cout<<"**"<<"  Sales tax               $     "<<isp<<"                 **"<<endl;
          cout<<"**"<<"                            ----------------        **"<<endl;
          cout<<"**"<<"  Grand Total             $     "<<gtot<<"                **"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"********************************************************"<<endl;
          cout<<"\n";
          cout<<"\n";
          cout <<"\n" << " Do you wants to run this program again? (Y/N):";

          cin  >> Continue;
      }
      while (Continue=='Y' || Continue=='y');      
}
#include <iomanip>
std::cout << std::setprecision(2);

I note that you are using C++ from before 1998, so you might have to change iomanip to iomanip.h or some other such pre-standard, ancient C++.

I was just learning it myself from internet and make up with this, and still learning, I want to 2 digits after in the result, after the decimal...

Try putting the include on line 6 of your original post, in the list of the other includes, and use the example syntax where ever desired to have the output be to 2 decimal point precision. the version posted will only work with the header file ionmanip (witout the .h). If your tutorial is out of date and still uses the .h syntax for header files, then the follow syntax will probably suffice.

cout << setprecision(2);

Note that this syntax could also be used with ionmanip (without the .h) as well, but you would have to change the namespace syntax a bit, which isn't considered the "best" approach.

Thank You for your time, but not working with me, kindly copy and try the whole coding with yurself too, and put the code u told me and see either it works or not...

Sorry, missed a bit. Try something like this:

# include <iostream>
# include <string>
# include <cmath>
# include <cstdlib>
#include <iomanip>

using namespace std;
int main()
{
      char Continue;
      do
      {

          std::cout.setf(std::ios::fixed);
          std::cout.precision(2);
          float hval,istval,isp,ival,gtot;
          int a=1;
          float isTotval=0.00;
          cout<<"How many sales items do you have? : ";
          cin>>hval;
          for (int i=0;i<hval;i++)
          {
              cout<<"Enter in the value of sale item "<<a<<" : $";
              cin>>ival;
              a=a+1;
              isTotval=isTotval+ival;
          } 
          cout <<"\n";
          cout <<"\n";
          cout <<"\n";  
          cout <<"Enter in the sales tax percentage <Enter 10 for 10%>: ";
          cin>>istval;
          isp=(istval*10/100)*hval;
          gtot = isTotval+isp;
          cout<<"\n";
          cout<<"\n";
          cout<<"\n"; 
          cout<<"********************************************************"<<endl;
          cout<<"*************** S A L E S R E C E I P T ****************"<<endl;
          cout<<"********************************************************"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"**"<<"  Total Sales             $     "<<  isTotval<<"                  **"<<endl;
          cout<<"**"<<"  Sales tax               $     "<< isp<<"                 **"<<endl;
          cout<<"**"<<"                            ----------------        **"<<endl;
          cout<<"**"<<"  Grand Total             $     "<< gtot<<"                **"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"**                                                    **"<<endl;
          cout<<"********************************************************"<<endl;
          cout<<"\n";
          cout<<"\n";
          cout <<"\n" << " Do you wants to run this program again? (Y/N):";
          cin  >> Continue;
      }
      while (Continue=='Y' || Continue=='y');      
}

Edited 3 Years Ago by Moschops

First, you need to use standard headers and only standard headers. The headers like iostream.h are pre-standard (before 1998), and should no longer be used, more the .h part (note: this is for standard headers only). The C-standard headers like math.h and stdlib.h must have the .h removed and a c must be added to the start. The conio.h header is an old and non-standard header dating back to the times of MS-DOS, few compilers still support it. So, the list of includes become:

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

Since the standard (1998), everything from the standard libraries are in the std namespace. So, things like cout are actually std::cout. To avoid having to put the std:: everywhere, you can just write this after the includes:

using namespace std;

Now, the code starts as follows:

int main() {

  char Continue;
  do
  {
    cout << "How many sales items do you have? : ";
    int hval;
    cin >> hval;

Notice that I have removed the list of variables that you were declaring at the start of the loop. Declaring variables en masse at the start of a function / loop is an archaic practice and is generally discouraged. Instead, declare variables close to where they are first used.

For instance, here I declare the isTotval just before I have the loop that computes the total of the sales items. Also, individual sales items are inputted in a local temporary variable ival, which avoids polluting the rest of the function scope with this temporary variable (it no longer exists when you leave that loop, so there is no chance of using it by mistake).

    float isTotval = 0.0;
    for (int i = 0; i < hval; i++)
    {
      cout << "Enter in the value of sale item " << (i+1) << " : $";
      float ival;
      cin >> ival;
      isTotval += ival;
    } 
    cout << "\n\n\n";

At this point, I have one new variable called isTotval with the total of the sales.

Now, the same goes for creating the istval where you first need it.

    cout << "Enter in the sales tax percentage <Enter 10 for 10%>: ";
    float istval;
    cin >> istval;

Did you make a mistake here? The following calculations seem a bit odd:

    float isp = (istval * 0.1) * hval;
    float gtot = isTotval + isp;

Now, to your question. To get the numbers to display with a fixed precision as a fixed number of digits after the decimal point, you need to tell set the decimal point notation to "fixed" (std::fixed) and you have to set the precision (beyond the fixed decimal point) to 2 using std::setprecision(2), as follows:

    cout << fixed << setprecision(2);
    cout << "\n\n\n";

Then, to display things nicely, you can use another formatting functionality called std::setw() which allows you to tell the stream to output the next element with a fixed overall width (it will fill the empty characters with spaces). For example, if I assume you won't have prices that will need more than 8 characters in total (including the decimal point), then I can set the width to 8 for each output of numbers and be guaranteed that each line will take the same amount of space and I can achieve a need alignment of the box:

    cout << "********************************************************" << endl;
    cout << "*************** S A L E S R E C E I P T ****************" << endl;
    cout << "********************************************************" << endl;
    cout << "**                                                    **" << endl;
    cout << "**                                                    **" << endl;
    cout << "**                                                    **" << endl;
    cout << "**" << "  Total Sales             $" << setw(8) << isTotval << "                 **" << endl;
    cout << "**" << "  Sales tax               $" << setw(8) << isp      << "                 **" << endl;
    cout << "**" << "                            ----------------        **" << endl;
    cout << "**" << "  Grand Total             $" << setw(8) << gtot     << "                 **" << endl;
    cout << "**                                                    **" << endl;
    cout << "**                                                    **" << endl;
    cout << "********************************************************" << endl;
    cout << "\n";
    cout << "\n";
    cout << "\n" << " Do you wants to run this program again? (Y/N):";

    cin >> Continue;
  }
  while (Continue=='Y' || Continue=='y');      
}

With the above, I get the following output:

How many sales items do you have? : 2
Enter in the value of sale item 1 : $343.43
Enter in the value of sale item 2 : $87.2



Enter in the sales tax percentage <Enter 10 for 10%>: 5



********************************************************
*************** S A L E S R E C E I P T ****************
********************************************************
**                                                    **
**                                                    **
**                                                    **
**  Total Sales             $  430.63                 **
**  Sales tax               $    1.00                 **
**                            ----------------        **
**  Grand Total             $  431.63                 **
**                                                    **
**                                                    **
********************************************************



 Do you wants to run this program again? (Y/N):n
Comments
very very helpful.. Thank You Mike :)

Thank You Everyone ! Your Work is really Appreciated, and Specially Mike..
Regards..
Stay Blessed

This question has already been answered. Start a new discussion instead.