looking for errors

This is a discussion on looking for errors within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having some trouble making this program work, and I thought another set of eyes might help. I'm also ...

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

    Post looking for errors

    Hi, I'm having some trouble making this program work, and I thought another set of eyes might help. I'm also hoping that I could get some suggestions to improve it from its current form. I've been staring at this screen for hours and my eyes are totally shot. Anyway, what I'm trying to do is use a character pointer to slide along the string so I can extract all of the From: and Subject fields so they look something like:

    1. Jim Zoo
    2. Lauren Hey

    I also thought I might simplify things a bit by terminating each message with a unique character, so i don't have to do the size check or the x++, and maybe build my loop clause to check for the special char. anyway, i'm exausted and can't revise this code anymore tonight, but I hope someone can offer me some tips on how to improve it. Thanks.
    Code:
    string msg = ">From: Jim\nTo: Lauren\nSubject: Zoo\nMessage: blah\n>From: Lauren\nTo: Jim\nSubject: Hey\nMessage: blah\n";
    
    string s = msg;
    const char *pointer = msg.c_str();
    int x=0;
    int size = s.length();
    
    
    while (x< size) {
    	pointer = pointer + 7;  //pointer shift to end of first FROM
    	x + 7;
    	
    	string temp;
    	while (*pointer != '\\') {     // the while does have double slashes, just doesn't show up in the code here
    		temp = temp + *p;
    		pointer++;
    		x++;
    		}   //iterate along the string until first \ character is reached
    
    	cout<<"From is "+temp<<endl;
    
    	pointer= pointer+2; //skips us past the \n character
    	x=x+2;
    
    	while (*pointer !='\\') {
    		pointer++;
    		x++;
    		} //slide along until we get to the subject
    	pointer = pointer+2;
    	x=x+2;
    	
    
    	pointer=pointer + 7;
    	x=x+7; //skip past subject word
    
    
    	string temp_two;
    	while (*pointer !='\\') {
    		temp_two = temp_two + *p;
    		pointer++;
    		x++;
    		} //get the subject
    	cout<<"subject is"+temp_two<<endl;
    	pointer = pointer+2;
    	x=x+2;
    
    	while (*pointer !='\\') {
    		pointer++;
    		x++;
    		}//slide along to get past the message	
    
    	}
    
          //cout << "\n\tPress any key to return to the main menu";
          // getchar();
    
    }
    btw, it compiles fine, but it crashes when I run the executable
    Last edited by jimothygu; 04-14-2003 at 11:39 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I would be tempted to use the >> to parse msg.
    Code:
    //in pseudo code
    string input;
    ifstream fin;
    
    while(fin >> input)
    {
       if input == ">From:";
          fin >> input;
          cout << "1. " << input << "  ";
       else if input == "Subject:"
          fin >> input; 
          cout << "2. " << input << endl;
       else
         do nothing
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21