Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm attempting a parser for a text-based game:
C++
  1  #include <iostream>
  2  #include <list>
  3  #include <string>
  4  #include <algorithm>
  5  #include <bits/stdc++.h>
  6  
  7  
  8  using namespace std;
  9  
 10  
 11  
 12  string input;
 13  string output = " ";
 14  char* commandI;
 15  bool nounCheck2;
 16  bool verbCheck2;
 17  
 18  string parseCommand(char commandp[2]) {
 19      size_t noun;
 20      size_t verb;
 21      list<string> verbs = {"take", "drop"};
 22      list<string> nouns = {"sword", "shield"};
 23      
 24      verb = commandp[0];
 25      noun = commandp[1];
 26      
 27      size_t verbCheck (verb);
 28      if(verbCheck == string::npos) {
 29          cout << "invaild verb" << "\n";
 30      } else {
 31          verbCheck2 = true;
 32      };
 33      size_t nounCheck (noun);
 34      if (nounCheck == string::npos) {
 35          cout << "invaild noun" << "\n";
 36      } else {
 37          nounCheck2 = true;
 38      };
 39      
 40  };
 41  
 42  string parseString(string fInput) {
 43      char delims[2] = {" "}; {".";};
 44      char arr[fInput.length()]; 
 45  	strcpy(arr, fInput.c_str()); 
 46  	for (int i = 0; i < fInput.length(); i++) {
 47  		cout << arr[i]; 
 48  	}
 49      commandI = strtok(arr, delims);
 50      while (commandI != NULL) {
 51          parseCommand(commandI);
 52          if (verbCheck2 && nounCheck2) {
 53              cout << "command vaild" << "\n";
 54          }
 55          commandI = strtok(NULL, delims);
 56      }
 57      return 0;
 58  };  
 59  
 60  int main()
 61  {
 62      do{
 63          getline(cin, input);
 64          parseString(input);
 65  
 66      }while (input != "quit");
 67  
 68      return 0;
 69  }

but everytime I run it, it does this:
main.cpp: In function ‘std::string parseCommand(char*)’:
main.cpp:40:1: warning: no return statement in function returning non-void [-Wreturn-type]
   40 | };
      | ^
take sword
Segmentation fault (core dumped)


It lets me put one input in but then dumps the core. Any help given is appreciated! If you could give any pointers on making this parser, that would also be appreciated.

What I have tried:

Google and various documentation.
Posted
Updated 17-Aug-23 2:01am
v3

1 solution

There are too many problems with your code to even compile it into a valid executable.

On line 18 you define parseCommand as returning a string object, but you never return anything from the function.

Also on line 18 you define commandp as an array of two characters. then on lines 24 and 25 you use them to populate two size_t (i.e. integer) types. You then assign one of the values to another size_t item named verbCheck. You then do the same with nounCheck.

on lines 38 and 40 you have typed semi-colons after the closing braces, which are both incorrect.

The parseString function has similar errors and although you define it as as returning a string object, the only thing it returns is zero.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900