Ok, so I decided to try and write a simple chat bot.
To start, I made it so he will scan the string that you input, and see what emotion you have. (this is just the base, eventually he will read verbs and such)

So, here is the code...

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string input;
int i = 0;
bool sad = false;
bool happy = false;
bool mad = false;
int found = 0;

int generalAmm = 7;

string sadwords[8] =
       "im sad ",
       "im very sad ",
       "im sad.",
       "im very sad.",
       "makes me sad ",
       "makes me very sad ",
       "makes me sad.",
       "makes me very sad."

string happywords[8] =
       "im happy ",
       "im very happy ",
       "im happy.",
       "im very happy.",
       "makes me happy ",
       "makes me very happy ",
       "makes me happy.",
       "makes me very happy."

string madwords[8] =
       "im mad ",
       "im very mad ",
       "im mad.",
       "im very mad.",
       "makes me mad ",
       "makes me very mad ",
       "makes me mad.",
       "makes me very mad."

bool findwords(string var)
for(i = input.find(var, 0); i != string::npos; i = input.find(var, i))
if (found >= 1)
          i = 0;
          return true;
i = 0;
return false;

int main(int argc, char *argv[])
sad = false;
happy = false;
mad = false;
found = 0;
cout << ">> ";
getline(cin, input, '\n');
for(int g = 0; g < generalAmm; g++)
happy = findwords(happywords[g]);
for(int g = 0; g < generalAmm; g++)
sad = findwords(sadwords[g]);
for(int g = 0; g < generalAmm; g++)
mad = findwords(madwords[g]);
if (sad == true && happy == false)
cout << "Im sorry your sad.\n";
else if (happy == true && sad == false)
cout << "That's great!\n";
else if (sad == true && happy == true || mad == true && happy == true)
cout << "It sounds like you have mixed emotions!\n";
else if (sad == false && happy == false && mad == true)
cout << "Wow, thats a serious emotion.\n";
goto start;

Now, the weird part is that, it doesn't let you say your happy because it just says "It sounds like you have mixed emotions!"
even though your only happy and I want it to say that if your happy and sad.
Every other emotion works, but not happy.

Why is it doing this?

8 Years
Discussion Span
Last Post by ArkM

- Global variables called i, should (nay, must) be local
- Poor indentation
- Using goto where a while loop is needed.

I'd say you need to work on the basics of programming a while longer.


It seems you have broke all good program design principles. As a result you have this incomprehensible and unstable code.
1. Why input, i and found variables are global ones?! You never use it outside the findwords function (and no need to use it in a such manner).
2. The only reasonal functionality of the findwords function is to report about word occurence in the input string. It's exactly the same as string::find functionality. Moreover, the findwords call loops are wrong: your loops overwrite all findword results except the last one.
3. No need to pass std::string argument by value. As usually the most effective and safe method looks like:

bool findspecial(const string& where, const string& what);

4. It seems you don't know about a safe and clear initialized array size construct:

AnyType a[] = { val0, ... valN };
const int asize = sizeof a/sizeof *a;
for (int i = 0; i < asize; ++i) {

5. I thing it's a rather strange and mannered code:

bool sad, happy;
if (sad == true && happy == false)...
if (sad == true && happy == true || mad == true && happy == true)

Keep it simpler:

if (sad && !happy)...
if (happy && (sad || mad))...

That's why you can't detect logical errors in this code...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.