RSS Forums RSS

How to read strings into 1D array and more?

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
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?

  #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:

#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;

}

AddThis Social Bookmark Button
Reply With Quote  
Posts: 2,903
Reputation: VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of 
Solved Threads: 367
VernonDozier VernonDozier is online now Online
Posting Maven

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

  #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:

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.

  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 9:46 pm.
Reply With Quote  
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?

  #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  
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?

  #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  
Posts: 2,903
Reputation: VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of 
Solved Threads: 367
VernonDozier VernonDozier is online now Online
Posting Maven

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

  #5  
Nov 29th, 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?

	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);
	}
Reply With Quote  
Posts: 2,903
Reputation: VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of 
Solved Threads: 367
VernonDozier VernonDozier is online now Online
Posting Maven

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

  #6  
Nov 29th, 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  
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?

  #7  
Nov 29th, 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  
Posts: 2,903
Reputation: VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of 
Solved Threads: 367
VernonDozier VernonDozier is online now Online
Posting Maven

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

  #8  
Nov 29th, 2008
Reply With Quote  
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?

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 617 | Replies: 8 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:33 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC