Thread: Can't understand my error...

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    bumping it for myself...it was a few pages behind

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when calling ordered second time
    start == "2"
    so ordered skips "1"
    outputs "2" and calls rest

    rest starts with "1" and because it is != "2" - outputs it, then skips "2", outpupts "3" and "4"

    If you think this behavior is incorrect - fix your code logic
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Not really understand what you are trying to achive with this long code, but the desired output could be received simplier

    Code:
    #include <iostream>
    #include <list>
    #include <string>
    
    using std::cout;
    using std::endl;
    using std::string;
    using std::cin;
    using std::list;
    
    void ordered(list<string>::iterator& start, std::list<std::string>& phrase);
    
    int main(){
    
    	//asks for user input
    	cout << "Please type in your input: ";
    	
    	//the container
    	list<string> sentence;
    	string x;
    	
    	//adds user input to container
    	while(cin >> x)
    	{
    		sentence.push_back(x);
    	}
    	
    	sentence.sort();
    	
    	//starts container from beginning, and continues until the end
    	for(list<string>::iterator iter = sentence.begin(); iter != sentence.end(); iter++)
    	{
    		ordered(iter, sentence);
    		cout << endl;
    	}
    
    
    	return 0;
    
    }
    
    void ordered(list<string>::iterator& start, std::list<std::string>& phrase)
    {
    	
    	//current word the iterator is on
    	list<string>::iterator current(start);
    	cout << *current << ' ';
    	current++;
    	if (current == phrase.end())
    	{
    		current = phrase.begin();
    	}
    	
    	//while the count is not the end
    	while(current != start)
    	{
    		cout << *current << ' ';
    		current++;
    		if (current == phrase.end())
    		{
    			current = phrase.begin();
    		}
    	}
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Code:
    list<string>::iterator current(start);
    May I ask what does this part do?

    I also got my code to work, but I am trying to understand yours also.

    Code:
    void rest(std::string& start, std::list<std::string>& phrase){
    	
    	//counts the number of words outputted
    	list<string>::iterator count = phrase.begin();
    	//current word the iterator is on
    	list<string>::iterator current = phrase.begin();
    	
    	while(*current != start){
    		current++;
    	}
    
    	//while the count is not the end
    	while(count != phrase.end()){
    		
    		//if it reaches the end set it to the beginning
    		if (current == phrase.end()){
    			current = phrase.begin();
    		}
    
    		//if start equals current continue along and increment current and count
    		if(start == *current){
    			current++;
    			count++;
    		}
    		
    		//otherwise output the current and increment current and count 
    		else{
    			cout << *current << ' ';
    			current++;
    			count++;
    			}
    		}
    
    }
    I had to add a line of code to rline to let it go through the list until it reaches start and then continue from there.
    Last edited by dnguyen1022; 03-26-2009 at 10:19 PM.

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dnguyen1022 View Post
    Code:
    list<string>::iterator current(start);
    May I ask what does this part do?
    this initializes the iterator to be pointed to the member where start points. Because I want to start printing from this point

    I do not see a reason to start from the beginning and then skip till the start if I already have the pointer I need in hand
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dnguyen1022
    Basically take in a string, and a phrase. Find that string in the phrase, and then output that phrase starting from that string. If that string is in the middle of the phrase, output until it hits the end, and recurse back to the beginning of the phrase and output until count hits phrase.end(), or if count was an int, phrase's size. It should keep recursing as long as count does not equal to phrase.end().
    Seeing that you managed to implement the rotation on your own, I would now suggest that you use say, std::rotate_copy(). For example:
    Code:
    #include <string>
    #include <iostream>
    #include <list>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
        using namespace std;
    
        string word;
        cout << "Please enter the word: ";
        cin >> word;
    
        list<string> phrase;
        cout << "Please enter the phrase: ";
        copy(istream_iterator<string>(cin), istream_iterator<string>(),
            back_inserter(phrase));
    
        list<string>::iterator found = find(phrase.begin(), phrase.end(), word);
        if (found != phrase.end())
        {
            rotate_copy(phrase.begin(), found, phrase.end(),
                ostream_iterator<string>(cout, " "));
        }
        else
        {
            copy(phrase.begin(), phrase.end(), ostream_iterator<string>(cout, " "));
        }
        cout << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dnguyen1022 View Post
    bumping it for myself...it was a few pages behind
    If it's been that long, surely you can describe what you've investigated since the last time this post was on the front page.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Quote Originally Posted by vart View Post
    this initializes the iterator to be pointed to the member where start points. Because I want to start printing from this point

    I do not see a reason to start from the beginning and then skip till the start if I already have the pointer I need in hand
    Ah I see the code makes sense now, and it is muchh shorter! Thank you.

    @laserlight
    When I get home I'll look over this code and understand the functions, and if I have any questions I'll post back. Thanks!

    Quote Originally Posted by brewbuck View Post
    If it's been that long, surely you can describe what you've investigated since the last time this post was on the front page.
    It was only one day..when I bumped it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM