| | |
reading data from a file into vectors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 32
Reputation:
Solved Threads: 0
Hello again, I'm trying to finish a function I am working on that reads data from a file set up like:
itemID
itemName
pOrdered manufPrice sellingPrice
itemID
itemName
pOrdered manufPrice sellingPrice
....and so on
and puts the data into vectors
this is what I've gotten so far and I'm not sure I am going about this in the right way, the difference between using arrays and vectors is confusing the heck out of me; pSold is supposed to be 0 initially and pInStore is supposed to be the same as pOrdered, that is why they are not in the input file. Any further guidance would be greatly appreciated.
[code]
void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice)
{
int sold = 0;
string name;
int id, ordered;
double mPrice;
double sPrice;
char ch;
infile >> id;
infile.get(ch);
getline(infile, name);
infile.get(ch);
getline(infile, ordered, mPrice, sPrice);
while (infile)
{
itemID.pushback(id);
itemName.pushback(name);
pOrdered.pushback(ordered);
pInStore.pushback(ordered);
pSold.pushback(sold);
manufPrice.pushback(mPrice);
sellingPrice.pushback(sPrice);
}
}
[code]
itemID
itemName
pOrdered manufPrice sellingPrice
itemID
itemName
pOrdered manufPrice sellingPrice
....and so on
and puts the data into vectors
this is what I've gotten so far and I'm not sure I am going about this in the right way, the difference between using arrays and vectors is confusing the heck out of me; pSold is supposed to be 0 initially and pInStore is supposed to be the same as pOrdered, that is why they are not in the input file. Any further guidance would be greatly appreciated.
[code]
void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice)
{
int sold = 0;
string name;
int id, ordered;
double mPrice;
double sPrice;
char ch;
infile >> id;
infile.get(ch);
getline(infile, name);
infile.get(ch);
getline(infile, ordered, mPrice, sPrice);
while (infile)
{
itemID.pushback(id);
itemName.pushback(name);
pOrdered.pushback(ordered);
pInStore.pushback(ordered);
pSold.pushback(sold);
manufPrice.pushback(mPrice);
sellingPrice.pushback(sPrice);
}
}
[code]
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
Just a suggestion, have you considered using a struct/class the encapsulates the data of a single item? You could then operate with a vector of such struct/class instead of the many separate vectors you have now. E.g.
C++ Syntax (Toggle Plain Text)
struct Item { string ID; string Name; double Price; }; void getData(ifstream& infile, vector < Item > & Items) { } int main() { vector < Item > Items; ifstream infile("file.txt"); getData(infile, Items); return 0; }
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
>>getline(infile, ordered, mPrice, sPrice);
To my knowledge there is no version of getline() that takes four parameters, and no version of getline() that stores information in ints or doubles. To my knowledge getline() is only for strings . Therefore I'd suggest a series of three calls to >> to obtain the information for ordered, mPrice and sPrice.
The while loop should start before this line:
infile >> id;
In fact the above line should actually be the conditional statement for the while loop, like this:
while(infile >> id)
BTW: Thanks for using code tags. Unfortunately you forgot to use the / in front of the word code within the [] as the closing tag. I've done that on more than one occassion, too. Next time you'll know to review your code after posting to look for that so you can edit your code and put it in.
To my knowledge there is no version of getline() that takes four parameters, and no version of getline() that stores information in ints or doubles. To my knowledge getline() is only for strings . Therefore I'd suggest a series of three calls to >> to obtain the information for ordered, mPrice and sPrice.
The while loop should start before this line:
infile >> id;
In fact the above line should actually be the conditional statement for the while loop, like this:
while(infile >> id)
BTW: Thanks for using code tags. Unfortunately you forgot to use the / in front of the word code within the [] as the closing tag. I've done that on more than one occassion, too. Next time you'll know to review your code after posting to look for that so you can edit your code and put it in.
Last edited by Lerner; May 3rd, 2008 at 7:53 pm.
•
•
Join Date: Apr 2008
Posts: 32
Reputation:
Solved Threads: 0
Would this be the correct way to do it? I'm having some difficulty following the logic of having the infile >> id as the while condition. could you explain how it works?
C++ Syntax (Toggle Plain Text)
void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice) { int sold = 0; string name; int id, ordered; double mPrice; double sPrice; char ch; while (infile >> id) { infile.get(ch); getline(infile, name); infile >> ordered >> mPrice >> sPrice; itemID.pushback(id); itemName.pushback(name); pOrdered.pushback(ordered); pInStore.pushback(ordered); pSold.pushback(sold); manufPrice.pushback(mPrice); sellingPrice.pushback(sPrice); } }
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
•
•
•
•
Would this be the correct way to do it? I'm having some difficulty following the logic of having the infile >> id as the while condition. could you explain how it works?
C++ Syntax (Toggle Plain Text)
void getData(ifstream& infile, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice) { int sold = 0; string name; int id, ordered; double mPrice; double sPrice; char ch; while (infile >> id) { infile.get(ch); getline(infile, name); infile >> ordered >> mPrice >> sPrice; itemID.pushback(id); itemName.pushback(name); pOrdered.pushback(ordered); pInStore.pushback(ordered); pSold.pushback(sold); manufPrice.pushback(mPrice); sellingPrice.pushback(sPrice); } }
C++ Syntax (Toggle Plain Text)
while (infile >> id) { infile >> name >> ordered >> mPrice >> sPrice; // code to put the info in appropriate vectors }
I don't see the need for the getline or the get commands. All the data is separated by white space, so C++ will figure it exactly where everything starts and stops (I'm assuming that name has no spaces in it?). When you use the >> operator, it's irrelevant whether data is separated by a newline or spaces. When you run out of data, the command
infile >> id will fail, so that will bail you out of the while loop at the proper time.If name IS the first and last name and it's separated by a space, then the above won't work and getline may be a good idea, in which case the way you have it should work (the way you have it should also work even if there are no spaces) . I don't know if you need this line:
C++ Syntax (Toggle Plain Text)
infile.get(ch);
Also, the command is push_back, not pushback:
http://www.cplusplus.com/reference/s...push_back.html
Last edited by VernonDozier; May 4th, 2008 at 1:45 am.
![]() |
Similar Threads
- Reading from file (C++)
- Reading data from text (Again!) (C++)
- data c++ (C++)
- Reading in 2 arrays from one file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Quick TT_EOL question (Java)
- CSV file (C)
- Txt Coordinates into chart... (Java)
- Vectors Versus Arrays (Java)
Other Threads in the C++ Forum
- Previous Thread: C++ static class
- Next Thread: does anyone know what this erros means?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






