0

I have a C++ program that generates a random sentence (with a word bank read from an accompanying text file) when it is run from the command line.

I have a personal, low-traffic web page, and I would like to make this program run on the page (e.g., a visitor could press a button and a random sentence from the program would be displayed there).

What, if any way, is the most painless way to achieve this?

I'm a complete novice at web-related applications, but, to my understanding, these kinds of things are carried out with cgi, scripting languages, etc., which run on either the server or the user's machine and come with a lot of considerations about flexibility, feasibility, etc.


EDIT: More details, if that helps:

In particular, I want to be able to do the following things that are easy in C++:

-Read from my own text file (thus, it would be on the server) and store the words I read into array-type structures

-Randomly select words from these arrays

-Assemble different combinations of these words in different ways, which depends on more random number generations

-Print an assembled sentence onto the screen


If still too broad (maybe I just don't know the potential scope of web applications!):

The text file I use in the C++ implementation contains long lists of words that represent different parts of speech (plus a termination character '#'); each list is read into a different vector such as "articles", "nouns", etc. :

a and the ... #

cat dog frog Bob Joe astronaut programmer ... #

ate defeated slapped violated ... #

ran walked jumped ... #

So, along with being able to store things nicely, I'd probably want something that works in as many browsers as possible, but also something that's likely to be allowable on a host that is not my own. Not sure of what all needs to be considered, but learning as I go...

norman
  • 5,128
  • 13
  • 44
  • 75
  • 3
    CGI is certainly the most low tech way of doing this. I would start there. But whoever is hosting your web-site will certainly have something to say about this. – john Oct 13 '13 at 06:42
  • CGI applications can be written in C++, you don't have to use a scripting language if you are more confortable with C++. – john Oct 13 '13 at 06:46
  • CGI is one possibility, but if you are a complete novice at web applications, my gut feeling says that converting a command line program is not the most painless way. Perhaps rewriting the program in Javascript would be easier. – Mr Lister Oct 13 '13 at 07:35
  • I'm thinking Javascript might be the way to go. Figuring out how to read from my local words.dat file will be interesting, but I can at least say that I've achieved "hello, world!" with JS on the web page so far! :-) – norman Oct 15 '13 at 06:09
  • Maybe you'd like to get familiar with the newly-released WebAssembly. Here's a [starting point](https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm) for running a simple c++ program in the browser. – OfirD Apr 07 '17 at 13:30

1 Answers1

4

Hm, I'd use php/javascript for this:

  1. Create php script on the server:

    <?php
    $out = '';
    exec( 'myprogram cmdline', $out );
    echo $out;
    ?>
    
  2. On button click create Ajax request to the script to get new word and show it. I recommend you to use jQuery library. Then your js code will look like this:

    <script>
    
    var showNewWord = function() {
        $.ajax( "myscript.php" ).done(function( msg ) {
            alert( "new word is " + msg );
        });
    }
    
    </script>
    
    ...
    <button onclick="showNewWord()">Show new word</button> 
    
Ivan
  • 2,007
  • 11
  • 15