Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Can someone help me get my program to work correctly? I'm having an issue with it displaying the file (last entry twice), finding certain

C++ Can someone help me get my program to work correctly? I'm having an issue with it displaying the file (last entry twice), finding certain item numbers, and the change when you give too little of the amount when paying.

------------------------------------------------

this is the input file named stock.txt:

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

----------------------------------------------

this is my code:

#include

#include

#include

#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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

Students also viewed these Databases questions

Question

=+11.3. Show that Theorem 11.4(ii) can fail if u(B) =00.

Answered: 1 week ago