I'm making a program that asks for an item number and if it matches an item number in the text file it will then determine its next move by what variable is displayed on the line that the item number is on. So, lets say I have a text file:
"Item number, Item name, taxability, unit type, price" and its 100 lines long. I need to match the user input to the item number then use the taxability variable, unit type, and price to get a sub-total. Then loop it to keep asking for an item number, ect. I'm a beginner, but from my code it looks like it "should" work.

Might want to print a newline there. Or an endl. You might also want to assign "grocery_item.txt" to a variable and use that rather than typing out "grocery_item.txt" over and over again. (Okay, twice.)

Just from the title I'm guessing you want the user to enter an item number, and then search the entire file for that item number, etc, is that right? Because that's not what the code does right now.

Have you tested your code?

10-03-2007

ch68813

Thats exactly what I want it to do. I tested it and it only works once for the first item number in the file. I'm very new to this and what I have been bonking my head over is how to make my program search my text file for the user's input.

10-03-2007

dwks

You're going to have to search the whole file for each item number. This means that you have to read every line in the file, comparing it with the item number until you find a match or you reach the end of the file; and then rewind the file to the beginning so that you can start searching for the next number. Or, alternatively, you could read the whole file into memory into an array of lines, say, and search through that.

Assuming that you want to use the first method . . . I think file.seekg(0) will rewind the file once you're done with it. The code might look something like this:

Code:

repeat until done
get an item number from the user
for every line in the file
parse the line into item numbers, etc
if the item number entered by the user is equal to the one in the file
do whatever you do for a successful match
end the for-every-line-in-the-file loop
if the end of the file was reached
do whatever you do when there is no matching item number in the file
rewind the file to the beginning

10-03-2007

ch68813

I think my problem lies in this part

Code:

repeat until done
get an item number from the user
for every line in the file
parse the line into item numbers, etc

I'm not to knowledgable in what kind of loops I should be using in these areas or how to parse my variables.

10-03-2007

dwks

You're already "parsing your variables" with this code:

Code:

if (instream >> Inum >> name >> letter1 >> letter2 >> price)

What types of loops should you use? It doesn't really matter. Personally, I might go with something like this: