Would somebody please assist me? I have been consistently been doing trial and error with my counter. You see, when the program cannot locate an item (which is typed at the bottom) it displays "Sorry, no such item number exists, please enter another one or 0 to stop." Every time I try to locate the first few items it loops back into the same message, whereas when I type the last item number on the list, it can locate it... And if I type a 0 in when it can't locate the number the total adds up to a really high number.
I don't understand why this occurs, but if someone would like to help, I'd be happy.
*Please note that arrays or tables CANNOT be used.
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main()
{
//Declare files
ifstream infile;
ofstream outfile;
//Creat a output text file named "Sales"
outfile.open("Sales.txt");
//Declare variables that are entered from user
int itemNumber = 0;
int quantity = 0;
float paid = 0.0;
//Formula variables
int counter = 0;
float totalCost = 0.0;
float tax = 0.0;
float totalForAll = 0.0;
float taxForAll = 0.0;
float change = 0.0;
//Variables that are received from the text file "Stocks"
int partNumber = 0;
const int SIZE = 81;
char description[SIZE];
float price = 0.0;
//Ask user for the item number
cout << "Please enter the item number of the product, type 0 to stop ";
cout << "inputting numbers \n" << "and print the receipt.\n";
cout << "Item #: ";
cin >> itemNumber;
while (itemNumber != 0)
{
//Open the Stock.txt file
infile.open("Stock.txt");
//Use a loop to test each part number and see if it is found
for(counter = 9; itemNumber != 0; counter--)
{
//Get partNumber from the file
infile >> partNumber;
infile >> description;
infile >> price;
//If it finds a match to the part number
if(itemNumber == partNumber)
{
//Then write them to the output file "Sales"
outfile << "Item Number: ";
outfile << partNumber << endl;
outfile << "Description: ";
outfile << description << endl;
outfile << "Price of Item: $";
outfile << price << endl;
//Show user the description and price
cout << endl;
cout << description << endl;
cout << "Current Price: $" << price << endl;
//Quantity, total cost 4 item, and Tax
cout << endl;
cout << "How many items of that product ";
cout << "are there?\n";
cout << "Quantity: ";
//Input Quantity
cin >> quantity;
//Calculate total cost 4 the item
totalCost = price * quantity;
//Format accordingly
cout << fixed << setprecision(2);
cout << showpoint;
outfile << fixed << setprecision(2);
outfile << showpoint;
//Write the cost before tax to "Sales"
outfile << "Quantity: ";
outfile << quantity << endl;
outfile << "Total without Tax: $";
outfile << totalCost << endl;
//Tax
tax = (totalCost * .08);
//Write Tax
outfile << "Tax: $";
outfile << tax << endl;
//Add Tax to the total cost and then write
totalCost += tax;
outfile << "Total with Tax: $";
outfile << totalCost;
outfile << endl << endl << endl;
//Ask another item number, set counter = 10
cout << endl << endl;
cout << "Enter another item number, or ";
cout << "type 0 to finish.\n";
cout << "Item #: ";
cin >> itemNumber;
counter = 10;
infile.close();
infile.open("Stock.txt");
} //End if for testing item number
//If it can't locate the item number...
if(counter < 1)
{
cout << endl;
cout << "Sorry, no such item number exists, ";
cout << "please enter another one or ";
cout << "0 to stop.\n";
cout << "Item #: ";
cin >> itemNumber;
counter = 10;
infile.close();
infile.open("Stock.txt");
} //End if
totalForAll += totalCost;
taxForAll += tax;
} //End repeat for testing item number
//Close Stocks.txt
infile.close();
} //End the first repeat
cout << endl << endl << endl;
cout << "Total Tax: $" << taxForAll;
outfile << "Total Tax: $" << taxForAll;
cout << endl << endl;
cout << "Total Due: $" << totalForAll << endl;
outfile << endl << endl;
outfile << "Total Due: $" << totalForAll;
outfile << endl;
cout << endl;
cout << "Amount Paid: $";
cin >> paid;
//Write the amount paid
outfile << endl;
outfile << "Amount Paid: $";
outfile << paid << endl;
//Calculate change
change = paid - totalForAll;
if(change == 0.0 || change > 0.0)
{
cout << "Change: $" << change << endl;
outfile << "Change: $" << change << endl;
}
//Doesn't display when change is = 0, problem comparing floats?
if(change < -1.0)
{
change = change * -1; //Converts to positive
cout << "The customer owes $" << change << "..." << endl;
outfile << "The customer owes $" << change << "..." << endl;
}
outfile << "\nThank you for shopping @ FUNNY STUFF RETAIL INC.\n";
cout << "\nThank you for shopping @ FUNNY STUFF RETAIL INC.\n";
outfile.close();
system("pause");
return 0;
}
Stock.txt file contents
1234
Stockings
12.39
2865
Pajamas
17.99
4020
Dress
23.00
3640
Sweater
20.50
5109
Shorts
56.99
4930
TV
88.20
6600
ToothBrush
94.55
5020
AluminumPan
16.79
2336
Pencils
4.55