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:
#ifndef MESSAGE_H
#define MESSAGE_H
#include <iostream>
#include <sstream>
using namespace std;
class Message
{
private:
string result;
string sender;
string recipient;
string body;
string date;
int messageID;
static int messageIDGen;
public:
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();
string toString();
string convertToString( int value );
int getMessageID();
};
#endif
message.cpp:
#include "Message.h"
int Message::messageIDGen = 0;
Message::Message()
{
messageID = ++messageIDGen;
}
Message::Message(string Sender,string Recipient,string Body,string Date)
{
sender = Sender;
recipient = Recipient;
body = Body;
date = Date;
messageID = ++messageIDGen;
}
Message::~Message()
{
}
void Message:: setSender(string Sender)
{
sender = Sender;
}
void Message:: setRecipient(string Recipient)
{
recipient = Recipient;
}
void Message:: setBody(string Body)
{
body = Body;
}
void Message:: setDate(string Date)
{
date = Date;
}
string Message:: getSender()
{
return sender;
}
string Message:: getRecipient()
{
return recipient;
}
string Message:: getBody()
{
return body;
}
string Message:: getDate()
{
return date;
}
string Message:: toString()
{
string result = "Message #" + messageID()
+ "\nDate: " + date + "\nSender: " + sender + "\nRecipient: "
+ recipient + '\n' + body;
return result;
}
string Message:: convertToString( int value )
{
ostringstream temp;
temp << value;
return temp.str();
}
int Message:: getMessageID()
{
return messageID;
}
INCOMPLETE DRIVER: still need linear search but need proper way to read in first.
#include "Message.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
Message msg [1000];
string data;
int messageID;
ifstream inFile;
inFile.open("messages.txt");
if(!inFile)
{
cout<<"File not found!" << endl;
return 1;
}
for(int i = 0; i < 1000; i++)
{
msg[i].convertToString(messageID);
getline(inFile,data);
msg[i].setDate(data);
getline(inFile,data);
msg[i].setSender(data);
getline(inFile,data);
msg[i].setRecipient(data);
getline(inFile,data);
msg[i].setBody(data);
}
inFile.close();
cout << "Which messageID would you like to view?" << endl;
cin >> messageID;
return 0;
}