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;

}

Recommended Answers

All 8 Replies

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);
	}

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:

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:

int Message::ExtractIntegerFromString (string input);

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

int messageid;
string input;
inFile >> input;
int messageid = msg[i].ExtractIntegerFromString (input);
msg[i].setMessageID (messageid);

You'll need to write the setMessageID function. I don't see it anywhere.

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?

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?

for(int i = 0; i < 1000; i++)
	{
		msg[i].setMessageID (i);

		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);
	}
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?

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( );

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.