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?
Re: outping HTML from within C++
Well, you're just printing to standard output, so yes, that certainly can go inside a switch statement.
Re: outping HTML from within C++
Hey if you really want to learn then Google it Buddy u'll get better answer
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;
}
}
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.
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.
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;
}
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.
Re: outping HTML from within C++
Quote:
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
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;
Re: outping HTML from within C++
Quote:
Originally Posted by
Paul McKenzie
const char* mathops = "+-*/m";
...
if ( mFunc >= 0 && mFunc < sizeof(mathops)-1)
Regards,
Paul McKenzie
You sure?
Re: outping HTML from within C++
Quote:
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