943,916 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 959
  • C++ RSS
Nov 29th, 2008
0

How to read strings into 1D array and more?

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Trader09 is offline Offline
7 posts
since Oct 2008
Nov 29th, 2008
0

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

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. int Message::ExtractIntegerFromString (string input);

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

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 29th, 2008
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Trader09 is offline Offline
7 posts
since Oct 2008
Nov 29th, 2008
0

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

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Trader09 is offline Offline
7 posts
since Oct 2008
Nov 30th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Trader09 ...
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?

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 30th, 2008
0

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

	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?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 30th, 2008
0

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

	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( );
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Trader09 is offline Offline
7 posts
since Oct 2008
Nov 30th, 2008
0

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

Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 30th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Trader09 is offline Offline
7 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: helpppp
Next Thread in C++ Forum Timeline: Yet Another Vtable Problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC