Expert C++ question in writing keyword questions for NPC's

Started by
23 comments, last by phil05 20 years ago
Here's the code as is. I figure it's more easy to show you than describe it. "offe" is convered wrongly on this board. It's "one" without being copied here.

#include <iostream>char player_name[40];////////////////////////////////////////////////////////////////////////////// Armorervoid armorer_dialog(std::string player_text) {  // Have sword?  if ((player_text.find("sword") != std::string::npos) || 	  (player_text.find("swords") != std::string::npos)) {     std::cout << "\n\"Yes, in fact, I have offe right here...\"\n" << std::endl;  }   // Have shield?  else if ((player_text.find("shield") != std::string::npos) ||			(player_text.find("shields") != std::string::npos)) {     std::cout << "\n\"I think I have offe lying around.\"\n" << std::endl;  }  // Have armor?  else if (player_text.find("armor") != std::string::npos) {     std::cout << "\n\"No, I sell food products. Dumbass...\" *rolls eyes*\n" << std::endl;  }  else {  std::cout << "*sigh* Times are tough." << std::endl;  }  }////////////////////////////////////////////////////////////////////////////int main(int, char **) {  		// Gets Player's Name	std::cout << "Name: ";	std::cin >> player_name;	std::cout << "\n" << std::endl;	std::cin.ignore(1, '\n');	// Introduces Player's Surroundings	std::cout << "The sun shines through the dense woods, making its way offto a small ";	std::cout << "village \nof where you are. You stand near an armorer who greets you warmly.\n" << std::endl;			// Player Input	char buffer[512];  	std::cout << player_name << " -> ";	std::cin.getline(buffer, sizeof(buffer));	armorer_dialog(buffer);  		return 0;}/////////////////////////////////////////////////////////////////////////////   


[edited by - philvaira on March 20, 2004 2:46:11 AM]

[edited by - philvaira on March 20, 2004 2:47:02 AM]
Advertisement
I''m confused. What do you expect to happen? You don''t have any code to do anything else.
Well, I want the conversation to loop til the player says goodbye. I guess that gives me an idea of waht to do..
then add a LOOP

the:

while()
for()
do / while()

statements are the loop constructs in C++ ... you MUST use one (or recurison ... which you don''t want to do here) ... to cause repeating behavior.

for example:

if(x == 5)
printf("hi");

prints "hi" ONCE

bool quitRequested = false;
while(!quitRequested)
{
printf("hi");
}

will just print "hi" forever ...

but if you ADD CODE INSIDE THE WHILE, to set quitRequested to true when the user enters a command like "quit" or "exit" then it will keep running until the type "quit" or "exit" ...
so for your case, place the code which gets the input from the user in a function ... like GetUserInput() or something ... and place the code which responds to the input in another ... then do something like this:


bool quitRequested = false;

PrintInitialMessage();

while(!quitRequested)
{
string userInput = GetUserInput();
quitRequested = RespondToInput(userInput);
}

inside GetUserInput just read the line the user types
inside RepondToInput, do your processing of the line, and compare to words you are looking for, then output the response AND RETURN true if the user typed quit or exit, and false otherwise.

the expanded version is somethng like this:

bool quitRequested = false;

PrintInitialMessage();

while(!quitRequested)
{
string userInput;
userInput = GetUserInput(); // get the next line from the keyboard
StringList inputWords;
split(userInput, inputWords); // splits string into words
NormalizeInPlace(inputWords); // makes all words lower case
string responseString;
quitRequested = GenerateResponseString(responseString, inputWords); // sets responseString, also returns true if quiting, false otherwise
OutputResponse(responseString); // prints the response to the user
}

This topic is closed to new replies.

Advertisement