CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2013
    Posts
    30

    outping HTML from within C++

    I'm working on a CGI application.
    I'm trying to test my input with a switch statement and output the result with html tags to populate a web page

    From within the switch, I've coded as follows:
    HTML Code:
    	 switch(mFunc)
    		{
    	 case 0: 
    		 cout << "<p><b>YOU ENTERED THE FOLLOWING TO BE CALCULATED:</b></p>" "<h2>"<< number1 <<"+" << number2  << "</h2>" << endl;
    		 break;
    	 case 1: 
    		 cout << "You've entered" << number1 <<"-" << number2 << "to be evaluated" << endl;
    		 break;
    I know that I'll need to put this in an html body with a content type as such:
    HTML Code:
    cout << "Content-type: text/html\n\n";
         cout << "<html><body>\n";
    Am I able to do that directly inside of the switch statement?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: outping HTML from within C++

    Well, you're just printing to standard output, so yes, that certainly can go inside a switch statement.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2013
    Location
    hyderabad
    Posts
    3

    Re: outping HTML from within C++

    Hey if you really want to learn then Google it Buddy u'll get better answer

  4. #4
    Join Date
    Feb 2013
    Posts
    30

    Re: outping HTML from within C++

    And that's what I thought.
    However, despite compiling and running correctly, when I select the addition option (which is option or case: 0), nothing is output. on the web page.

    Here's the block of code I'm using to make this happen:
    Is there something you see here that I might have mis-entered?

    NOTE: I've only added the HTML tags to the first case, which is addition.

    HTML Code:
         //   Perform the correct mathematical operation based on the
         //   user specifications
    	 
         if(mFunc == 0)
         {
              answer = number1 + number2; //addition
         }
         else if (mFunc == 1) 
         {
              answer = number1 - number2; //subtraction
         }
         else if (mFunc == 2)
         {
    		 answer = number1 * number2; //multiplication
         }
    	 else if (mFunc == 3)
         {
    		 answer = number1 / number2; //division
    		      
    			 if (number2 != 0){						//make sure the number is not zero (0)
    						answer = number1 / number2;					//divide, if division
    					}else{
    					   strcpy(buffer, "Divide by zero");
    					}			
    				}else{
    					   strcpy(buffer, "Invalid operator");
    	 	
    		 
    	  	 //switch to output response
    	 switch(mFunc)
    		{
    	 case 0: 
    		 cout << "Content-type: text/html\n\n" << "<html><body>\n";
    		 cout << "<p><b>YOU ENTERED THE FOLLOWING TO BE CALCULATED:</b></p>" "<h2>"<< number1 <<"+" << number2  << "</h2>" << endl;
    		 cout << "</body></center></html>\n"; 
    		 break;
    	 case 1: 
    		 cout << "You've entered" << number1 <<"-" << number2 << "to be evaluated" << endl;
    		 break;
         case 2: 
    		 cout << "You've entered" << number1 <<"*" << number2 << "to be evaluated" << endl;
    		 break;
         case 3: 
    		 cout << "You've entered" << number1 <<"/" << number2 << "to be evaluated" << endl;
    		 break;
    	 case 4: 
    		 cout << "You've entered" << number1 <<"m" << number2 << "to be evaluated" << endl;
    		 break;
    	 default:
    		 cout << "You haven't entered any operation" << endl;
    	 
    		}
    	 }

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: outping HTML from within C++

    Quote Originally Posted by tmcfadden
    Here's the block of code I'm using to make this happen:
    Is there something you see here that I might have mis-entered?
    Basically, the problem is that your indentation is poorly done, so it is hard for you to trace the flow of control. Here's my version of your code:
    Code:
    //   Perform the correct mathematical operation based on the
    //   user specifications
    
    if(mFunc == 0)
    {
        answer = number1 + number2; //addition
    }
    else if (mFunc == 1)
    {
        answer = number1 - number2; //subtraction
    }
    else if (mFunc == 2)
    {
        answer = number1 * number2; //multiplication
    }
    else if (mFunc == 3)
    {
        answer = number1 / number2; //division
    
        if (number2 != 0){                         //make sure the number is not zero (0)
            answer = number1 / number2;            //divide, if division
        }else{
            strcpy(buffer, "Divide by zero");
        }
    }
    else
    {
        strcpy(buffer, "Invalid operator");
    
        //switch to output response
        switch(mFunc)
        {
        case 0:
            cout << "Content-type: text/html\n\n" << "<html><body>\n";
            cout << "<p><b>YOU ENTERED THE FOLLOWING TO BE CALCULATED:</b></p>" "<h2>"<< number1 <<"+" << number2  << "</h2>" << endl;
            cout << "</body></center></html>\n";
            break;
        case 1:
            cout << "You've entered" << number1 <<"-" << number2 << "to be evaluated" << endl;
            break;
        case 2:
            cout << "You've entered" << number1 <<"*" << number2 << "to be evaluated" << endl;
            break;
        case 3:
            cout << "You've entered" << number1 <<"/" << number2 << "to be evaluated" << endl;
            break;
        case 4:
            cout << "You've entered" << number1 <<"m" << number2 << "to be evaluated" << endl;
            break;
        default:
            cout << "You haven't entered any operation" << endl;
        }
    }
    Now, it is obvious: if mFunc is equal to 0, then you compute a value that is assigned to answer. The entire block containing the switch is not executed because it is in the final else branch.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: outping HTML from within C++

    How did you determine it was running correctly, when clearly it wasn't? Stepping through the code in the debugger is really the only way to know for sure what your program is doing, and it would have made your problem obvious.

  7. #7
    Join Date
    Feb 2013
    Posts
    30

    Re: outping HTML from within C++

    After copying the .cpp file to the cgi-bin directory of the server, I use the g++ compiler.

    Stepping through the program locally errors out when it hits the GET method because it's got no way to see the server file.

    I understand what laserlight said about the switch statement being in the final else branch.
    Are you saying it has to be between its own brackets like so:

    HTML Code:
     if(mFunc == 0)
         {
              answer = number1 + number2; //addition
         }
         else if (mFunc == 1) 
         {
              answer = number1 - number2; //subtraction
         }
         else if (mFunc == 2)
         {
    		 answer = number1 * number2; //multiplication
         }
    	 else if (mFunc == 3)
         {
    		 answer = number1 / number2; //division
    		      
    			 if (number2 != 0){						//make sure the number is not zero (0)
    						answer = number1 / number2;					//divide, if division
    					}else{
    					   strcpy(buffer, "Divide by zero");
    					}			
    				}else{
    					   strcpy(buffer, "Invalid operator");
    	 }
    	
     //switch to output response
         switch(mFunc)
    		{
         case 0: 
    		 cout << "You've entered" << number1 <<"+" << number2 << "to be evaluated" << endl;
    		 break;
         case 1: 
    		 cout << "You've entered" << number1 <<"-" << number2 << "to be evaluated" << endl;
    		 break;
         case 2: 
    		 cout << "You've entered" << number1 <<"*" << number2 << "to be evaluated" << endl;
    		 break;
         case 3: 
    		 cout << "You've entered" << number1 <<"/" << number2 << "to be evaluated" << endl;
    		 break;
         case 4: 
    		 cout << "You've entered" << number1 <<"m" << number2 << "to be evaluated" << endl;
    		 break;
    	 default:
    		 cout << "You haven't entered any operation" << endl;
    	 
    		}

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: outping HTML from within C++

    Please indent the code properly. I know you're using tabs, not just for indenting but also for pretty alignment. If you find it hard with tabs, just use a fixed number of spaces per indent level. I personally use 4 spaces per indent level.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: outping HTML from within C++

    Quote Originally Posted by tmcfadden View Post
    And that's what I thought.
    However, despite compiling and running correctly, when I select the addition option (which is option or case: 0), nothing is output. on the web page.
    We don't even know if the program crashes.
    Code:
      strcpy(buffer, "Divide by zero");
    How is it declared? If it is not pointing to a buffer big enough to hold all those characters plus the terminating NULL, the program then goes into the world of undefined behaviour, possibly crashing.

    You should declare buffer as std::string instead of using low-level and dangerous 'C'-style string functions. Then there is no need to worry about this problem. Not only that, the assignment becomes "natural" by using =.
    Code:
    #include <string>
    //...
    std::string buffer;
    //...
    buffer = "Divide by zero";
    Second, that second switch need not be a switch:
    Code:
    const char* mathops = "+-*/m";
    //...
    if ( mFunc >= 0 && mFunc < sizeof(mathops)-1)
        cout << "You've entered" << number1 << mathops[mFunc] << number2 << "to be evaluated" << endl;
    else
        cout << "You entered an invalid math operation" << endl;
    That entire switch statement is reduced to a single if() statement plus two lines of code.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 3rd, 2013 at 03:57 PM.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: outping HTML from within C++

    The multiple if statements and the switch can be combined.

    Code:
    const char mathops[] = "+-*/m";
    //...
    if ( mFunc >= 0 && mFunc < sizeof(mathops) - 1) {
        cout << "You've entered" << number1 << mathops[mFunc] << number2 << "to be evaluated" << endl;
        switch (mFunc) {
            case 0:
               answer = number1 + number2; //addition
               break;
    
            case 1:
               answer = number1 - number2; //subtraction
               break;
    
          //.......
         }
         cout << "The answer is " << answer << endl;
    } else
        cout << "You entered an invalid math operation" << endl;
    Last edited by 2kaud; May 4th, 2013 at 03:04 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: outping HTML from within C++

    Quote Originally Posted by Paul McKenzie View Post
    const char* mathops = "+-*/m";
    ...
    if ( mFunc >= 0 && mFunc < sizeof(mathops)-1)

    Regards,

    Paul McKenzie
    You sure?

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: outping HTML from within C++

    Quote Originally Posted by GCDEF View Post
    You sure?
    Yes, you're right. It should be this:
    Code:
    if (mFunc >= 0 && mFunc < sizeof("+-*/m") - 1 )
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 4th, 2013 at 09:12 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured