Can't understand my error...

This is a discussion on Can't understand my error... within the C++ Programming forums, part of the General Programming Boards category; bumping it for myself...it was a few pages behind...

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

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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();
    		}
    	}
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,923
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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...

Page 2 of 2 FirstFirst 12
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, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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