• 0

(C++) Printing data from text file to screen


Question

Hi, I've got this project I'm working on and one part of it should allow the user to place an order for a meal and print a bill to the screen when 2 zeros are entered. So far I've got the input, calculations and writing to the file done, but the second while loop where I should print the data from the file to the screen isn't terminating. And I'm pretty sure it's printing garbage as well.

The Inventory.dat is a random access file in binary mode, and the PrintBill.dat is (should be) a regular text file.

The variables meal.idNo, meal.UnitCost and meal.Name should be pulled from the Inventory.dat file.

Any help would be appreciated. Thanks.

void placeOrder ()
{
	item meal;
	int amount;
	double orderTotal; //total of each meal order - amount * unit cost
	double subTotal = 0.0; //sume of all orders excluding vat
	double grandTotal = 0.0; //grand total of meal order including the addition of vat
	double vat;

	displayAllMeals(); //calls function to display all the meals to the screen

	cout << "\n\n PLACE YOUR ORDER\n"
		 << " ________________\n";

	fstream outFile("Inventory.dat", ios::in);
	fstream billFile("PrintBill.dat", ios::trunc | ios::in | ios::out); 

	cout << "\n Please enter the quantity and meal ID (0 0 to end):  ";
	cin >> amount >> meal.idNo;

	while (!(amount == 0 && meal.idNo == 0))
	{
		//seek position of user-specified record
		outFile.seekp((meal.idNo - 100) * sizeof(item)); 

		orderTotal = amount * meal.unitCost;
		subTotal = subTotal + orderTotal;	

		billFile << amount << meal.name << meal.unitCost << orderTotal << "\n";

		cout << "\n Please enter the quantity and meal ID (0 0 to end):  ";
		cin >> amount >> meal.idNo;
	}  //end while

	vat = subTotal * 0.15;
	grandTotal = grandTotal + (subTotal + vat);

	system("cls"); //clear screen

	displayBillHeader(); //displays header for order bill

	while (!billFile.eof())
	{
		cout << "\t\t\t" << amount << "  x " << meal.name << " @ $" << meal.unitCost << " = $" << orderTotal << "\n";

	} //end while

	cout << "\n\t\t\t\t VAT \t $" << vat << "\n\n"
		 << "\t\t\t\t TOTAL \t $" << grandTotal << "\n\n\n"
		 << "\t\t\t Study harder and come back for more! \n\n";

	outFile.close(); //close Inventory.dat file
	billFile.close(); //close PrintBill.dat file

	cout << "\n\n\n Press ENTER to return to the main menu.";

	getch();

	system("cls"); //clear screen

} //end of placeOrder function

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.