Hi,
well my question is how to calcuate the total cost for a shop which is as follows

Item           Quantity      Price per unit            Total cost
1                  2                  20                           40
3                  1                  25.50                      25.50

Total 65.50

float total(int qty, int price);
{
	int t;
	t=qty*price;
	if ((qty<10.00)&&(price<100.00);
	return t;
	else 
		cout<<"Error"<<endl;

}

Is it like that???
really need help to calculate this plz help

Thx

Recommended Answers

All 2 Replies

first let me point out some problems in your existing code

//check that price is double/float i.e. 25.5 etc.
double total (int qty, double price)           
{
    double t;
    t = qty* price;
    return t;
}

what ever the total is you need to return the total whether its positive or negative.

But what I don't understand is how you'll read the line and process them? from file stream etc.
well your question requires a bit description, let me help you in what i understand.

S.# Quantity Price/Item Total.
========================
1 2 20 40
2 1 25.5 25.5
Grand Total = 65.5.

Solution.

for the stated problem what you need to do is to read the elements from any souce stream (filestream, or any other form).

if you are reading from file.
then your mile must be well formated like below
1 2 20
2 1 25.5

then what you need to do is to open the fstream and read in corresponding variable.
like below sample code

int main ()
{
   ifstream in ("C:/input.txt");
   int serialNumber;
   int qty;
   double price;
  
   if (!in.is_open ())
   {
              cout<<"File doesn't open"<<endl;
   } 
  
   double total = 0.0;                  // this hold the total of item.
   double grandTotal = 0.0;         // this variable hold the grand total.
   while ( !in.eof ())
  {
       in>>serialNumber;
       in>>qty;
       in>>price;

      total = qty*price;
      cout<<"Item # "<<serialNumber<<" total = " <<total<<"\n";
     grandTotal += total;
  }
    
  cout<<"Grand Total is  : "<<grandTotal<<endl;
  return 0;
}

hope the above idea helps

oups sorry its reading from a file.. anyway its just a part of the ans that ive posted here but i think i got to know how to do it now..

Thx a lot u really help

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.