2015-11-12 189 views
-4

我正在創建一個帶有簡單文本解析器的RPG,並且我仍然在編寫文本解析器中存在違規的acces時遇到問題。這是我到目前爲止的代碼:0xC0000005:訪問衝突讀取位置0x00000000。 C++ vs2015

/* 
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() 

我乳寧進在文本分析器功能的麻煩。

+5

郵政相關的代碼在這裏,清楚地表明你的調試器說,如果知道的話會導致錯誤,以及相關變量的值就行了。 –

+0

對您的結局做一些初步調查併發布相關代碼片段。添加的鏈接有100行代碼。 – Nandu

+0

修正了這個問題 – NotSanley

回答

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(); 

}

仍然在 的strcpy引發accces衝突錯誤(WORD1,strtok的(NULL, 「.-」));

0

一些評論您的(前)TL; DR KOD上市


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

句話:不使用flushall() 見http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392 而不是僅僅有一個循環,例如讀取緩衝區使用fgetc() 或使用替代方法 - 請參閱下一條評論。

句話:有輸入更多的控制,使用fgets和 然後sscanf的代替,把在一個單獨的函數


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

句話:這是不尋常的,do { ... } while (!correctI)會更清楚。


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

句話:檢查字詞1是否爲NULL做,而不是

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"); 
... 

備註:可代替寫成

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

你正在用C寫C,而不是C++。

C++通過在它們之上構建瘦抽象來避免許多指針問題。

例如,您可以使用std::string而不是使用char* C風格的陣列。因此,而不是不安全

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

你會得到

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

沒有的

strcmp(word1, NULL) 

模擬,因爲它是沒有意義的比較字符串以空指針(而這是不允許的在C)。

您可能想比較一個空字符串:使用文字""

請注意,您在if (cray = 0)(與上面的代碼相比)中也有錯誤。

此外,無論何時你有一個錯誤,你都不應該立即在最近的論壇或StackOverflow上發佈你的代碼。相反,您需要在調試器下啓動您的應用程序,並嘗試自行找出問題。這樣你會更好地理解你的代碼。

我認爲,在嘗試編寫更多C++和用[C++]標記更多問題之前,您應該選擇一些關於該主題的好書。這對您和SO社區都會更有成效。 SO對此有一個很好的職位:

The Definitive C++ Book Guide and List

相關問題