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 }
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)
parseCommand
string
commandp
size_t
verbCheck
nounCheck
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)