C++ programming

You can make programs in C++ to run on this server. The program is called through the CGI interface, for example from a form.

Include the header file cgi_in.h in the beginning of your C++ program to get easy access to the CGI interface.

Transfer your C++ program to your public_html directory on the server by FTP. The C++ program must be compiled on the server: Log in by Secure Shell. Write cd public_html to go to your public_html directory. Write ls if you want to see the contents of the directory.

Then write compile XXX, where XXX.cpp is the name of your C++ program. This will produce an executable program with the name XXX.cgi.

Example 1

C++ program:

#include <cgi_in.h>     // define cgi interface

void main ( )
   {
   // output HTML code
   cout << "<html><body>\n";
   cout << "<h2>Hello ";

   // output name of visitor
   cout << cgi.GetString("visitor");

   // end of HTML document
   cout << "</h2></body></html>";
   }

This program assumes that there is an input parameter named "visitor" which contains the name of a person to say hello to. The output from the program is HTML code.

Try to write this program, call it hello.cpp, and compile it on the server as described above. Call it from your web browser as http://www.eit.ihk-edu.dk/YOURID/hello.cgi?visitor=Whoever, where YOURID is your id (student number).

The program will then reply 'Hello Whoever'.

The program gets more interesting when you call it from a form:

HTML code for form:

<form method="POST" action="hello.cgi">
   Please enter your name <input type="text" name="visitor" size="30"><br>
   <input type="submit" value="OK">
</form>

 This form will look like this:

Please enter your name

If you write Mickey Mouse in the name field and press OK, then the program will answer Hello Mickey Mouse. Try it!

Example 2

This example is a simple pocket calculator

First operand:
Second operand:
+
-
*
/

The program has three input parameters: the first operand named a, the second operand named b, and the operator, named opera, which is 1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division.

C++ code:

#include <cgi_in.h>                // define cgi interface

void main ( ) {
   float a, b;                     // operands
   float result;                   // result
   int opera;                      // 1 = +, 2 = -, 3 = *, 4 = /
   char OperatorNames[] = "+-*/";  // names of the four operators

   a = cgi.GetFloat("a");          // get value from input field "a"
   b = cgi.GetFloat("b");          // get value from input field "b"
   opera = cgi.GetInt("opera");    // get operator from input field "opera"

   switch (opera) {                // select operation to do
   case 1:                         // add
      result = a+b;
      break;
   case 2:                         // subtract
      result = a-b;
      break;
   case 3:                         // multiply
      result = a*b;
      break;
   case 4:                         // divide
      if (b != 0) {                // avoid division by zero
         result = a/b;}
      else {
         opera = 0;}
      break;
   }
   
   // output HTML code
   cout << "<html><head><title>Calculator Result</title></head>\n";
   cout << "<h1>Calculator Result</h1>";
   if (opera > 0 && opera < 5) {
      cout << a << " " << OperatorNames[opera-1] << " " << b << " = " << result;}
   else {
      cout << "Not defined";}

   cout << "</body></html>";}

HTML code for form:

<form action="calc.cgi" method="POST">
   First operand: <input type="text" size="20" name="a"><br>
   Second operand: <input type="text" size="20" name="b"><br>
   <input type="radio" checked name="opera" value="1"> +<br>
   <input type="radio" name="opera" value="2"> -<br>
   <input type="radio" name="opera" value="3"> *<br>
   <input type="radio" name="opera" value="4"> /<br>
   <input type="submit" value="Do it!"> 
   <input type="reset" value="Clear fields">
</form> 

Finding errors

These are methods to find errors if your program doesn't work:

Test your form
First, you must test if your HTML form works correctly. Insert action="/cgi-bin/test.cgi" in the <FORM > tag instead of your own program and see if the form gives the correct parameters. Note that parameter names are case sensitive.
 
Syntax errors
The compile command only gives output in case of errors or warnings. An error message may look like this:
calc.cpp:25: `upera' undeclared
This means that there is an error in the file calc.cpp on line 25. In this case it is a misspelled variable name.
You may use the C++ compiler on your PC to check for syntax errors. You have to copy the header file cgi_in.h to your PC first.
 
Is your program in the right directory?
It is recommended that you put your HTML document and your CGI program in the same directory. If not, then you need to include the path to the CGI program.
 
Have you compiled the latest version of your C++ program?
Every time you have modified your C++ file you must transfer it to the server by FTP and compile it again with the compile command. If you forget this then you will be running an old version of the program.
 
Internal Server Error
If you get an Internal Server Error message then there has been an error when executing your program. Make sure you include cgi_in.h in your program before you output anything. Try to run the program on a PC to see if you get division by zero, overflow, go outside array bounds, or whatever.
 
Testing the program on the PC
If you want to test your C++ program on a PC then you should be aware that you have no CGI interface on the PC (unless it has a web server). Copy the header file cgi_in.h to your PC. This file is programmed to read the input from a file named input.txt if it is not compiled on a UNIX system. Make a file named input.txt containing the input parameters for your program in the form
parameter1=value1
parameter2=value2
Then you should be able to run the program on your PC and see the output as HTML code.
Do not transfer the compiled program (exe file) to the server - it will not work there.

Source code

cgi_in.h  hello.cpp  calc.cpp  compile

This page was last modified 2008-Dec-08