Hi all, I am trying to write code that will read from a file that is set up as an inventory file. So, on each line you'll have something similar to:

1. Toothpaste
2. Toothbrush

The problem I'm having is saving the numeric item number into one array index, then the item description into a different array index. The user should be able to add to the file and the program will need to sort this based on item number. I can use getline, but don't know how to delimit it based on this type of file. I've tried using get, but don't know how to assemble the individual characters into one array index. Any help would be greatly appreciated, I'm new to this. Thanks in advance.

If the item numbers are consecutive (no missing numbers) then you don't need to keep them in an array. Just use the array index of the text array as the item number. One way to do it is like this:

std::string item;
// get item number, which is ended with a period
getline(fin, item, '.');
// get the reset of the line

Edited 7 Years Ago by Ancient Dragon: n/a

Thanks for taking the time to respond to my question. It really helps us newbies. The only problem is that the program also has to have a search function that would allow the user to search for an item based on the item #. That being said, wouldn't I have to have the item number in a separate array? Also, the user will need to be able to enter new item #'s and descriptions, not necessarily in succession. So after the user enters a new item, via append, it will then be sorted based on item number. The whole purpose of this assignment is to learn sorting and searching. Thanks again for taking the time to offer your expertise.

This article has been dead for over six months. Start a new discussion instead.