3 Replies - 1912 Views - Last Post: 12 November 2012 - 11:33 AM Rate Topic: -----

#1 porralouca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 11

appending a file

Posted 11 November 2012 - 02:25 PM

I am trying to append 2 files. I have made a menu with an option to do this. My menu allows me to pick my option to add, and allows me to give the required information. The problem is when I look at these 2 files nothing has been appended to them.Here is my code.
/**********************
*File: Program5.cpp   *
*Author: Louis Palumbo*
*Date: 11-13-2012     *
***********************/
#include "program6.h"

int menu()
{
	int imenuOption;

	cout << "\t\t Bob's Sporting Goods \n\n"
		    "\t\t 1 - Stock Report \n\n"
			"\t\t 2 - Add Item \n\n"
			"\t\t 0 - Exit Program \n\n"
	        "\t\t Choose Menu Option 1, 2 or 0:  ";

	cin >> imenuOption;
	cout << endl << endl;

		switch(imenuOption)
		{
		case 1:
			report();
			break;

		case 2:
			addItem();
			break;

		case 0:
			exit(0); 
			break;

		default:
			cout << "\n Please Choose A Valid Option! \n";
			cout << "Press <Enter> To Try Again!: \n";
		PAUSE;
		break;
		}
		return imenuOption;
}

void report(void)
{
	int iInStock, iInCode, iCode;
	string sName, sCat;
	double Price;
	time_t sysTime;
	time(&sysTime);

	ifstream finInventory, finStock, finReport;
	ofstream foutReport;

	//open file
	finInventory.open("inventry.dat");
	finStock.open("instock.dat");
	foutReport.open("report.txt");

	//Tells you if inventry.dat does not open
	if (finInventory.fail())
	{
		cout << "inventry.dat did not open" << endl;
		cout << "The program is now closing" << endl;

		exit(0);
	}
	//Tels you if instock.dat does not open
	if (finStock.fail())
	{
		cout << "instock.dat did not open" << endl;
		cout << "The program is now closing" << endl;
	}
	//Tells you if report.txt does not open
	if (foutReport.fail())
	{
		cout << "report.txt did not open" << endl;
		cout << "The program s now closing" << endl;
		exit(0);
	}
	//write report heading
	

	foutReport << "Program 5, " << ctime(&sysTime);
	foutReport << "Louis Palumbo & Xavier\n";
	foutReport << "Bob's Sporting Goods Inventory Report                 \n";
	foutReport << "   Item Code |    Item       |  Number  |Total Price of    \n";
	foutReport << "    Number   |    Name       | In Stock |Number In Stock   \n";

	while(!finStock.eof())
	{
		finStock >> iInStock >> iInCode;
		finInventory >> sName >> iCode >> sCat >> Price;

	    //caling function to make report
	    fileout(iInStock, iCode, sName, Price, foutReport);
	}
	
	
        foutReport.close();
	finReport.open("report.txt");
	screenout(finReport);
}

	void fileout(int iStock, int iCode, string sName,
		         double Price, ofstream &fout)
	{
		//set decimal output to 2 places
		fout << fixed << setprecision(2);
		fout <<  iCode << "   |  " << setw(20) << sName << "  |    ";
		fout << setw(3)  << iStock << "   |    $ ";
		fout << setw(7)  << iStock * Price <<  endl;
	}

	void screenout(ifstream &fin)
	{
	
	
	string sLine;
	getline(fin, sLine, '\n');

	while(!fin.eof())
	{
		cout << sLine << endl;
		getline(fin, sLine, '\n');
	}
	PAUSE;
	}

	void addItem()



	{
		ofstream foutInventory, foutStock;
		foutInventory.open("inventry.dat",ios::app);
		foutStock.open("instock.dat", ios::app);

		

		string sName;
		string sCat;
		int iCode;
		double price;
		int iInStock;

		

		CLS;
		cout << "Enter Item Name: ";
		cin  >> sName;
		cout << endl << "Enter Item Code: ";
		cin  >> iCode;
		cout << endl << "Enter Item Category: ";
		cin  >> sCat;
		cout << endl << "Enter Price: ";
		cin  >> price;
		cout << endl << "Enter Number In Stock: ";
		cin  >> iInStock;

		foutStock << endl << iInStock << setw(7) << iCode;

		foutInventory << endl << left  << setw(10) << sName
			          << right << setw(6) << iCode << "  " << sCat
					  << setw(8) << price;

		foutInventory.close();
		foutStock.close();

	}




Here is one of the files

BASKETBALL 4935 SG 24.95
BIKE 4505 SG 299.99
BLENDER 4240 HW 223.95
CAMERA 9128 SW 26.95
CANVAS 5626 SG 26.95
CARB 9296 AP 31.95
CORN_POPPER 6822 HW 29.95
COVER 6115 HW 25.95
CROWBAR 1282 HW 27.95
DARTBOARD 7998 SG 12.95
FUEL_GAUGE 3813 SG 151.99
GAS_GRILL 3000 AP 149.99
GRIDDLE 3752 HW 39.99
INJECTOR 3116 HW 150.99
IRON 5634 HW 24.95
JACK 1528 HW 13.95
MICROWAVE 6044 HW 30.95
NET 5791 AP 15.95
PRESS 9547 HW 25.95
REFRIG 4111 AP 27.95
STRUTT 3804 AP 152.99
TIRE 2674 AP 32.95
TREADMILL 7348 SG 349.95
VOLLEYBALL 8066 SG 14.95
WASHER 9145 AP 399.99


Here is what the other looks like

39 4935
47 4505
69 4240
97 9128
181 5626
236 9296
244 6822
265 6115
305 1282
310 7998
393 3813
395 3000
452 3752
474 3116
493 5634
536 1528
541 6044
605 5791
635 9547
745 4111
748 3804
776 2674
780 7348
846 8066
987 9145

This post has been edited by macosxnerd101: 11 November 2012 - 02:55 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: appending a file

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,777
  • Joined: 27-December 08

Re: appending a file

Posted 11 November 2012 - 02:56 PM

Moved to C/C++. Please don't post programming help questions in the Student Campus forum.
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: appending a file

Posted 11 November 2012 - 08:49 PM

use ofstream file("file.txt", ios::out | ios::app)
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: appending a file

Posted 12 November 2012 - 11:33 AM

View PostIngeniousHax, on 12 November 2012 - 07:19 AM, said:

use ofstream file("file.txt", ios::out | ios::app)

While there is nothing wrong with that, mentioning ios::out explicitly while working with an ofstream is not necessary. ofstream already has this set as default.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1