Solved C++ chatbot doesn't respond properly

February 16, 2014 at 16:36:54
Specs: Windows 7, 8GB
This code only responds to hi, why? Specifically how do i fix this?

The code:

#include <iostream>
#include <string>

using namespace std;

string pb[3] = {"hi","how are you","what is your name"};
string rb[3] = {"Hi there","I am fine","My name is Ian"};

void init()
//to be later filled with things.

void respond(const string inp)
int n = sizeof(pb) / sizeof(string);
for(int i = 0; i < n; i++)
cout << rb[i] << "\n";

int main()
int run = 1;
string inp;
while (run == 1)
cin >> inp;
return 0;

See More: C++ chatbot doesnt respond properly

Report •

February 17, 2014 at 01:18:49
✔ Best Answer
When faced with a problem like this you need to do a little debugging. I would suggest that you add the line

cout << inp << endl;

as the first line of your "respond" function. Now run the program, try a few inputs, see what is output and think about why.

Try as inputs:

how are you
how hi you

Inspect the output for each of the 3 lines and see if is what you expect. If not, try to think why it is not what you expected. Then you will understand why your program is not working. Next you have to think about how you can solve that problem; that's another question. If you can't answer it, come back and tell us what your new problem is.

(Sorry if I'm being a little cryptic and not just giving you the answer. I sincerely believe that you will learn more, and become a better programmer, by working through this exercise by yourself than if I just tell you what is wrong.)

Report •

February 17, 2014 at 07:11:23
So the problem seems that it cant respond to inputs with spaces.. how do i fix this?

Report •

February 17, 2014 at 07:25:12
You have correctly identified the problem.

To solve it, look at all the methods available to cin. >> is not the only way to get input to a variable.

message edited by ijack

Report •

Related Solutions

Report •

February 17, 2014 at 07:42:42
Not quite. That method inputs to a char *, not a C++ string. But there is a reference on that page to the correct function to use. (To correct what I said earlier, it's a function rather than a method of cin.)

message edited by ijack

Report •

February 18, 2014 at 05:28:12
Used getline function instead of cin

Report •

Ask Question