Need assistance with a retrieval program that outputs into another .txt file

Okay, this program is designed to withdraw information from a .txt file that contains a list of grocery items. After the item number is used, then the item number will be withdrawn and you will have to input how many of the item there is. What I am having a problem with is that when I run it, it works fine to a point, but if I put an invalid number before a valid number, then the items after that invalid number continue to state that its invalid, even if it is valid. I am not sure how to change or make it work anymore and am requesting help. Besides that, it works, and also curious of how to use set precision correctly to space everything out.

Thank you.

//This program is designed to ask for a unit item, if taxed with calculate the tax and withdraw the item from 'item.txt'
//to a program called 'mark.txt'

//Mark
//Homework Assignment #3

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

int main ()

{

ifstream inputdata;
ofstream outputdata;

inputdata.open("item.txt");
outputdata.open("mark.txt");

int itemnum;
string itemname;
char unit;
float unitprice;
char tax;
float subtotal;
int itemn;
float taxamt;
float total;
float tmp;
float taxtmp;

//Initializing the variables...
taxamt = .06;
subtotal = 0;
tmp = 0;
taxtmp = 0;
total = 0.0;

outputdata << "Item number " << "Item Name " << "Unit " << "Unit price " << "Tax " << "Subtotal" << endl;

cout << "Menu for Grocery Lists" << endl;
cout << "-------------------------" << endl;
cout << "Enter Item Number" << endl;
cout << "Press '0' to exit and print out reciept" << endl;

//where the Item input is
cin >> itemn;

//While the itemn does not equal (checks the user input)

int isFound = 0;
int numWanted;
while(itemn != 0) {

//While not the end of file (gets values from the input file)
while( inputdata >> itemnum >> itemname >> tax >> unit >> unitprice ) {

//matching input to .txt file
if(itemn == itemnum) {

cout << "How many of " << itemname << " do you want?" << endl;
cin >> numWanted;

isFound++;
subtotal = numWanted * unitprice;
if(tax == 't') {
taxtmp = subtotal * taxamt;
subtotal = subtotal + taxtmp;
}

total = total + subtotal;

outputdata << itemnum << " " << itemname << " " << numWanted << " " << unitprice << " " << taxtmp << " " << subtotal << endl;

break;
}
}

if(isFound == 0)
cout << "Invalid Number, please try again" << endl;
else
cout << "Enter Item Number" << endl << "Press '0' to exit and print out reciept" << endl;


//move the inputdata pointer back to top of list
inputdata.seekg(0);



cin >> itemn;
}

outputdata << "Total" << total << endl;

inputdata.close();
outputdata.close();

}

Any help is appreciated.
the way you wrote the prog is verey confusing try to make your prog more structured
you can use for the end of file
1
2
while (inputData)//will repeat the loop until the end of the file
//then read the codes  

also u can use the do while loop insted of writing inputdata.seekg(0);


and about the setprecision we use it to determain how many digits we want for float
values
if u want to controll the spaces u can use setw()
all this u can find them in the refernce of this site

Topic archived. No new replies allowed.