Thread: Help with output

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    Help with output

    Code:
    void Graph::printPath( const Vertex & dest ) const
    {
       string m;
        if( dest.prev != NULL )
        {
             vector<Edge> e = (dest.prev)->adj;
            for(unsigned int i = 0; i< e.size(); i++)
              {
                Edge edge = e[i];
                if( edge.dest == &dest)
                {
                    m = (*edge.movieTitle).title();
                    break;
                }
            
              }
              
              printPath( *dest.prev );
              
              cout << " was in " << m << " with ";
              cout << ((*data)[(dest.a)]).name() << endl;
         	  cout << ((*data)[(dest.a)]).name();
        	
       
        }
        else
        {
        	cout << ((*data)[(dest.a)]).name();	
        } 
    }
    I have this function and currently with the input I give it the output is:

    Damon, Matt was in Saving Private Ryan with Hanks, Tom
    Hanks, Tom was in Apollo 13 with Harris, Ed
    Harris, Ed
    It is displaying the final name an extra time. I want the output to just be:


    Damon, Matt was in Saving Private Ryan with Hanks, Tom
    Hanks, Tom was in Apollo 13 with Harris, Ed
    Anyone know how I can fix this?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by tallguy View Post
    Code:
    void Graph::printPath( const Vertex & dest ) const
    {
       string m;
        if( dest.prev != NULL )
        {
             vector<Edge> e = (dest.prev)->adj;
            for(unsigned int i = 0; i< e.size(); i++)
              {
                Edge edge = e[i];
                if( edge.dest == &dest)
                {
                    m = (*edge.movieTitle).title();
                    break;
                }
            
              }
              
              printPath( *dest.prev );
              
              cout << " was in " << m << " with ";
              cout << ((*data)[(dest.a)]).name() << endl;
         	  cout << ((*data)[(dest.a)]).name();
        	
       
        }
        else
        {
        	cout << ((*data)[(dest.a)]).name();	
        } 
    }
    I guess it's time to bang your head on your desk =P

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    But I'm doing that because I want to display it again on the next line as the first name.

    Damon, Matt was in Saving Private Ryan with Hanks, Tom
    Hanks, Tom was in Apollo 13 with Harris, Ed

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Oh, sorry, I didn't see that.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    At the point in the function after you call printPath, you know that *dest.prev refers to the first name in the line and dest refers to the second name, right? So just use that information to write out an entire single line by itself right there.

    At least I think that will work. You will want to get rid of the else, since I assume that is meant only to print the very first name the first time, but the new method won't need that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM