Question
C++ I am having issues debugging parts of my program. Can someone help me fix the parts that aren't working correctly? These are the instructions:
C++ I am having issues debugging parts of my program. Can someone help me fix the parts that aren't working correctly?
These are the instructions:
Do not use arrays or tables, functions are not required. I am providing for you a file called Stock.txt that contains information regarding the 9 different stock items carried by FUNNY STUFF RETAIL INC. The file is a repeating sequence of three records such that the first record represents an item code number, the second record represents the description of each item and the third record contains the current selling price. Then the sequence repeats. Your program is to simulate the check out register allowing the check out clerk to input item codes to obtain the current selling prices and descriptions. If an incorrect code is entered, the clerk should be notified by an appropriate message on the screen. If the item is found, the clerk will enter the number of items with that description and price and continue to the next item. When all items have been entered the clerk terminates the transaction by entering an item code of 0 (zero). The program will build a sales slip called Sales.txt enumerating the item code, description, unit price, quantity purchased, and total extended price. The bill will be subtotaled and an 8% sales tax added for a final total. Amount tendered by the customer and change given will be finally added to the invoice. This invoice should be written out to an output file called Sales.txt. The final total and change should appear on the screen as well. Your output file should line up all columns and output formatted for dollars and cents on money amounts, using setprecision, showpoint, and fixed. Make sure you format your output to the screen as well. This assignment uses file processing found in chapter 5. Do not use arrays or tables.
My Professors pseudocode for our assignment (gave to us after starting our program so mine might not be correct to this):
ALGORITHM FOR LAB 2
open output file
found = no
total = 0
get item code from user
REPEAT if item code is not 0
open input file
found = no
REPEAT 9 times
read three values (number, price, description)
IF user's item code = number
display number, price, description to screen
ask for how many
calculate subtotal
add subtotal to total
write item, description, quantity and subtotal to output file
found = yes
ENDIF
ENDREPEAT
IF found is still no
write part not found message to screen
ENDIF
found = no
close input file
get next item code from user
ENDREPEAT
calculate and display tax
add tax to total
display total
ask for money tendered * please note exact total tendered may not
produce expected results
due to rounding errors
REPEAT while money tendered < total
display more money needed message
get more money
add money to money tendered
ENDREPEAT
calculate change
display change
write tax, total, money tendered, change to output file
close output file
My program:
#include
using namespace std;
int main(){
//Declare files
ifstream infile;
ofstream outfile;
//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;
//Format accordingly
cout << fixed << setprecision(2);
cout << showpoint;
outfile << fixed << setprecision(2);
outfile << showpoint;
//Open the Stock.txt file
infile.open("Stock.txt");
//show user item numbers
//ISSUE SHOWING LAST ENTRY TWICE
while (infile)
{
infile >> partNumber;
cout << partNumber << " ";
infile >> description;
cout << description << " ";
infile >> price;
cout << price << endl;
}
//closethe Stock.txt file
infile.close();
//Create a output text file named "Sales"
outfile.open("Sales.txt");
//Ask user for the item number
cout << " Please enter the item number of the product, type 0 to stop ";
cout << "inputting numbers " << "and print the receipt. ";
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 << "Price: $" << price << endl;
//Quantity, total cost 4 item, and Tax
cout << endl;
cout << "How many items of that product ";
cout << "would you like to buy? ";
cout << "Quantity: ";
//Input Quantity
cin >> quantity;
//Calculate total cost 4 the item
totalCost = price * quantity;
//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. ";
cout << "Item #: ";
cin >> itemNumber;
counter = 10;
infile.close();
infile.clear();
infile.open("Stock.txt");
} //End if for testing item number
//If it can't locate the item number...
//ISSIE FINDING SOME PART Numbers that exist....
if(itemNumber != partNumber)
{
cout << endl;
cout << "Sorry, no such item number exists, ";
cout << "please enter another one or ";
cout << "0 to stop. ";
cout << "Item #: ";
cin >> itemNumber;
counter = 10;
infile.close();
infile.clear();
infile.open("Stock.txt");
} //End if
totalForAll += totalCost;
taxForAll += tax;
} //End repeat for testing item number
//Close Stocks.txt
infile.close();
infile.clear();
} //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)
{
cout << "Change: $" << change << endl;
outfile << "Change: $" << change << endl;
}
//issue asking for more money/correct change until customer
//fully pays
if(change < 0)
{
change = change * -1; //Converts to positive
cout << "The customer owes $" << change << endl;
outfile << "The customer owes $" << change << endl;
}
cout << " ---------------------------------------------------";
outfile << " Thank you for shopping @ FUNNY STUFF RETAIL INC. ";
cout << " Thank you for shopping @ FUNNY STUFF RETAIL INC. ";
cout << " ---------------------------------------------------";
outfile.close();
return 0;
}
input file:
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started