943,800 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3728
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 13th, 2008
0

getline failing to read from data file?

Expand Post »
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)
  1. long readFile (string code[], string product[], double price[])
  2. {
  3. string temp,
  4. store;
  5. long count;
  6. ifstream fin ("program5.txt");
  7.  
  8. getline(fin,store);
  9. count=0;
  10. fin>>temp;
  11.  
  12. while (!fin.eof())
  13. {
  14. code[count]=temp;
  15. getline(fin,product[count]);
  16. fin>>price[count];
  17.  
  18. count++;
  19. fin>>temp;
  20. }
  21.  
  22. fin.close();
  23. return count;
  24. }
Last edited by Ancient Dragon; Apr 13th, 2008 at 10:01 pm. Reason: replace icode tags with code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n8makar is offline Offline
8 posts
since Apr 2008
Apr 13th, 2008
0

Re: getline failing to read from data file?

Here is how to code that loop. You can delete lines 8, 10 and 19
C++ Syntax (Toggle Plain Text)
  1. int count = 0;
  2.  
  3. while( getline(fin, product[count]) )
  4. count++;
Last edited by Ancient Dragon; Apr 13th, 2008 at 10:09 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Apr 14th, 2008
0

Re: getline failing to read from data file?

ok that helps but i need more than just the product name. i need the product codes and prices to be put into arrays as well thus making 3 arrays.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n8makar is offline Offline
8 posts
since Apr 2008
Apr 14th, 2008
0

Re: getline failing to read from data file?

I think it should be cin.getline...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tigger0484 is offline Offline
6 posts
since Apr 2008
Apr 14th, 2008
0

Re: getline failing to read from data file?

Click to Expand / Collapse  Quote originally posted by tigger0484 ...
I think it should be cin.getline...
tried it. first it wouldnt let me convert from string to char. also even if it did i think cin.getline would ask the user to input data which isnt what we want. it needs to be read from the data file.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n8makar is offline Offline
8 posts
since Apr 2008
Apr 14th, 2008
0

Re: getline failing to read from data file?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Apr 14th, 2008
0

Re: getline failing to read from data file?

This is the text file. ignore spelling mistakes as I had to make all product names one word.

Chuck's Computer Company
A123
Case
59.99
B234
Motherboard
149.99
C345
Processor
199.99
D456
Keyboard
14.99
E567
Monitor
39.99
F789
Mouse
4.99
G890
Harddrive
9.99
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n8makar is offline Offline
8 posts
since Apr 2008
Apr 14th, 2008
0

Re: getline failing to read from data file?

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)
  1. struct company
  2. {
  3. string companyName;
  4. string something; // e.g. A123, whatever that represents
  5. string somethingElse // e.g. case
  6. // etc. etc for each of the other fields
  7. };
  8.  
  9. // create an array to hold the structures
  10. long readFile (vector<company>& colist)
  11. {
  12. // now read the data
  13. struct company co;
  14. while( getline(fin,co.company) )
  15. {
  16. getline(fin,co.something);
  17. getline(fin,co.somethingElse);
  18. // etc for each field
  19. //
  20. // finally put co into the vector
  21. colist.push_back(co);
  22. }
  23. }
Last edited by Ancient Dragon; Apr 14th, 2008 at 2:13 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Apr 14th, 2008
0

Re: getline failing to read from data file?

looks good thank you for your time and quick replies! also learned a little something about cin.getline...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n8makar is offline Offline
8 posts
since Apr 2008
Apr 14th, 2008
0

Re: getline failing to read from data file?

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:

C++ Syntax (Toggle Plain Text)
  1. fin.ignore(80, '\n');

right before the getline trying to read the product name. a simple fix.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n8makar is offline Offline
8 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Connect 4 computer player
Next Thread in C++ Forum Timeline: Windows API - Use QT Now?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC