未处理的异常错误执行数学操作
C++ GET Method Unhandled exception error performing mathematical operation
有人能帮帮我吗?我得到一个未处理的异常错误"在program.exe中的0x1027d440 (msvcr100d.dll)未处理的异常:0xC0000005:访问违规读取位置0x00000000。"现在程序实际上并没有执行任何操作。所有的答案都会返回,就好像用户选择了第一个单选按钮一样(我设置了5个单选按钮来选择要使用的方法)。但答案总是为零?我需要改变什么才能得到它所以如果用户选择说。然后是减法?我有它设置的值,但我猜有一个错误的地方。谢谢!
这是我在HTML页面
上的设置<!DOCTYPE HTML PUBLIC "-//SoftQuad Software//DTD HoTMetaL PRO 6.0::19990601::extensions to HTML 4.0//EN" "hmpro6.dtd">
<HTML>
<HEAD>
<TITLE>Calculations</TITLE>
</HEAD>
<BODY>
<P>
<p align="center">
<td>Select which Mathmatical operation you would like to perform:</td>
<BR> </P>
<table border="0" align="center">
<tr>
<td>Enter a number to be calculated:</td>
<td><INPUT TYPE="TEXT" NAME="Number1" SIZE="14" MAXLENGTH="20"></p></td>
</tr>
<tr>
<td>Enter a second number to be calculated:</td>
<td><INPUT TYPE="TEXT" NAME="Number2" SIZE="14" MAXLENGTH="20"></p></td>
</tr>
<tr>
<FORM ACTION="http://localhost:8080/cgi-bin/calc.exe" METHOD=”GET">
<p align="center">
<INPUT TYPE = "RADIO" NAME = "mathOperation" VALUE = "0" ><b>Addition</B>
<BR>
<INPUT TYPE = "RADIO" NAME = "mathOperation" VALUE = "1" ><b> Subtraction</B>
<BR>
<INPUT TYPE = "RADIO" NAME = "mathOperation" VALUE = "2" ><b> Multiplication</B>
<BR>
<INPUT TYPE = "RADIO" NAME = "mathOperation" VALUE = "3" ><b> Division</B>
<BR>
<INPUT TYPE = "RADIO" NAME = "mathOperation" VALUE = "4" ><b> Square Root</B>
<BR>
</TR>
</table>
<p align="center">Enter the number of decimal places you'd like displayed:
<input TYPE="text" NAME="Decimal" size="14" MAXLENGTH="50">
<P ALIGN="CENTER"><INPUT TYPE="SUBMIT" NAME="Submit1" CHECKED="CHECKED"
VALUE="Submit Information"> </P>
<P ALIGN="CENTER"><INPUT TYPE="RESET" NAME="Reset1"
VALUE="Reset Form"></P></FORM> </BODY>
</HTML>
下面是我的代码:
#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Function prototype
int getvar(char *, char *, char *);
int main(int argc, char **argv)
{
int length; // only up to 32767 chars
char buffer[1024]; // Buffer size
char *data; // Input to program pointer
char dest1[10]; // Destination fields
char dest2[10];
char dest3[10];
float number1; // first number
float number2; // second number
int mathOperation; // operation to perform
float answer; // place to store the result
// GET Method
if (strcmp(getenv("REQUEST_METHOD"), "GET") == 0)
{
// CGI input is located in the QUERY_STRING environment variable
strcpy( buffer, getenv("QUERY_STRING"));
}
else
{
// CGI input data is read from standard input
length=atoi(getenv("CONTENT_LENGTH"));
data= (char *) malloc(sizeof(char)*(length+1));
cin.get(data, length);
strcpy(buffer, data);
free (data);
}
// Find the program inputs in the user supplied data
getvar("Number1", dest1,buffer);
getvar("Number2", dest2,buffer);
getvar("MathOperation", dest3,buffer);
// Convert the data to the right data type
number1 = (float) atof(dest1); // The variable number1 contains the first number
number2 = (float) atof(dest2); // The variable number2 contains the second number
mathOperation = atoi(dest3); // The math operation code
// Perform the correct mathematical operation based on the
// user specifications
if(mathOperation == 0) // perform addition
{
answer = number1 + number2;
cout << "<p/>n" "Your addition answer is = " <<(answer)<< "<br/>n";
}
else if (mathOperation == 1) // perform subtraction
{
answer = number1 - number2;
cout << "<p/>n" "Your subtraction answer is = " <<(answer)<< "<br/>n";
}
else if (mathOperation == 2) // perform multiplication
{
answer = number1 * number2;
cout << "<p/>n" "Your answer is = " <<(answer)<< "<br/>n";
}
else if (mathOperation == 3) // perform division
{
answer = number1 / number2;
cout << "<p/>n" "Your answer is = " <<(answer)<< "<br/>n";
}
else if (mathOperation == 4) // perform square root
{
cout << "The square root of " << number1 << " is " << sqrt(number1) << endl;
cout << "The square root of " << number2 << " is " << sqrt(number2) << endl;
}
else
{
// I still need to add additional code.. but.. for now..
}
cout << "Content-type: text/htmlnn";
cout << "<html><body>n";
cout << "</body></html>n";
return 1;
}
//(Include the c++ getvar comment block and code here)
int getvar(char *var, char *dest, char *stream)
{
char *vptr;
int size, i=0, j=0, hex; /* ptr+i to src, ptr+j to dest */
vptr=strstr(stream, var);
if(vptr) ;
else return(1); /* 1 for a checkbox thats off */
if((vptr==stream)||(*(vptr-1)=='&')) ;
else return(-1); /* -1 for a var that appears in error */
size=(int) strlen(var)+1; /* +1 accounts for the = */
while(*(vptr+size+i)!='&')
{
if(*(vptr+size+i)=='+') /* output a space */
*(dest+j)=' ';
else if(*(vptr+size+i)=='%') /* hex character */
{
sscanf(vptr+size+i+1,"%2x",&hex);
*(dest+j)=(char)hex;
i+=2;
}
else *(dest+j)=*(vptr+size+i);
i++; j++;
}
*(dest+j)=' ';
return(0);
}
我认为事情很清楚:
getenv("REQUEST_METHOD")返回一个空指针,strcmp尝试读取它。您应该以编程方式检查环境变量是否存在。
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 重载操作程序时出错>>用于类中的字符串 memebr
- 对字符串进行位操作
- 我可以在 C++ 中的函数体之外进行操作吗?
- MPI突然停止了对多个核心的操作
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 对字符数组中的元素执行逐位操作
- 如何在directx/c++中进行平移/缩放操作
- 逐位操作的隐式类型转换
- 为什么一个向量上的多线程操作很慢
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 位移操作和位掩码未检测到重复字符
- 如何进行特定的位操作?
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 字符串操作 - 字符计数
- 此代码中的操作流程是什么?C/C++.
- 复制和交换习惯用法与移动操作之间的交互
- 像union_这样的 Boost.Geometry 操作如何处理浮点类型的基本不精确性?
- 为什么 std::lerp 不适用于任何已实现所需操作的类型?
- 无法合并生成操作.. 先决条件不同