1

谁能帮我?我收到未处理的异常错误“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/html\n\n";
     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)='\0';
            return(0);
    }
4

1 回答 1

1

我认为事情很清楚:

getenv("REQUEST_METHOD") 返回一个 NULL 指针,strcmp 尝试读取它。您应该以编程方式检查环境变量是否存在。

于 2013-05-05T17:09:19.010 回答