getline failing to read from data file?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 8
Reputation: n8makar is an unknown quantity at this point 
Solved Threads: 0
n8makar n8makar is offline Offline
Newbie Poster

getline failing to read from data file?

 
0
  #1
Apr 13th, 2008
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: getline failing to read from data file?

 
0
  #2
Apr 13th, 2008
Here is how to code that loop. You can delete lines 8, 10 and 19
  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.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: n8makar is an unknown quantity at this point 
Solved Threads: 0
n8makar n8makar is offline Offline
Newbie Poster

Re: getline failing to read from data file?

 
0
  #3
Apr 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: tigger0484 is an unknown quantity at this point 
Solved Threads: 1
tigger0484 tigger0484 is offline Offline
Newbie Poster

Re: getline failing to read from data file?

 
0
  #4
Apr 14th, 2008
I think it should be cin.getline...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: n8makar is an unknown quantity at this point 
Solved Threads: 0
n8makar n8makar is offline Offline
Newbie Poster

Re: getline failing to read from data file?

 
0
  #5
Apr 14th, 2008
Originally Posted by tigger0484 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: getline failing to read from data file?

 
0
  #6
Apr 14th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: n8makar is an unknown quantity at this point 
Solved Threads: 0
n8makar n8makar is offline Offline
Newbie Poster

Re: getline failing to read from data file?

 
0
  #7
Apr 14th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: getline failing to read from data file?

 
0
  #8
Apr 14th, 2008
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.

  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.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: n8makar is an unknown quantity at this point 
Solved Threads: 0
n8makar n8makar is offline Offline
Newbie Poster

Re: getline failing to read from data file?

 
0
  #9
Apr 14th, 2008
looks good thank you for your time and quick replies! also learned a little something about cin.getline...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: n8makar is an unknown quantity at this point 
Solved Threads: 0
n8makar n8makar is offline Offline
Newbie Poster

Re: getline failing to read from data file?

 
0
  #10
Apr 14th, 2008
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:

  1. fin.ignore(80, '\n');

right before the getline trying to read the product name. a simple fix.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC