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();
}

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 (http://www.cplusplus.com/reference/iostream/manipulators/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.