![]() |
| ||
| How to read strings into 1D array and more? 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 message.cpp: #include "Message.h" INCOMPLETE DRIVER: still need linear search but need proper way to read in first. #include "Message.h" |
| ||
| Re: How to read strings into 1D array and more? for(int i = 0; i < 1000; i++) Line 3. There is nothing to convert, since you've never actually read messageIDin 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; You'll need to write the setMessageIDfunction. I don't see it anywhere. |
| ||
| Re: How to read strings into 1D array and more? 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? |
| ||
| Re: How to read strings into 1D array and more? 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! |
| ||
| Re: How to read strings into 1D array and more? Quote:
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++) |
| ||
| Re: How to read strings into 1D array and more? static int messageIDGen; 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? |
| ||
| Re: How to read strings into 1D array and more? Quote:
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( ); |
| ||
| Re: How to read strings into 1D array and more? |
| ||
| Re: How to read strings into 1D array and more? 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. |
| All times are GMT -4. The time now is 2:24 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC