View Single Post
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How to read strings into 1D array and more?

 
0
  #2
Nov 29th, 2008
  1. for(int i = 0; i < 1000; i++)
  2. {
  3. msg[i].convertToString(messageID);
  4.  
  5. getline(inFile,data);
  6. msg[i].setDate(data);
  7.  
  8. getline(inFile,data);
  9. msg[i].setSender(data);
  10.  
  11. getline(inFile,data);
  12. msg[i].setRecipient(data);
  13.  
  14. getline(inFile,data);
  15. msg[i].setBody(data);
  16. }

Line 3. There is nothing to convert, since you've never actually read messageID in from the file, have you? What conversion is required, by the way? You store messageID as an integer, but you call a function called convertToString , which takes an integer and returns a string. You have this as input:

  1. messageID: 12

Read it in as a string, then pass it to a function that returns an integer, not vice versa, because you want the 12 and you want to throw out the part before the 12. So you need a function that parses a string and extracts the integer at the end of it:
  1. int Message::ExtractIntegerFromString (string input);

After doing that, this could be the code in your loop to replace line 3 above.

  1. int messageid;
  2. string input;
  3. inFile >> input;
  4. int messageid = msg[i].ExtractIntegerFromString (input);
  5. msg[i].setMessageID (messageid);

You'll need to write the setMessageID function. I don't see it anywhere.
Last edited by VernonDozier; Nov 29th, 2008 at 10:46 pm.
Reply With Quote