| | |
Reading data from a file and storing into seperate variables
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 Nov 6th, 2009
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; Nov 6th, 2009 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 Nov 7th, 2009
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
Views: 310 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






