Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 705483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:03:32+00:00 2026-05-14T04:03:32+00:00

I have a Prolog file (Hanoi.pl) containing the code for solving the Hanoi Towers

  • 0

I have a Prolog file (Hanoi.pl) containing the code for solving the Hanoi Towers puzzle:

hanoi( N ):-
    move( N, left, middle, right ).

move( 0, _, _, _ ):-
    !.

move( N, A, B, C ):-
    M is N-1,
    move( M, A, C, B ),
    inform( A, B ),
    move( M, C, B, A ).

inform( X, Y ):-
    write( 'move a disk from ' ),
write( X ),
write( ' to ' ),
writeln( Y ).

I also have a C++ file written in VS2008 IDE:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include "SWI-cpp.h"
#include "SWI-Prolog.h"

predicate_t phanoi;
term_t t0;

int main(int argc, char** argv)
{
    long n = 5;
    int rval;

    if ( !PL_initialise(1, argv) )
        PL_halt(1);

    PL_put_integer( t0, n );

    phanoi = PL_predicate( "hanoi", 1, NULL );

    rval = PL_call_predicate( NULL, PL_Q_NORMAL, phanoi, t0 );

    system( "PAUSE" );
}

How can I consult my Prolog source code (Hanoi.pl) from within my C++ code? Not from the Command Prompt – from the code, something like include or consult or compile? It is located in the same folder as my cpp file.

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:03:32+00:00Added an answer on May 14, 2026 at 4:03 am

    My Prolog Source Code (hanoi.pl)

    hanoi( N ):- 
        move( N, left, middle, right ).
    
    move( 0, _, _, _ ):- 
        !.
    
    move( N, A, B, C ):- 
        M is N-1, 
        move( M, A, C, B ), 
        inform( A, B ), 
        move( M, C, B, A ).
    
    inform( X, Y ):- 
        write( 'move a disk from ' ), 
        write( X ), 
        write( ' to ' ), 
        writeln( Y ). 
    

    My C++ Code (written in VS2008 IDE)

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdafx.h>
    using namespace std;
    #include "Windows.h"
    #include "ctype.h"
    #include "SWI-cpp.h"
    #include "SWI-Prolog.h"
    #include "SWI-Stream.h"
    
    term_t t;
    predicate_t p;
    
    int main( int argc, char** argv )
    {
        argc = 4;
    
        argv[0] = "libpl.dll";
        argv[1] = "-G32m";
        argv[2] = "-L32m";
        argv[3] = "-T32m";
    
        PL_initialise(argc, argv);
    
        if ( !PL_initialise(argc, argv) )
            PL_halt(1);
    
        PlCall( "consult(swi('plwin.rc'))" );
        PlCall( "consult('hanoi.pl')" );
    
        int rval;
        long n = 3;
        PL_put_integer( t, n );
        p = PL_predicate( "hanoi", 1, NULL );
        rval = PL_call_predicate( NULL, PL_Q_NORMAL, p, t );
    
        PL_halt( PL_toplevel() ? 0 : 1 );
    }
    

    Just press F5 in VS2008 to build and compile and run!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you guys had any experiences (positive or negative) by placing your source code/solution
Have had to write my first proper multithreaded coded recently, and realised just how
I am new to Prolog and I have so far learned how to define
I have written a lexer and a parser in Prolog. It unifies a string
I'm diving into the world of prolog headfirst but I seem to have hit
I have been learning prolog.. I'm using a editor named prol1.1.1 I need to
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.