| | |
getline failing to read from data file?
![]() |
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
sort of a noob as i have been doing c++ for only a couple months now. my problem is that i am using a function to pass data from a file into an array then into a .out file. the function im using to read the data from the file is using a while loop. the data file consists of the name of a store, then the pattern of a product code then product name then product price repeated over and over for multiple products. the product name has embedded blanks which i try to read with getline but fail horribly. no compiler error just debug assertion failure. using vs. can provide more info if needed. dont think you need main or other functions.function code for reading data is as follows:
C++ Syntax (Toggle Plain Text)
long readFile (string code[], string product[], double price[]) { string temp, store; long count; ifstream fin ("program5.txt"); getline(fin,store); count=0; fin>>temp; while (!fin.eof()) { code[count]=temp; getline(fin,product[count]); fin>>price[count]; count++; fin>>temp; } fin.close(); return count; }
Last edited by Ancient Dragon; Apr 13th, 2008 at 10:01 pm. Reason: replace icode tags with code tags
Here is how to code that loop. You can delete lines 8, 10 and 19
C++ Syntax (Toggle Plain Text)
int count = 0; while( getline(fin, product[count]) ) count++;
Last edited by Ancient Dragon; Apr 13th, 2008 at 10:09 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.
There are two versions of getline() -- cin.getline() is for character arrays and getline(cin,...) if for std::strings.
You didn't say how the data file is formatted, so I just assumed it has all the information on one line because you said you wanted to use getline(). Post a few lines of the data file so that we can see how it should be read.
You didn't say how the data file is formatted, so I just assumed it has all the information on one line because you said you wanted to use getline(). Post a few lines of the data file so that we can see how it should be read.
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.
First I would create a structure (or class if you like) to contain all the information for one company. Change the function to accept a reference to a vector of structures instead of all those string arrays. That will simplify your intire program.
C++ Syntax (Toggle Plain Text)
struct company { string companyName; string something; // e.g. A123, whatever that represents string somethingElse // e.g. case // etc. etc for each of the other fields }; // create an array to hold the structures long readFile (vector<company>& colist) { // now read the data struct company co; while( getline(fin,co.company) ) { getline(fin,co.something); getline(fin,co.somethingElse); // etc for each field // // finally put co into the vector colist.push_back(co); } }
Last edited by Ancient Dragon; Apr 14th, 2008 at 2:13 am.
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: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
ok since im a beginner this was explained to me in a different way that was easier to understand. not bashing anyone else's solution, but this fix requires only one line of code. im just posting this in case someone else has a similar problem. apparently after reading the product code (the A123) a new line was trying to be read by the getline. therefore the only thing needed was:
right before the getline trying to read the product name. a simple fix.
C++ Syntax (Toggle Plain Text)
fin.ignore(80, '\n');
right before the getline trying to read the product name. a simple fix.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Connect 4 computer player
- Next Thread: Windows API - Use QT Now?
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel fail-thread file forms fstream function functions game getline givemetehcodez gmail graph gui header homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






