I am writing a program in which you can ask a question, and then if the phrase is jknown to the computer, it will ouput an answer, but I was wondering is there a faster way to the way I am doing it, because at the moment i am just using a whole lot of ifs, which is quite a slow process.
Here is my code:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <tchar.h>
using namespace std;

int test()
{
    cout << ":::";
    return 0;
}
int main()
{
    bool exit = false;
    while(exit == false)
    {
        int pos = 11;
    cout << "Welcome to Virtual Chat, ask any question you want .\n" << "(" << pos << ")";
    int Keep = 1;
    string Input;
    while (Keep == 1)
    {
        getline(cin, Input);
        if (Input.compare("b")==0 || Input.compare("B")==0 || Input.compare("B")==0)
        {
            cout << "Second Letter in the Alphabet\n Rhymes with 'Bee'";
            test();

        }
        if (Input.compare("a")==0 || Input.compare("A")==0 || Input.compare("A")==0)
        {
            cout << "The first letter in the alphabet\n Vowel \n";
            test();

        }

        if (Input.compare("Binary")==0 || Input.compare("binary")==0 || Input.compare("What is Binary?")==0)
        {
            cout << "Binary Code is the language of which computers use, using 0's and 1's\n";
            test();

        }
        if (Input.compare("Why?")==0 || Input.compare("why")==0 || Input.compare("Why")==0)
        {
            cout << "Why what?\n";
            test();

        }

        if (Input.compare("Do you hate me?")==0 || Input.compare("do you hate me")==0 || Input.compare("Do you hate me?")==0)
        {
            cout << "Hard to say\n";
            test();

        }
        if (Input.compare("Do you like me?")==0 || Input.compare("do you like me")==0 || Input.compare("Do you like me?")==0)
        {
            cout << "Hard to say\n";
            test();

        }
        if (Input.compare("Hello")==0 || Input.compare("hello")==0 || Input.compare("Hi")==0)
        {
            cout << "Hello!\n";
            test();

        }
        if (Input.compare("What is your name?")==0 || Input.compare("what is your name?")==0 || Input.compare("What is your name")==0)
        {
            cout << "I am a computer, I do not have a Name\n";
            test();

        }
        if (Input.compare("Where do you live?")==0 || Input.compare("where do you live?")==0 || Input.compare("Where do you live")==0)
        {
            cout << "I am a computer, I Live where you live\n";
            test();

        }
        if (Input.compare("How old are you?")==0 || Input.compare("how old are you?")==0 || Input.compare("How old are you")==0)
        {
            cout << "I am a computer, I am not a human being\n";
            test();

        }
        if (Input.compare("Are you smart?")==0 || Input.compare("are you smart?")==0 || Input.compare("Are you smart")==0)
        {
            cout << "No, I am not\n";
            test();

        }
        if (Input.compare("Are you stupid?")==0 || Input.compare("are you stupid?")==0 || Input.compare("Are you stupid")==0)
        {
            cout << "Yes, I am\n";
            test();

        }
         if (Input.compare("Why Are you stupid?")==0 || Input.compare("Why are you stupid?")==0 || Input.compare("Why Are you stupid")==0)
        {
            cout << "Because I am a computer\n";
            test();

        }
         if (Input.compare("Help")==0 || Input.compare("help")==0 || Input.compare("Help!")==0)
        {
            cout << "\nType in a question or phrase, or even a word, and if it is stored in the program, a suitable asnwer or decription will be released.\nIf the input is not recognised by the computer, nothing will happen, and the computer will wait for the next input\n Current Commands: \n1)Help,help,Help! = Get Some help on using virtual chat.\n2)End = End Virtual Chat ";
            test();
        }
        if (Input == "End") return 0;
    }


        continue;
    }
    char f;
    cin >> f;
    return 0;
}

Recommended Answers

All 6 Replies

Suggestion #1: Do your compares case-insensitive so that binary, Binary, BINARY, and bInArY use one single compare.
Suggestion #2: Put all the questions (and answers) in a file. Have the questions sorted alphabetically and read them into arrays. Then look up how to do a binary search. That will make the program extremely fast.
Suggestion #3: Get rid of conio.h -- you do't need it and should not use it. It's non-standard. out of all the compilers available, only a couple mavericks support it, and even they support it differently

How many supported phrases do you ultimately want? If it's only a handful like in the example program, even chains of if statements will be more than fast enough. Walt's suggestion of binary search is the next step, followed by perhaps a binary search tree.

If you want a lot of phrases then something better suited to textual search would be a good idea, in which case I'd recommend looking into a trie. The reason I suggest this only for a large number of phrases is because the setup and constant overhead for a trie can be overwhelming on smaller collections even though the search complexity is excellent.

Okay I will do the Binat=ry thing. Thankyou for the help! :)

i think you could use a
std::map<std::string,std::string> _q;

Look at his code. Is he anywhere near understanding std::map when he can barely understand IFs?

You don't really need to understand how std::map works in order to understand how to use it.
I have an idea. Let's ask the OP which version (s)he finds easier to implement / understand.
This one -> http://ideone.com/ojv1H ? Or this one -> http://ideone.com/Vz9oh ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.