View Single Post
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