How to read strings into 1D array and more?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 7
Reputation: Trader09 is an unknown quantity at this point 
Solved Threads: 0
Trader09 Trader09 is offline Offline
Newbie Poster

How to read strings into 1D array and more?

 
0
  #1
Nov 29th, 2008
For future reference here are the instructions for my assignment. Thank you in advance for any feedback you can provide:

Implement the Message class using a header and implementation file named Message.h and Message.cpp respectively.

In addition to the two Message class files, you must write a driver.

The driver must create a Message array that is capable of holding 1000 messages (make sure that you don’t exceed the maximum number of Messages).

It then must read all Messages from a file named messages.txt (ordered date\n sender\n recipient\n body\n), find a Message based on recipient (value will be given by keyboard input), and print the first five Messages and the Message resulting from the search.

example of a message in file messages.txt:

messageID: 12 (must add this)
date: 2006/11/20
sender: bob@university.edu
recipient: john@university.edu
body: Hope your semester is going well!


Im having a problem adding the message ID to the message.

Im also confused about where I should place my for loops, inside the get functions, set functions or in my driver?

Finally, no vectors allowed.
Any additional advice greatly appreciated.

message.h:

  1. #ifndef MESSAGE_H
  2. #define MESSAGE_H
  3. #include <iostream>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7.  
  8. class Message
  9. {
  10.  
  11. private:
  12. string result;
  13. string sender;
  14. string recipient;
  15. string body;
  16. string date;
  17. int messageID;
  18. static int messageIDGen;
  19. public:
  20. Message();
  21.  
  22. Message(string,string,string,string);
  23.  
  24. ~Message();
  25.  
  26. void setSender(string);
  27.  
  28. void setRecipient(string);
  29.  
  30. void setBody(string);
  31.  
  32. void setDate(string);
  33.  
  34. string getSender();
  35.  
  36. string getRecipient();
  37.  
  38. string getBody();
  39.  
  40. string getDate();
  41.  
  42. string toString();
  43.  
  44. string convertToString( int value );
  45.  
  46. int getMessageID();
  47.  
  48. };
  49. #endif

message.cpp:

  1. #include "Message.h"
  2.  
  3.  
  4. int Message::messageIDGen = 0;
  5.  
  6. Message::Message()
  7. {
  8. messageID = ++messageIDGen;
  9. }
  10.  
  11. Message::Message(string Sender,string Recipient,string Body,string Date)
  12. {
  13. sender = Sender;
  14. recipient = Recipient;
  15. body = Body;
  16. date = Date;
  17. messageID = ++messageIDGen;
  18. }
  19.  
  20. Message::~Message()
  21. {
  22.  
  23. }
  24.  
  25. void Message:: setSender(string Sender)
  26. {
  27. sender = Sender;
  28. }
  29.  
  30. void Message:: setRecipient(string Recipient)
  31. {
  32. recipient = Recipient;
  33. }
  34.  
  35. void Message:: setBody(string Body)
  36. {
  37. body = Body;
  38. }
  39.  
  40. void Message:: setDate(string Date)
  41. {
  42. date = Date;
  43. }
  44.  
  45. string Message:: getSender()
  46. {
  47. return sender;
  48. }
  49.  
  50. string Message:: getRecipient()
  51. {
  52. return recipient;
  53. }
  54. string Message:: getBody()
  55. {
  56. return body;
  57. }
  58. string Message:: getDate()
  59. {
  60. return date;
  61. }
  62.  
  63. string Message:: toString()
  64. {
  65. string result = "Message #" + messageID()
  66. + "\nDate: " + date + "\nSender: " + sender + "\nRecipient: "
  67. + recipient + '\n' + body;
  68. return result;
  69. }
  70.  
  71. string Message:: convertToString( int value )
  72. {
  73. ostringstream temp;
  74. temp << value;
  75. return temp.str();
  76. }
  77.  
  78. int Message:: getMessageID()
  79. {
  80. return messageID;
  81. }

INCOMPLETE DRIVER: still need linear search but need proper way to read in first.

  1. #include "Message.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. Message msg [1000];
  11. string data;
  12. int messageID;
  13.  
  14. ifstream inFile;
  15. inFile.open("messages.txt");
  16. if(!inFile)
  17. {
  18. cout<<"File not found!" << endl;
  19. return 1;
  20. }
  21.  
  22. for(int i = 0; i < 1000; i++)
  23. {
  24. msg[i].convertToString(messageID);
  25.  
  26. getline(inFile,data);
  27. msg[i].setDate(data);
  28.  
  29. getline(inFile,data);
  30. msg[i].setSender(data);
  31.  
  32. getline(inFile,data);
  33. msg[i].setRecipient(data);
  34.  
  35. getline(inFile,data);
  36. msg[i].setBody(data);
  37. }
  38.  
  39. inFile.close();
  40.  
  41. cout << "Which messageID would you like to view?" << endl;
  42. cin >> messageID;
  43.  
  44.  
  45. return 0;
  46.  
  47. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
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 Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: Trader09 is an unknown quantity at this point 
Solved Threads: 0
Trader09 Trader09 is offline Offline
Newbie Poster

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

 
0
  #3
Nov 29th, 2008
Thanks. Messages.txt will only contain sender, recip,date and body, I need to add the messageID. When I read from the file I need to be able to add the messageID somehow and use it as a reference for the user. This is why I thought I could use the function converttostring. What do you think?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: Trader09 is an unknown quantity at this point 
Solved Threads: 0
Trader09 Trader09 is offline Offline
Newbie Poster

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

 
0
  #4
Nov 29th, 2008
to clear it up a little more...
data read from file:
date: 2006/11/20
sender: bob@university.edu
recipient: john@university.edu
body: Hope your semester is going well!

output:
messageID: 12 (I need to add this when I read into my array)
date: 2006/11/20
sender: bob@university.edu
recipient: john@university.edu
body: Hope your semester is going well!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
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
  #5
Nov 30th, 2008
Originally Posted by Trader09 View Post
to clear it up a little more...
data read from file:
date: 2006/11/20
sender: bob@university.edu
recipient: john@university.edu
body: Hope your semester is going well!

output:
messageID: 12 (I need to add this when I read into my array)
date: 2006/11/20
sender: bob@university.edu
recipient: john@university.edu
body: Hope your semester is going well!

OK, messageID is not in the input file, so you need to create it. You don't need a string. messageID is an integer. Why not just make i the messageID?

  1. for(int i = 0; i < 1000; i++)
  2. {
  3. msg[i].setMessageID (i);
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
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
  #6
Nov 30th, 2008
	static int messageIDGen;
public:
	Message();

	Message(string,string,string,string);

Just noticed this line. I guess you are supposed to use messageIDGen to produce the messageID. So you never pass it a value from the driver. Have messageID assigned in your constructor. Assign messageID to equal messageIDGen, then increment messageIDGen. Do all of this in your constructor. You probably aren't supposed to have a setMessgeID function. I hadn't noticed the messageIDGen variable before and the fact that it is static. Was that given to you or did you create it yourself?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: Trader09 is an unknown quantity at this point 
Solved Threads: 0
Trader09 Trader09 is offline Offline
Newbie Poster

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

 
0
  #7
Nov 30th, 2008
Originally Posted by VernonDozier View Post
	static int messageIDGen;
public:
	Message();

	Message(string,string,string,string);

Just noticed this line. I guess you are supposed to use messageIDGen to produce the messageID. So you never pass it a value from the driver. Have messageID assigned in your constructor. Assign messageID to equal messageIDGen, then increment messageIDGen. Do all of this in your constructor. You probably aren't supposed to have a setMessgeID function. I hadn't noticed the messageIDGen variable before and the fact that it is static. Was that given to you or did you create it yourself?
Thanks again, the following was given:

attributes:
sender : string
recipient : string
body : string
date : string
messageID : int
messageIDGen : static int

member functions:
Message( );
Message(string, string, string, string);
~Message( );
void setSender(string);
void setRecipient(string);
void setBody(string);
void setDate(string);
string getSender( );
string getRecipient( );
string getBody( );
string getDate( );
int getMessageID( );
string toString( );
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
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
  #8
Nov 30th, 2008
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: Trader09 is an unknown quantity at this point 
Solved Threads: 0
Trader09 Trader09 is offline Offline
Newbie Poster

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

 
0
  #9
Nov 30th, 2008
Thanks again.
Can anyone help with my linear search?
What is the basic structure for a linear search for a word in my array of read in data?
Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC