Thread: question on methods

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    Post question on methods

    Hiya,

    I was wondering if anyone else could give me some input on what would be the best way to search a string for some values, and then print it out.

    Suppose I have a string like: ">From: me To: you Subject: fun! Message: I hope you're having fun and all Time: Fri Apr 11 17:57:16 2003"

    First I want to search the string for who it's from, and the subject. I've been using string.find(">From") and then another .find() to get the end of the sender's name. Then I make a .substr() with those values. Then I do that again for the subject.

    so like:
    Code:
       From                  Subject
    -----------------------------------
    1. Me                   fun!
    2. Me                   C++ is cool!

    I've also been trying to store those values in a vector, because eventually, there will be several strings like this, and I'd like to keep them organized. I will also eventually be viewing or deleting the whole message, so I thought storing the values in a vector might be easy. Anyway, I just wanted to know if anyone else had any ideas that might simplify this process. Thanks!

    Jim

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is to search for key words such as "From: " and "To: ."

    Kuphryn

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    18
    Yeah, that's what I'm doing now. I search for From and the subject, and then put those in a vector. I am currently having trouble with a way of finding the next occurance of From and Subject, and so-on and so-forth. I was thinking of putting it in a for() loop, but I haven't had any luck with that yet.

    cheers,

    Jim

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    do you have a vector of strings? if so, then try searching through vect[i] in a for loop; maybe having another temp vector of strings for storing substrings.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is a while loop and the last position.

    Code:
    size_t nFind = theMsg.find("From: ");
    if (nFind != theMsg.npos)
    {
    ...
    // First position of valid data.
    nFind += 6;
    }
    else
    nFind = 0;
    
    nFind = theMessage.find("To: ", nFind);
    ...
    As you can see, the algorithm is very simply. I recommend one function for each key word search. That will help organize your code.

    Kuphryn

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    Unhappy still having trouble

    I've been trying various methods, but none of them is giving me the output I want, or it terminates the program because of an error.
    This is what I have so far:
    Code:
    string ms;
    int s1, f1, hold, f2hold, f1hold, length, i=0;
    string f2, s2;
    
    ms = ">From: Jim\nTo: Lauren\nSubject: Zoo\nMessage: I was just wondering if you were going to the zoo today.  
    Gimmie an email please. \nTime: Sat Apr 12 11:32:09 2003>From: Lem\nTo: Jim\nSubject: Re:Zoo\n
    Message: Nah, not going today.  \nTime: Sat Apr 12 11:32:09 2003";  
    // string is all one line
    
    while(ms.find("From:")){
        length = ms.size();
        f1 = ms.find("From:");
        hold = ms.find("\nTo:");
        f1hold = hold - f1;
        f2.assign( ms, f1, f1hold);
    
        s1 = ms.find("Subject");
        hold = ms.find("\nMessage:");
        f2hold = hold - s1;
        s2.assign( ms, s1, f2hold);
        cout << ++i << ": " << f2 << "     " << s2 << endl;;
           ms.erase (hold, length);
                 }
    What I want it to do is search for the From and Subject, print it out, then delete the section it's already checked, and begin the process again.
    So I want the output to be:

    1. From: Jim Subject: Zoo
    2. From: Lem Subject: Re:Zoo

    I tried the .npos method, but I wasn't able to make it work.

    cheers,

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Lesson #5 - Methods
    By oval in forum C# Programming
    Replies: 1
    Last Post: 05-04-2006, 03:09 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Replies: 8
    Last Post: 07-27-2003, 01:52 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM