Forum Moderators: open

Message Too Old, No Replies

HTML and C++

A difficult question!

         

yannis27

2:38 pm on Dec 13, 2004 (gmt 0)

10+ Year Member



Let me explain in details what is the case. I have a page with 3 questions wich have 4 answers each (like a quiz). Each answer represents a number from 1 to 10. Lets say that the user answers:
1st q - 2
2nd q - 3
3rd q - 1
Then, I take these answers and using a mathematical equation (which is written in c++) I take a result.

What I want to know: Is there a way to embede the c++ code into the html? Some may say that i should write the equation in Javascript and embede it in html. The problem is that the equations are very complex (gauss curves) and javascript cant handle it. I must use the prepared codes.

Is there a way?

trillianjedi

2:49 pm on Dec 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld yannis27.

You can't embed C++ into a page.

Any reason not to put the calculation part server-side (CGI for example)?

I'm pretty sure that java-script can use 32bit real numbers though (could be wrong). It sounds like your code is just going to be raw maths. Are you sure that the code cannot be ported?

TJ

yannis27

3:23 pm on Dec 13, 2004 (gmt 0)

10+ Year Member



I'm not sure about anything my friend, I'm not even a programmer, I'm a psychologist! What I'm trying to do is to create a program which will do psychological diagnosis. I've gone far enough, concerning the methodology and all the theoritical stuff. I've already designed the page and I'm using the MatLab in order to make the mathematical work. Now I'm trying to "marry" the html pages with the Matlab. Difficult stuff...

Can you point me to a specific page with a code already written in cgi to study and learn how to do it? I want to create a form (thats easy), take the data from the form, input them in the c++ code, in this code all the mathematical work will be done, then take the results from the c++ code and renter them in a specific point in the html where they will be shown (difficult part).

I have the form, the pages, the c++ codes. How am I ever going to "marry" them?

lZakl

8:47 pm on Dec 13, 2004 (gmt 0)

10+ Year Member



What you looking for is called a script. A miniature program executed on the server where the pages reside, and then output to an HTML page. HTML is a markup language, it is not dynamic or interactive, therefor cannot contain 'active' code. The type of pages that you can use, depend on your server's (or host's) setup. For example, if you are using a Windows server, you will most likely be using ASP or PHP. However if you had a UNIX server you would probably use PERL or PHP.

The most common languages for scripting are PERL, PHP, JAVA, and ASP. There are a a few more but these are the most common.

If you understand what C++ is, then you might understand that the code needs to be compiled. When it comes to scripting languages, this is not the case. The 'compiler' ... well ... the best way to put it is that it compiles 'on the fly'. You can open any page in your favorite text editor and actually view and edit that code simply by editing and hitting "save". No need to compile.

These pages work then by taking input from say "index.html" and sending information to say "index.cgi", at which time it's proccessed ON THE SERVER, and finally outputted to html. Thus making the site dynamic.

As an example, if I were to have a page called "index.php", and opened it in a text editor, it would look something like this:

<?
echo "<html>";
echo "<body>";
echo "<H1>Hello World!</H1>;"
echo "</body></html>;"
?>

This would output after proccessing:

<html>
<body>
<H1>Hello World!</H1>
</body></html>

Of course there is no dynamic content in a hello world script, but your proccesses would simply go anywhere between the <? and?> of the PHP page. to reasearch the actual programming, you can google any of the above langauges and hit literally thousands of tutorial sites. I don't know what your setup is, thus my inablility to suggest a language to you for further research. You might try hitting the ASP, PHP, and PERL forums in this site though, they are a wealth of knowledge!

-- Zak

Robin_reala

10:17 pm on Dec 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hang on, why can't you send form data to a compiled program on the server and retrieve the output? I've not done it myself but it's got to be possible surely? No point in recoding his C++ program if it's sitting there already...

lZakl

1:24 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



I suppose it would be possible, but there would have to be a lot of configuration wouldn't there? You'd have to set up an executable to be run FROM an HTML page... How?

There is one tool I have seen called html++, but from the way I understood it, the only part that is keepable is the algorithm he's already got. Now he says he's not a programmer, so which do you think is easiest (consider he'll have to learn html either way):

1) He learn C++, (ask anyone how easy this language is, and they'll tell you to start with something simpler. I've used it off-and-on for 6 years and it still gives me fits sometimes) and learn to include the classes needed, and which ones. And to input his the variables somehow, proccess and format it into a readable html page, and finally direct the browser TO that page output.

2) He learn a scripting language, not have to include any classes unless it's REALLY complicated like shooting off emails & such. He already knows the algorithm so all he has to do is learn the syntax. Create the html doc formatted into a readable page.

Beleive me, if he's not a programmer, it'll be faster to start over for a simple project like this one. I might be able to understand if it were a 15,000 line proccess, but we're talking about 3 questions with four answers a peice. This gives us just 3 numbers we have to deal with (probably just coming up with the mean, or the median?) Hardly Rocket-Science. It'll save a ton of time to just do it in a scripting language, If that's all there is to it...

Considering it's as simple as I understand it, this should be sufficient:

HERE'S THE HTML CALLED 'questions.html'


<html>
<body>
<form method=post action="math.php">
Question 2:<br>
A: <input type="radio" name="input1" value="1">
B: <input type="radio" name="input1" value="2">
C: <input type="radio" name="input1" value="3">
D: <input type="radio" name="input1" value="4">
<br><br>
Question 2:<br>
A: <input type="radio" name="input2" value="1">
B: <input type="radio" name="input2" value="2">
C: <input type="radio" name="input2" value="3">
D: <input type="radio" name="input2" value="4">
<br><br>
Question 3:<br>
A: <input type="radio" name="input3" value="1">
B: <input type="radio" name="input3" value="2">
C: <input type="radio" name="input3" value="3">
D: <input type="radio" name="input3" value="4">
<input type="submit" value="calculate">
</form>
</body></html>

THIS WOULD BE THE PHP FOR THE ABOVE INPUT CALLED 'math.php':


<?
$total = $input1 + $input2 + $input3;
$total = $total / 3;

echo "your total is $total";
?>

P.S. I did not test this, but it looks right, so I think just cutting and pasting to test it should work.

[edited by: lZakl at 1:36 pm (utc) on Dec. 14, 2004]

Robin_reala

1:34 pm on Dec 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, but the equations are more than just adding and dividing, as he said in the first page he's solving Guass curves. Learning a scripting language then rewriting the algorithm in that (and don't forget the debugging time) is going to be hard work.

I suspect the easiest way to do this (although I've never tried it) is going to be using a Perl (or PHP, or whatever) script to pass the form inputs into the program, retrieve the outputs, and format them into HTML data. I just think rewriting the existing C++ code into PHP is more trouble that it's worth.

trillianjedi

1:41 pm on Dec 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How am I ever going to "marry" them?

You could execute the app on the server in realtime, depending on how the app. outputs the result, but it's going to be real messy doing it - and you'll need some PERL "glue" to hold it all together.

I agree that porting to PERL is the obvious solution, but I'm not sure if PERL does C++, or straight C only?

TJ

lZakl

2:14 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



PERL can get a complicated as you need :0)

cpan.uwinnipeg.ca/htdocs/perl/Math/Trig.html

If you understand how to get the Standard Deviation, it shouldn't be too difficult...

Getting output from an executable to a script, that'll be a little tougher. I suppose you could do a 2 stepper. HTML form takes you to first PERL page. PERL writes to text file, calls and starts executable. Executable proccesses aformentioned text file, outputs to ANOTHER text file, button on current PERL page says "go to next page and get your results" next page calls to executable-written txt file. Could be done though... But he'll STILL have to learn a scripting program. Might as well go all the way.. lol

In essence, it would be:

HTML form ---> CGI input ---> file1.txt outputted and math.exe executed ---> Math.exe reads file1.txt and proccesses and then writes file2.txt ---> Current CGI page takes you to NEXT CGI page ---> CGI page 2 reads file2.txt and gives user results...

Robin_reala

3:08 pm on Dec 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, that's basically what I was suggesting, although I'd assumed you could pass the data contained in the first text file directly into the program as arguments.

Red_Eye

3:34 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



What about turning the C++ program into a webservice, You then send the input parameters to the webservice the webservice does the math and returns the answer.

[codeproject.com...]

lZakl

6:33 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Assuming he has M$ Visual Studio, that would be a good way to go... If he doesn’t have it, it’s a bit spendy. He’ll still have to learn to program at least somewhat in C#/C/C++ though... The wizards can only do so much when you have ‘already written code’ you want to ‘insert’ into the webservice I am assuming. This site is pretty interesting though, I’ve had Visual Studio at home for some time, and was unaware that you could do such things. Neat! Though my wife is the VB.net & C# programmer. I wouldn’t even know where to start with that. If I HAD to do what he has to, I would learn I suppose, but I use Mac and my wife uses PC, so... Depending on Doc’s system, this would be a plausible solution.

Captaffy

8:43 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



If he used ASP.NET, couldn't he use C++ as the language then?
Maybe I am wrong, but it was my understanding that with ASP.NET, you could write the actual code in any .NET compatible language (VB.NET, C#, C++, etc.).

Oh yeah, and buying an IDE for a single language tends to cost around $120 CAN, as opposed to the entire Visual Studio package, which was over $1000 last I checked.

Red_Eye

2:46 pm on Dec 15, 2004 (gmt 0)

10+ Year Member



There is a free development IDE called web matrix which can be found at [asp.net...]

I think that you can create webservices with this. Although not completely sure. I use visual studio.