Sory for asking too much about this..But I really need to solve this problem immendiately..
This is just simple,but i dont know why i cant finf whats wrong with my code..
I'm trying to display the price,sale and the to total sale in point form..
like this :

TOTAL SALE : $20.00

but now i can only display it in single number without the point..
i already declared it as double but i dont know why its not working..
This is the only thing left for my program to work successfully..
Below is my code...
Thank you for helping :)really need your help.....

#include <iostream>
#include <iomanip>
using namespace std;

int main () {

struct record{
   string id;
   string name;
   double price;
   double quantity;
   double sale;
   };

  record product[100];


      int i=0;
      char answer;

      cout<<"--------------------------------------------------------------------------------";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"--------------------------------------------------------------------------------";
      cout<<endl;

      do{

      cout << "Enter the Product ID : ";
      cin >> product[i].id;


      cout << "Enter the Product Name : ";
      cin >> product[i].name;


      cout << "Enter the Price For Single Item : ";
      cin >> product[i].price;

      cout << "Enter The Quantity : ";
      cin >> product[i].quantity;

      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;


      i++;

      cout<<endl;

      }while(answer == 'y' || answer == 'Y');


int index;

cout<<"--------------------------------------------------------------------------------";
cout<<"ID |"<<setw(13)<<"ITEM |"<<setw(12)<<"PRICE |"<<setw(10)<<"QTY |"<<setw(10)<<"SALE"<<endl;
cout<<"--------------------------------------------------------------------------------";


 for(index=0; index<i; index++){

product[index].sale=(product[index].price)*(product[index].quantity);

// Display all the info about that product
cout <<product[index].id<<setw(10)<<product[index].name<<setw(10)<<"$"<<product[index].price<<setw(10)
<<product[index].quantity<<setw(10)<<product[index].sale;

cout<<endl;
}

cout<<endl;

int total = 0;
for(int counter=0; counter < i; counter++)
{
total += product[counter].sale;
}

cout<<"TOTAL SALE IS:"<<total;
cout<<endl;

double payment;

cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;

while (payment<total){
cout<<"The Amount Of Payment Is Unsufficient !";
cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
}

cout<<endl;
cout<<"TOTAL SALE      :$"<<total<<endl;
cout<<"RECEIVE PAYMENT :$"<<payment<<endl;
cout<<"BALANCE         :$"<<payment-total<<endl;

cin.get();
}

Dani AI

Generated

Brief diagnosis and practical fix

The reason the printed total shows no decimal point is twofold: the running total is stored in an integer type (so fractional cents are dropped), and iostream's default formatting does not force trailing zeros. correctly pointed out the integer issue, and 's suggestion to control precision is the right direction. A reliable approach is to (1) accumulate into a floating type and (2) format only the monetary outputs to two decimal places, restoring the stream state afterward so other columns (like integer quantities) remain unaffected.

Example pattern (replace the accumulator and print where appropriate):

// accumulate with a floating accumulator
double grandTotal = 0.0;
for (int idx = 0; idx < i; ++idx) {
    product[idx].sale = product[idx].price * product[idx].quantity;
    grandTotal += product[idx].sale;
}

// save, set money-format, print, then restore
std::streamsize savedPrec = std::cout.precision();
std::ios_base::fmtflags savedFlags = std::cout.flags();
std::cout.setf(std::ios::fixed, std::ios::floatfield);
std::cout.precision(2);

std::cout << "TOTAL SALE      : $" << grandTotal << '\n';

// restore previous formatting
std::cout.flags(savedFlags);
std::cout.precision(savedPrec);

Additional tips

  • If quantity is always a count, prefer an integer type (int) for clarity; use double only for fractional units.
  • setw applies to the next insertion only. For consistent column alignment when printing a currency sign plus formatted number, format the money into a string first (via std::ostringstream) and then apply setw to that string.
  • Mixing operator>> and getline for names can drop input; consume the leftover newline (for example with cin.ignore()) before calling getline if full product names with spaces are needed.

Summary

Change the accumulator to a floating type and format monetary outputs to two decimals while restoring stream flags. That will produce lines like "TOTAL SALE : $20.00" without affecting other columns.

Recommended Answers

All 4 Replies

cout << setiosflags(ios::fixed) << setprecision(2); Sets the display to fixed precision and then fixes the number of digits after the decimal to 2. Any calls to cout after that will have those properties.
Use resetiosflags ( ) to restore the default settings (it might not reset the 2 digit portion, I'm not sure)

P.S. you need to include <iomanip> for the above

I can send messages on lan on local but I cann't send messages on internet on global. please help me. do you have a source of sending messages on global on internet?

Actually you declared total as in integer and you're using it to store the total price. So, it's not printing any decimals because integers don't have any decimal points in them.

Also, 1 more thing. When you print a double like 2.00 you'll get the output 2. So what you want to do is:

cout << fixed << setprecision(2);

This set's the precision of output to 2 decimal points and will print 2.00. A word or warning though, this will also print your integers (quantity etc) with 2 decimal points. So, you'll have to switch the precision between your integers and your doubles to get the output that you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.