5 Replies - 1454 Views - Last Post: 06 May 2008 - 11:09 PM Rate Topic: -----

#1 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

sales system

Posted 06 May 2007 - 06:12 PM

i have to use "piping". Below is teh quesiton and under tat is wat i hav done so far. i jus figured how to write things into a text file but now i dont kno how 2 use piping and allow user input

You are required to design a program invoice.cpp that will be used to calculate the total cost
for a list of purchased items. Each purchased item will be presented by its productId, the price
per item, and the corresponding quantity. We assume that such purchased items are recorded
in a plain text file purchase.dat in the form similar to the following sample
P010001 24.99 1
QX-35 19.99 2
DVD-player5 58.95 1
P010002 8.85 20
...
that is, the first column represents the product ID which will be alphanumeric without white
spaces, the second column represents the product price in Australian dollars, and the third
column represents the quantity of the corresponding purchased items. Your program
invoice.exe compiled from invoice.cpp will then be able to generate the invoice via
invoice.exe < purchase.dat > statement.txt

 #include <iostream>
#include <fstream>
#include <cstdlib>   // needed for exit()  
#include <string>
#include <iomanip>  // needed for formatting
using namespace std;

int main()
{
  string filename = "prices.dat";  // put the filename up front
  ofstream outFile;

  outFile.open(filename.c_str());

  if (outFile.fail())
  {
	cout << "The file was not successfully opened" << endl;
	exit(1);
  }

  // set the output file stream formats
  outFile << setiosflags(ios::fixed)
		<< setiosflags(ios::showpoint)
		<< setprecision(2);
  
  // send data to the file
  outFile << setw(3) << "PHANTOM COMPAY INVOICE" << "\n" << endl
		  << "Product ID	Quantity	Price ($)	Cost ($)" << endl
		  << "----------	--------	---------	--------" << endl
		  << setw(4) << "P010001" << 1 << 24.99 << 24.99 << endl
		  << setw(3) << "QX-35"  << 2 << 19.99 << 49.98 << endl
		  << setw(3) << "DVD-player5" << 1 << 58.95 << 58.95 << endl
		  << setw(3) << "P010002 " << 20 << 8.85 << 177.00 << endl;
   
  outFile.close();
  cout << "The file " << filename 
	   << " has been successfully written." << endl;
  system("pause");
  return 0;
}	  



Is This A Good Question/Topic? 0
  • +

Replies To: sales system

#2 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

Re: sales system

Posted 06 May 2007 - 06:25 PM

OK i figure out how to allow user input but ther a problem. e.g. wen i enter lik more than 8 characters it doesnt show teh rest afta 8 characters :S and it only allow only tat first input i..e enter product only but not the rest

help?

#include <iostream>
#include <fstream>
#include <cstdlib>   // needed for exit()  
#include <string>
#include <iomanip>  // needed for formatting
using namespace std;

int main()
{
  const int maxchars = 1000;
  char product [maxchars];
  double price, cost, totalcost;
  int quantity;
  char ch;
	
  cout<< "Enter name of product: ";  
  cin >> product;
  cout<< "Enter the quantity of the product: ";  
  cin >> quantity;
  cout<< "Enter the price of product: ";  
  cin >> price;
  cost = price * quantity;

  string filename = "prices.dat";  // put the filename up front
  ofstream outFile;

  outFile.open(filename.c_str());


  // set the output file stream formats
  outFile << setiosflags(ios::fixed)
		<< setiosflags(ios::showpoint)
		<< setprecision(2);
  
  // send data to the file
  outFile << setw(3) << "PHANTOM COMPAY INVOICE" << "\n" << endl
		  << "Product ID	Quantity	Price ($)	Cost ($)" << endl
		  << "----------	--------	---------	--------" << endl
		  << setw(4) << product << quantity << price << cost << endl
		  << setw(3) << "QX-35"  << 2 << 19.99 << 49.98 << endl
		  << setw(3) << "DVD-player5" << 1 << 58.95 << 58.95 << endl
		  << setw(3) << "P010002 " << 20 << 8.85 << 177.00 << endl;
   
  outFile.close();
  cout << "\nThe file " << filename 
	   << " has been successfully written." << endl;
  system("pause");
  return 0;
}	  
 

This post has been edited by noob2007: 06 May 2007 - 06:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 noob2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-April 07

Re: sales system

Posted 06 May 2007 - 07:16 PM

wait heres my UPDATED CODE

this code only allow enter once, but i want to allow multiple times

 
#include <iostream>
#include <fstream>
#include <cstdlib>   // needed for exit()
#include <string>
#include <iomanip>
using namespace std;

int main()
{
  string filename = "prices.dat";  // put the filename up front
  string product;
  ofstream outFile;
  

  double price, cost, totalcost;
  int quantity;
  char ch;
  
  
  outFile.open(filename.c_str());

  if (outFile.fail())  // check for successful open
  {
	cout << "\nThe file was not successfully opened"
	 << "\n Please check that the file currently exists."
	 << endl;
	exit(1);
  }
  
	cerr << "Enter name of product: ";  
	cin >> product;
  cerr << "Enter the quantity of the product: ";  
  cin >> quantity;
  cerr << "Enter the price of product: ";  
  cin >> price;
  
  cost = price * quantity;
  while (product != "")
  {
	  outFile << setw(3) << "PHANTOM COMPAY INVOICE" << "\n" << endl
		  << "Product ID	Quantity	Price ($)	Cost ($)" << endl
		  << "----------	--------	---------	--------" << endl
		  << setw(3) << product << "		 " << quantity <<  "		  " << price  << "		  " << cost << endl;

	getline(cin,product);
  }

  outFile.close(); 
  cout << "End of data input" << endl;
  cout << "The file has been written" << endl;

  cin.ignore();  // this line is optional
  
  
  
  
  
  
  cout << "dasd";
  
  
  
  system("pause");
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: sales system

Posted 06 May 2007 - 07:16 PM

Is the product name you are entering more than one word? If so, anything after the first whitespace will be lost - a limit of the cin >> operator. If this is the case, you may wish to use a cal to getline() to capture multiple word input.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: sales system

Posted 06 May 2007 - 07:30 PM

you would be totaly amazed at the problems that the little cin stream causes. I belive what your problem is that the cin leaves the '\n' in the buffer after reading the first number. An irritating little bug (well I suppose it is supposed to be a "feature") that cuases no end of trouble. There are quite a few workarrounds out there.
Was This Post Helpful? 0
  • +
  • -

#6 Akula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-May 08

Re: sales system

Posted 06 May 2008 - 11:09 PM

Hi,
Most of my questions were answer by the previous posts.....
But i am curious did you ever fix the loop? I've recreated something similar and don't quite understand why it doesn't loop.
Also Say i wanted to type in a a few products and get them all saved what would i need to add? at the moment all since the file keeps on getting remade the first product just keeps on changing... Or can somebody else help?.....
Thanks in advance for people even trying...
Akula

This post has been edited by Akula: 06 May 2008 - 11:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1