-
May 3rd, 2013, 08:34 AM
#1
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?
-
May 3rd, 2013, 09:14 AM
#2
Re: outping HTML from within C++
Well, you're just printing to standard output, so yes, that certainly can go inside a switch statement.
-
May 3rd, 2013, 09:21 AM
#3
Re: outping HTML from within C++
Hey if you really want to learn then Google it Buddy u'll get better answer
-
May 3rd, 2013, 09:25 AM
#4
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;
}
}
-
May 3rd, 2013, 09:30 AM
#5
Re: outping HTML from within C++
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.
-
May 3rd, 2013, 09:43 AM
#6
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.
-
May 3rd, 2013, 10:17 AM
#7
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;
}
-
May 3rd, 2013, 10:30 AM
#8
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.
-
May 3rd, 2013, 02:46 PM
#9
Re: outping HTML from within C++
Originally Posted by tmcfadden
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.
-
May 3rd, 2013, 03:48 PM
#10
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)
-
May 3rd, 2013, 06:08 PM
#11
Re: outping HTML from within C++
Originally Posted by Paul McKenzie
const char* mathops = "+-*/m";
...
if ( mFunc >= 0 && mFunc < sizeof(mathops)-1)
Regards,
Paul McKenzie
You sure?
-
May 4th, 2013, 09:04 AM
#12
Re: outping HTML from within C++
Originally Posted by GCDEF
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|