简体   繁体   中英

0xC0000005: Access violation reading location 0x00000000. c++ vs2015

I am working on creating an RPG with a simple text parser, and I am continuing to have problems with acces writing violation in the text parser. Here is my code so far:

/*
Author: Michael Norris
Program: Generiquest
Filename: TextParser.cpp
Maintainence Log:
10/28/2015 Created textParser.cpp


Notes:
This text parser is fairly inefficient, but is easier to manage and understand for beginners.

*/



#include <conio.h>
#include <stdio.h>
#include <Windows.h>
#include <time.h>
#include "myheader.h"
#include <iostream>
#include <string.h>


using namespace std;

class Words
{
public:
Words()
{
    word verbs[30];//This is an array of all the verbs
    strcpy(verbs[0].text, "Items"); 
    strcpy(verbs[1].text, "Stats");
    strcpy(verbs[2].text, "Use");
    strcpy(verbs[3].text, "Eat");
    strcpy(verbs[4].text, "Throw");
    strcpy(verbs[5].text, "Drop");
    strcpy(verbs[6].text, "Look");
    strcpy(verbs[7].text, "Move");
    strcpy(verbs[8].text, "Put");
    strcpy(verbs[9].text, "Speak");
    strcpy(verbs[10].text, "Attack");
    strcpy(verbs[11].text, "Go");
    strcpy(verbs[12].text, "Climb");
    strcpy(verbs[13].text, "Open");
    strcpy(verbs[14].text, "Take");
    strcpy(verbs[15].text, "Put");
    strcpy(verbs[16].text, "Kill");
    strcpy(verbs[17].text, "Get");
    strcpy(verbs[18].text, "LOL");

    //End of verb declarations
    for (int ele = 0; ele < 19; ele++)
    {
        verbs[ele].type = verb;
    }

}




};

Words mainWords;
void textParser()
{


    char str[51] = "Test String";
    char test[50] = "";
    char word1[20] = "";
    //char * word2;
    char word3[20] = "";
    char word4[20] = "";
    system("cls");
    scanf("%50[0-9a-zA-Z ]", &test);
    flushall();
    strcpy(word3, strtok(test, " ,.-"));
    int cray;
    for (bool correctI = false; correctI == false;)
    {

        if (word3 != NULL)
        {
            strcpy(word1, strtok(NULL, " ,.-"));
            cray = strcmp(word1, NULL);//Error thrown here
            if (cray != 0)
            {
                strcpy(word4, word1);

            }
        }
        printf("%s", word3);
        printf("%s", word4);
        cray = stricmp(word1, "Items");
        if (cray = 0)
        {
            printf("Success!!");

        }
        else
        {
            printf("Fail");
        }
    }
    _getch();


}

//TODO: use stricmp()

I am runing into trouble in the text parser function.

You are writing C with classes, not C++.

C++ avoids many of the pointer problems by building thin abstractions on top of them.

For example, instead of using char* C-style arrays, you could use std::string . So instead of unsafe

const char* word1 = "Whatever";
cray = stricmp(word1, "Items");
    if (cray == 0) {

you will get

std::string word1 = "Whatever";
    if("Items" == word1) {

There is no analog of

strcmp(word1, NULL)

because it makes no sense to compare string with null pointer (and it is not allowed in C).

You probably want to compare to an empty string: use literal "" .

Notice that you also have a mistake in if (cray = 0) (compare with my code above).

Additionally, whenever you have a bug, you should not post your code immediately on a nearest forum or on StackOverflow. Instead, you need to start your application under debugger and try to find out the issue by yourself. This way you will understand your code better.

I think that before trying to write any more C++ and tagging even more questions with [C++], you should probably pick some good book on that topic. This will be more productive for both, you and SO community. SO have a terrific post on this:

The Definitive C++ Book Guide and List

void textParser()

{

char str[51] = "Test String";
char test[50] = "";
char word1[20] = "";
//char * word2;
char word3[20] = "";
char word4[20] = "";
system("cls");
system("cls");
scanf("%50[0-9a-zA-Z ]", &test);
flushall();
strcpy(word3, strtok(test, " ,.-"));
int cray;
for (bool correctI = false; correctI == false;)
{

    if (word3 != NULL)
    {
        strcpy(word1, strtok(NULL, " ,.-"));
        if (word1 != NULL)
        {
            strcpy(word4, word1);

        }
    }
    printf("%s", word3);
    printf("%s", word4);
    cray = stricmp(word1, "Items");
    if (cray = 0)
    {
        printf("Success!!");

    }
    else
    {
        printf("Fail");
    }
}
_getch();

}

still throws an accces violation error at strcpy(word1, strtok(NULL, " ,.-"));

Some comments to your (former) TL;DR kod listing


scanf("%50[0-9a-zA-Z ]", &test);
flushall();
strcpy(word3, strtok(test, " ,.-"));

remark : do not use flushall() see http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392 instead just have a loop that reads the buffer eg using fgetc() or use an alternative - see next remark.

remark : to have more control of the input, use fgets and then sscanf instead, put that in a separate function


for (bool correctI = false; correctI == false;)         

remark : this is unusual, do { ... } while (!correctI) would be clearer.


strcpy(word1, strtok(NULL, " ,.-"));
cray = strcmp(word1, NULL);
if (cray != 0)
{
  strcpy(word4,word1);
}

remark : to check whether word1 is NULL do instead

char* tok = strtok(NULL, " ,.-");
if (tok != NULL)
{        
  strcpy(word4,tok);
}

word verbs[30];//This is an array of all the verbs
strcpy(verbs[0].text, "Items"); 
strcpy(verbs[1].text, "Stats"); 
...

remark : can be written instead as

word verbs[] = { {"Items", 0}, {"Stats", 1}, ... };    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM