Thread: Help on The Towers of Hanoi

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

    Post Help on The Towers of Hanoi

    HI!!!!
    I need some help on this program. I really don't know where I am...If I am very far for what I want or I am close, right now I am getting some error on the Solve function where I try to do this: Source.push_back(Destination.pop_back()); //Move from Source to Destination....................
    The error says something about the function is declared void..like if I am trying to return something.....

    If you have some hints for me or if you want to see how the output should look like, I have an executable file (for linux/unix system) that I can send to you. Right now I am programming it on Dev++ on Windows, later on I will paste it and everything on a linux system..Thanks for any help you can give to me

    Code:
    //Towers of Hanoi....
    
    using namespace std;
    
    #include <iostream>
    #include <stdlib.h>
    #include <vector>
    #include <iomanip>
    #include <string>
    #include <cmath>
    
    void header(int &argc, int i);
    void show(int &argc, vector<int> &array1, vector<int> &array2, vector<int> &array3);
    void waitForUser();
    void Solve(int N, vector<int> &Source, vector<int> &Aux, vector<int> &Destination);
    void init(int argc, vector<int> &array1);
    void swap(int &N, vector<int> &Source, vector<int> &Destination);
    
    int main(int argc, char *argv[])
    {
      argc = 3;
      int steps = pow(2,argc)-1;
       vector<int> array1(argc);
       vector<int> array2(argc);
       vector<int> array3(argc);  
       
      
         for(int i = 0; i <= steps; i++){
             header(argc, i);
             init(argc, array1);
             Solve(argc, array1, array2, array3);
             show(argc, array1, array2, array3);
             waitForUser();
             //system("clear");
         }
        
      system("PAUSE");	
      return 0;
    }
    void header(int &argc, int i){
       cout << "**** HANOI TOWER ****" << endl;
       cout << "N=" << argc << setw(17) << "STEP " << i << endl;
    }
    void show(int &argc, vector<int> &array1, vector<int> &array2, vector<int> &array3){
         for(int j = 0; j < argc; j++){
              
            
            cout << setw(3) << array1[j] << setw(8) << array2[j] << setw(8) << array3[j] << endl;
      }
      cout << "=====   =====   =====\n";
      cout << "  A       B       C\n\n";
    }
    void waitForUser() {
      // waits for the user to hit <ENTER>
      string dummy;
    
      cout << "Press <ENTER>";
      getline(cin, dummy);
      cout << endl;
    }
    void Solve(int N, vector<int> &Source, vector<int> &Aux, vector<int> &Destination){
       for(int i = 0; i < N; i++){
         // while{N =! 0){
            Solve(N-1, Source, Destination, Aux);
            Source.push_back(Destination.pop_back());  //Move from Source to Destination
            Solve(N-1, Aux, Source, Destination);
         // }
        }
    }
    void init(int argc, vector<int> &array1){
       for(int j = 0; j < argc; j++)
           array1[j] = j+1;
    }
    void swap(int &N, vector<int> &Source, vector<int> &Destination){
       for(int i = 0; i < N; i++){
          Destination[i] = Source[i];
          //Source[i] = 0;
       }
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    pop_back() returns void...

    gg

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    What means that pop_back() returns void????

  4. #4
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >What means that pop_back() returns void????
    It means it doesn't return a value, so
    Code:
    Source.push_back(Destination.pop_back());
    will not work. Look up your reference for how to use pop_back.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. towers of hanoi - what is wrong with this code?
    By kanesoban in forum C Programming
    Replies: 4
    Last Post: 09-17-2007, 01:20 PM
  2. Towers of Hanoi (need help)
    By Loudan in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2006, 10:17 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Towers of Hanoi, special output.
    By spoon_ in forum C Programming
    Replies: 3
    Last Post: 03-15-2003, 06:08 PM
  5. Towers of Hanoi
    By janehung in forum C Programming
    Replies: 12
    Last Post: 01-07-2002, 06:40 AM