| | |
Reading data from a file and storing into seperate variables
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
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
etc...
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.
1. Toothpaste
2. Toothbrush
etc...
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.
-7
#2 30 Days Ago
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:
C++ Syntax (Toggle Plain Text)
std::string item; // get item number, which is ended with a period getline(fin, item, '.'); // get the reset of the line getline(fin,item);
Last edited by Ancient Dragon; 30 Days Ago at 10:26 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
0
#3 29 Days Ago
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.
![]() |
Similar Threads
- help with reading data from file (C)
- Reading data from file and storing in an array using both calloc and sscanf (C)
- Reading data to a file (Java)
- Reading a txt file into a swing component (Java)
- Help reading data from a file (C++)
- Help with reading data from a file (C++)
- reading data from file to vector (C++)
Other Threads in the C++ Forum
- Previous Thread: Connect 4 won function
- Next Thread: Visual C++ form question
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






