ok so im taking a class at school and we have a lab that i am stuck on.

"the program must read in an inventory list of items at a small retail store."

i have most of it but im stuck on getting a discount price and having it line up under the header.

here is my code:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

#define cls   system("cls")
#define pause system("pause")

const int CHARS_ON_LINE = 80;

void main()
{
	ifstream inFile;

	ofstream outFile;

	string inFileName,
		   outFileName,

		item1,		// description of item 1
		item2,		// description of item 2
		item3,		// description of item 3
		item4,		// description of item 4
		item5,		// description of item 5

		Inventory_List, // list of inventory

		invListTitle = "Inventory_List ",   
		itemhrd  = "Item: ",
		pricehrd = "Price: ",
		salepricehrd = "Sale Price: ";	

	double	salePrice1,	// sale price of item 1
			salePrice2,	// sale price of item 2
			salePrice3,	// sale price of item 3
			salePrice4,	// sale price of item 4
			salePrice5,	// sale price of item 5

			price1,		// price of item 1
			price2,		// price of item 2
			price3,		// price of item 3
			price4,		// price of item 4
			price5;		// price of item 5

	int	itemhrdlen = itemhrd.length(),
		pricehrdlen = pricehrd.length(),
		salepricehrdlen = salepricehrd.length(),

	// number of blanks to seperate headers
	numOfBlanks = (CHARS_ON_LINE - (itemhrdlen + pricehrdlen + salepricehrdlen)) / 4;

	double pertChgItem1,
		   pertChgItem2,
		   pertChgItem3,
		   pertChgItem4,
		   pertChgItem5;

	cout << "Enter the FQN of the input file: ";
	cin >> inFileName;
	inFile.open(inFileName.c_str()); 
	if(!inFile.is_open()) 
	{
		cerr << "cannot open input file: " << inFileName << "\n";
		pause;
		cls;
		exit(5);
	}
	cin.get();
	cout << "\nPress the enter key to continue the program.\n";
	cin.get();
	cls;

	cout << "Enter the FQN of the output file: ";
	cin >> outFileName;
	outFile.open(outFileName.c_str());
	if(!outFile.is_open())
	{
		cerr << "Cannot open output file: " << outFileName << "\n";
		pause;
		cls;
		exit (5);
	}
	cin.get();
	cout << "\nPress the enter key to continue the program.\n";
	cin.get();
	cls;

	inFile >> Inventory_List;

	// obtain info for item1
	inFile >> ws;
	getline(inFile, item1, '#');
	inFile >> price1;

	// obtain info for item2
	inFile >> ws;
	getline(inFile, item2, '#');
	inFile >> price2;

	// obtain info for item3
	inFile >> ws;
	getline(inFile, item3, '#');
	inFile >> price3;

	// obtain info for item4
	inFile >> ws;
	getline(inFile, item4, '#');
	inFile >> price4;

	// obtain info for item5
	inFile >> ws;
	getline(inFile, item5, '#');
	inFile >> price5;

	inFile.close();

	cout << "Enter the percent discount: %\n\n";
	cin >> pertChgItem1 >> pertChgItem2 >> pertChgItem3 >> pertChgItem4 >> pertChgItem5;

	// calculate the discount applied
	salePrice1 = price1 - pertChgItem1 * 100; 
	salePrice2 = price2 - pertChgItem2 * 100; 
	salePrice3 = price3 - pertChgItem3 * 100; 
	salePrice4 = price4 - pertChgItem4 * 100; 
	salePrice5 = price5 - pertChgItem5 * 100; 

	outFile << fixed << setprecision(2);

	outFile << invListTitle << Inventory_List << "\n\n"

		// set up header row
		<< itemhrd << setw(numOfBlanks) << ' '
		<< pricehrd << setw(numOfBlanks) << ' '
		<< salepricehrd << setw(numOfBlanks) << '\n'

		// output the data
		// item 1
		<< left 
		<< setw(itemhrdlen) 
		<< item1
				<< right << setw(numOfBlanks + pricehrdlen) 
				<< price1
				<< setw(numOfBlanks + salepricehrdlen) 
				<< salePrice1 
				<< '\n'

		// item 2
		<< left << setw(itemhrdlen) << item2
				<< right << setw(numOfBlanks + pricehrdlen) << price2
				<< setw(numOfBlanks + salepricehrdlen) << salePrice2 << '\n'

		// item 3
		<< left << setw(itemhrdlen) << item3
				<< right << setw(numOfBlanks + pricehrdlen) << price3
				<< setw(numOfBlanks + salepricehrdlen) << salePrice3 << '\n'

		// item 4
		<< left << setw(itemhrdlen) << item4
				<< right << setw(numOfBlanks + pricehrdlen) << price4
				<< setw(numOfBlanks + salepricehrdlen) << salePrice4 << '\n'

		// item 5
		<< left << setw(itemhrdlen) << item5
				<< right << setw(numOfBlanks + pricehrdlen) << price5
				<< setw(numOfBlanks + salepricehrdlen) << salePrice5 << '\n';

	outFile.close();
	}

My input file is this:

Grandma's Lye Soap
          			0.49
Bag Bahm
                        1.29
Chicken Soup
                       	0.29
Liniment
                       	2.35
Baking Soda
                       	0.63

And when i run the program i get this:

Inventory_List Grandma's

Item:              Price:              Sale Price:             
Lye Soap
          			0.49
Bag Bahm
                        1.29
Chicken Soup
                       	0.29
Liniment
                       	2.35
Baking Soda
                       	0.63
-92559631349317831000000000000000000000000000000000000000000000.00-92559631349317831000000000000000000000000000000000000000000000.00
      -92559631349317831000000000000000000000000000000000000000000000.00-92559631349317831000000000000000000000000000000000000000000000.00
      -92559631349317831000000000000000000000000000000000000000000000.00-92559631349317831000000000000000000000000000000000000000000000.00
      -92559631349317831000000000000000000000000000000000000000000000.00-92559631349317831000000000000000000000000000000000000000000000.00
      -92559631349317831000000000000000000000000000000000000000000000.00-92559631349317831000000000000000000000000000000000000000000000.00

The discount is 10% but my teacher is going to try others too so it can just work for that. any help would greatly be appreciated.


<< moderator edit: added [code][/code] tags >>

Recommended Answers

All 6 Replies

You're actually entering the % character, aren't you? If so, you'll need to use more complicated input handling, because the % character is not a valid character in a floating point representation; or else consider entering a fraction such as 0.1.

no we dont have to put in the % when we run it i dont think cause this class is just the basic's of C++ so i dont know much more complicated coding then that. teh teacher calls the programs we make "toy Programs".

What are you entering, then?

Entering % might make it do modulus...dunno

What are you entering, then?

just like 10 for 10 % i thought is how you would do it.

man this math is killin me; so easy but yet so hard.

You may wanna try .1 insted of 10 beacuse .1 would be the percent.

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.