Adding Up Prices From An Input File

(problems with my "while" loop)

Page 1 of 1

6 Replies - 775 Views - Last Post: 20 September 2009 - 05:34 PM Rate Topic: -----

#1 Munkey106  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-September 09

Adding Up Prices From An Input File

Post icon  Posted 18 September 2009 - 11:13 PM

I need to read data from an input file ("inventory.data") and add up the prices to get a total price ("totalPrice"). I am having trouble adding up the prices correctly.


"inventory.data"
90312 Stool 12.34 100 25 
20705 Table 54.78 24 25
10101 Chair 123.45 183 4
90412 Files 22.34 60 2 
20205 Light 54.78 25 22
10501 Desk 153.98 123 2
40412 Alcohol 22.34 7 2 
30205 Binders 23.94 34 21
20502 Folders 38.21 143 68


(The price is after the item description)





Here is the snippet of code I am having problems with...
//Need to figure out how to  add up all the prices.
void totalValue()
{
	string partNumber;
	int QOH, MRQ;
	string partDescription;
	float unitPrice, totalPrice, tempPrice, tempPrice2;
	
	fstream inFile;
	inFile.open("inventory.data");
		if (inFile.fail())
			{
				cout << "Error opening input file - Program terminated!\n";
				exit(1);
			}
	
	while (!inFile.eof())
	{
		inFile >> partNumber >> partDescription >> unitPrice >> QOH >> MRQ;
		
		tempPrice = unitPrice;
		tempPrice2 = tempPrice + tempPrice;
	}	
	
	totalPrice = tempPrice2;
	cout << "Total Price: $" << totalPrice << endl;
}


I am fairly certain my problem lies within my "while" loop. If someone would be willing to help me out, I would greatly appreciate it!



David

Is This A Good Question/Topic? 0
  • +

Replies To: Adding Up Prices From An Input File

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Adding Up Prices From An Input File

Posted 18 September 2009 - 11:17 PM

	fstream inFile;
 

It needs to be ifstream or oftream when declaring an object;
Was This Post Helpful? 0
  • +
  • -

#3 Munkey106  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-September 09

Re: Adding Up Prices From An Input File

Posted 18 September 2009 - 11:34 PM

I appreciate the help, seeP, and I changed my code [in reference to what you posted], though that still does not answer my question about "totalPrice".
Was This Post Helpful? 0
  • +
  • -

#4 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Adding Up Prices From An Input File

Posted 18 September 2009 - 11:45 PM

 {
	string partNumber;
	int QOH, MRQ;
	string partDescription;
	float unitPrice, totalPrice, tempPrice, tempPrice2;
   
	fstream inFile;
	inFile.open("inventory.data");
		if (inFile.fail())
			{
				cout << "Error opening input file - Program terminated!\n";
				exit(1);
			}
   
	while (!inFile.eof())
	{
		inFile >> partNumber >> partDescription >> unitPrice >> QOH >> MRQ;



When getting a string(partNumber, partDescription) from a file it should be like this:
getline(inFile, string variable to hold from file, 'delimiter if needed');

'inFile >>' is used for char and number types.

This post has been edited by seeP+: 18 September 2009 - 11:46 PM

Was This Post Helpful? 0
  • +
  • -

#5 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Adding Up Prices From An Input File

Posted 19 September 2009 - 06:43 AM

Try
tempPrice+=unitprice;

and get rid of the temp2Price line only the one above then display tempPrice after the while is over
Was This Post Helpful? 1
  • +
  • -

#6 Munkey106  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-September 09

Re: Adding Up Prices From An Input File

Posted 20 September 2009 - 01:50 PM

View Postponcho4all, on 19 Sep, 2009 - 05:43 AM, said:

Try
tempPrice+=unitprice;

and get rid of the temp2Price line only the one above then display tempPrice after the while is over


This was exactly my problem! I appreciate the assistance, man. I made you sure "thank" you for you help.


If you have a second, could you (or anyone else) explain what the "+=" does so I can understand my code better? I'm not familiar with those operators being used together.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Adding Up Prices From An Input File

Posted 20 September 2009 - 05:34 PM

+= is shorthand for tempPrice = tempPrice + unitprice
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1