-4

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.

NotSanley
  • 1
  • 2
  • 5
    Post the relevant code here, clearly indicating the line that your debugger says causes the error, as well as the values of relevant variables if known. – Neil Kirk Nov 12 '15 at 05:38
  • do some initial investigation on your end and post a snippet of code in question. The added link has 100's of lines of code. – Nandu Nov 12 '15 at 05:41
  • which line of code gives you access violation? – Nandu Nov 12 '15 at 05:45
  • the first strcmp call – NotSanley Nov 12 '15 at 05:46
  • strcmp(word1, NULL); why with NULL ? – marcinj Nov 12 '15 at 05:48
  • 2
    You're giving NULL to strcmp and wondering by you get a null reference error? – Sami Kuhmonen Nov 12 '15 at 05:48
  • I am just comapring it to null, but i see how that could raise an error... – NotSanley Nov 12 '15 at 05:52
  • New code, but still throws an acces vioaltion at line 88 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(); } – NotSanley Nov 12 '15 at 05:54

3 Answers3

1

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

Community
  • 1
  • 1
Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
0
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, " ,.-"));

NotSanley
  • 1
  • 2
0

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 e.g. 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}, ... };    
AndersK
  • 35,813
  • 6
  • 60
  • 86