| | |
How to read strings into 1D array and more?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 7
Reputation:
Solved Threads: 0
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:
message.cpp:
INCOMPLETE DRIVER: still need linear search but need proper way to read in first.
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)
#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:
C++ Syntax (Toggle Plain Text)
#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.
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
C++ Syntax (Toggle Plain Text)
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: C++ Syntax (Toggle Plain Text)
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)
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)
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. Last edited by VernonDozier; Nov 29th, 2008 at 10:46 pm.
•
•
Join Date: Oct 2008
Posts: 7
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
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)
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); }
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
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?
•
•
Join Date: Oct 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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?
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( );
![]() |
Similar Threads
- strings, arrays, length program (C++)
- Read from a file then placing strings into a 2d array (C)
- Reading .dat file data into an array (C#)
- infile into array and sort (C++)
- Declaring string array, initializing later... (C++)
- Array help (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: map<string,string> error
- Next Thread: Yet Another Vtable Problem
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






