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

Resetting your counter to 10 at the bottom of the counter will cause it to run forever if you can't find the item, I'm not sure what you're trying to do there.

I would copy/paste all of your code into a separate file and strip out all of the output code in that loop until you get the input working the way you want it to, then add it back in.

Resetting your counter to 10 at the bottom of the counter will cause it to run forever if you can't find the item, I'm not sure what you're trying to do there.

I would copy/paste all of your code into a separate file and strip out all of the output code in that loop until you get the input working the way you want it to, then add it back in.

The reason I set counter = 10 at the bottom of each IF statement is to make it re-initialize itself so it can proceed through the loop again to test if there is an item number equal to the one entered. By the way, the sentinel value is 0 to stop inputting numbers and break out of the loops. I modified the code sort of, still getting the same results...

#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; counter != 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
                      
                      
                      totalForAll += totalCost;
                      taxForAll += tax;
                      
          } //End repeat for testing item number
          
          //If counter has done all iterations and itemNumber is not equal to sentinel
          if(counter == 0 && itemNumber != 0) 
                      {
                                 cout << endl;
                                 cout << "Sorry, no such item number exists, ";
                                 cout << "please enter another one or ";
                                 cout << "0 to stop.\n";
                                 cout << "Item #: ";
                                 
                                 cin  >> itemNumber;
                      } //End if
          
          //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;
}

Just throwing something out here. Nine items in file, counter set to 10, the eof bit in the stream never being reset or checked, the file continuously being closed and reopened, and this:

whereas when I type the last item number on the list, it can locate it

I'm thinking a failed stream that never gets "good" again. To the OP, stick some debugging statements in there and make sure that what you THINK is being read into the variable actually IS being read into the variable.

Also, check the state of the stream before you reopen the file with the good() function.

http://www.cplusplus.com/reference/iostream/ios/good/

If it's not good, clear it, then reopen it.

http://www.cplusplus.com/reference/iostream/ios/clear/

I'm not guaranteeing this is the problem, but it's worth checking. Closing and then reopening a file does NOT reset a failed stream. You have to do it manually.

Resetting your counter to 10 at the bottom of the counter will cause it to run forever if you can't find the item, I'm not sure what you're trying to do there.

I would copy/paste all of your code into a separate file and strip out all of the output code in that loop until you get the input working the way you want it to, then add it back in.

Okay, so here is my pseudocode for my program. I've desk-checked it and everything, and it should work properly... I placed the text using the code placement so the indention would line up properly, hopefully that's not too big of a deal. Anyway, Here it is:

INPUTS:

Item Code
Quantity
Amount Paid


OUTPUTS:

Current Selling Price
Description

Error Message

Final Total
Change


FORMULAS:

Change = Amount Paid - Final Total
Purchase Price = Current Selling Price * Quantity
Total Extended Price = Total Extended Price + Purchase Price


ALGORITHM:

INPUT Item Code
OPEN File
REPEAT for each item obtained from the File and that Item Number is not 0
	ASSIGN Item Number *to variable
	ASSIGN Description *to variable
	ASSIGN Current Selling Price *to variable

	IF the Item Code and Item Number are EQUAL
		
		OUTPUT Description		
		OUTPUT Current Selling Price
		
		GET Quantity
		WRITE Quantity

		Purchase Price = Current Selling Price * Quantity
		WRITE Purchase Price

		Total Extended Price = Total Extended Price + Purchase Price
		WRITE Total Extended Price

		Final Total = ((Total Extended Price * .08) + Total Extended Price) 
                               + Final Total
		
		INPUT another Item Code
		
		CLOSE File
		OPEN File
	END-IF

	IF the Item Code cannot be located after it has reached the end of the list
		
		CLOSE File
		OUTPUT Error Message
		
		INPUT Item Code
		OPEN File
	END-IF
END-REPEAT
CLOSE FILE

OUTPUT Final Total
WRITE Final Total

INPUT Amount Paid
Change = Amount Paid - Final Total

OUTPUT Change
WRITE Change

Resetting your counter to 10 at the bottom of the counter will cause it to run forever if you can't find the item, I'm not sure what you're trying to do there.

I would copy/paste all of your code into a separate file and strip out all of the output code in that loop until you get the input working the way you want it to, then add it back in.

Here's the code stripped from all the output except the necessities... Desk-checked it and it came out perfect, just still get the same error when it is executed... UGH!

#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");
    
    //INPUTS
    int itemCode      = 0;
    int quantity      = 0;
    double amountPaid = 0.0;
    
    //OUTPUTS
    int itemNumber      = 0;
    double currentPrice = 0.0;
    string description  = "";
    double finalTotal   = 0.0;
    double change       = 0.0;
    
    //FORMULAS
    int counter               = 9;
    double purchasePrice      = 0.0;
    double totalExtendedPrice = 0.0;
    
    //ALGORITHM
    cin >> itemCode;
    infile.open("Stock.txt");
    while(itemCode != 0)
    {
                  infile >> itemNumber;
                  infile >> description;
                  infile >> currentPrice;
                  
                  if(itemCode == itemNumber)
                  {
                              cout << description;
                              cout << currentPrice;
                              
                              cin  >> quantity;
                              outfile << quantity;
                              
                              purchasePrice = currentPrice * quantity;
                              outfile << purchasePrice;
                              
                              totalExtendedPrice += purchasePrice;
                              outfile << totalExtendedPrice;
                              
                              finalTotal = ((totalExtendedPrice * .08) + totalExtendedPrice) + finalTotal;
                              
                              cin >> itemCode;
                              infile.close();
                              infile.open("Stock.txt");
                              
                              counter = 9;
                  } //End if
                  
                  if(counter == 0)
                  {
                             infile.close();
                             cout << "No Such Thing...";
                             
                             cin >> itemCode;
                             infile.open("Stock.txt");
                             
                             counter = 9;
                  } //End if
                  
                  counter--;
                  
    } //End repeat
    infile.close();
    
    cout << finalTotal;
    outfile << finalTotal;
    
    cin >> amountPaid;
    change = amountPaid - finalTotal;
    
    cout << change;
    outfile << change;
    
    system("pause");
    return 0;
}

I don't get any kind of error

2336
Pencils4.5510
1234
Stockings12.3923
0
406.048
0
-406.048Press any key to continue . . .

Just some issues with not having newlines at certain places.

Your counter is going unused in this version, which is probably okay. You can probably check to see if infile is null (if so, you've run out of file) rather than counting. Check into what Vernon was saying, too. My thought is that by reopening the file you're resetting the stream, but test that out (using an infile.clear() to reset it).

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream a;
    int b;
    for(int i = 0; i< 2; i++)
    {
        //a.clear();
        a.open("a.txt");
        for(int j = 0; j < 4; j++)
        {
            a >> b;
            cout << b << endl;
            // b = 5;
        }
        a.close();
    }

    return 0;	
}

a.txt contains... 1 followed by a space followed by 2. The program above points out a few things that happen if you try to read past the end of the file. Results of above.

1
2
2
2
2
2
2
2

It appears that we keep reading 2 over and over again, but we're not. Note that the file does not reset itself after the re-opening. Uncomment line 17.

1
2
5
5
5
5
5
5

Nothing is being read. It just appeared that 2 was being read over and over again because it never changed. Uncomment line 12.

1
2
5
5
1
2
5
5

File was read from the beginning, unlike last time. "close" and "open" don't do that. You have to do it explicitly.

I don't know whether the OP ever reads past the end of the file, but if he does, without any flag clearing, there's a decent chance that it stays failed.

Yes, you're totally correct as usual, Vernon. Good example. For whatever reason I was thinking he was redeclaring the ifstream object rather than just opening and closing the same one. Sorry.

Just some issues with not having newlines at certain places.

Your counter is going unused in this version, which is probably okay. You can probably check to see if infile is null (if so, you've run out of file) rather than counting. Check into what Vernon was saying, too. My thought is that by reopening the file you're resetting the stream, but test that out (using an infile.clear() to reset it).

Thank you all SO MUCH! Haha, it was the stream. I added infile.clear(); before I closed the file, then reopened it. It works perfectly now, just time to add back in the outputs.

AGAIN, thank you all! =)

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.