Click here to Skip to main content
15,912,205 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Convert this code to C as I don't have a knowledge of C language syntax.

What I have tried:

C++
#include <iostream>
#include <string.h>
#include <vector>
#include <ctime>
#include<stdlib.h>

const int MAX_RESP = 3;

typedef std::vector<std::string> vstring;

vstring find_match(std::string input);
void copy(char *array[], vstring &v);


typedef struct {
    char *input;
    char *responses[MAX_RESP];
}record;

record KnowledgeBase[] = {
    {"WHAT IS YOUR NAME",
    {"MY NAME IS CHATTERBOT2.",
     "YOU CAN CALL ME CHATTERBOT2.",
     "WHY DO YOU WANT TO KNOW MY NAME?"}
    },

    {"HI",
    {"HI THERE!",
     "HOW ARE YOU?",
     "HI!"}
    },

    {"HOW ARE YOU",
    {"I'M DOING FINE!",
    "I'M DOING WELL AND YOU?",
    "WHY DO YOU WANT TO KNOW HOW AM I DOING?"}
    },

    {"WHO ARE YOU",
    {"I'M AN A.I PROGRAM.",
     "I THINK THAT YOU KNOW WHO I'M.",
     "WHY ARE YOU ASKING?"}
    },

    {"ARE YOU INTELLIGENT",
    {"YES,OFCORSE.",
     "WHAT DO YOU THINK?",
     "ACTUALY,I'M VERY INTELLIGENT!"}
    },

    {"ARE YOU REAL",
    {"DOES THAT QUESTION REALLY MATERS TO YOU?",
     "WHAT DO YOU MEAN BY THAT?",
     "I'M AS REAL AS I CAN BE."}
    }
};

size_t nKnowledgeBaseSize = sizeof(KnowledgeBase)/sizeof(KnowledgeBase[0]);


int main() {
    srand((unsigned) time(NULL));

    std::string sInput = "";
    std::string sResponse = "";

    while(1) {
        std::cout << ">";
        std::getline(std::cin, sInput);
        vstring responses = find_match(sInput);
        if(sInput == "BYE") {
            std::cout << "IT WAS NICE TALKING TO YOU USER, SEE YOU NEXTTIME!" << std::endl;
            break;
        }
        else if(responses.size() == 0)  {
            std::cout << "I'M NOT SURE IF I  UNDERSTAND WHAT YOU  ARE TALKING ABOUT." << std::endl;
        }
        else {
            int nSelection = rand()  % MAX_RESP;
            sResponse =   responses[nSelection]; std::cout << sResponse << std::endl;
        }
    }

    return 0;
}

// make a  search for the  user's input
// inside the database of the program
vstring find_match(std::string  input) {
    vstring result;
    for(int i = 0; i < nKnowledgeBaseSize;  ++i) {
        if(std::string(KnowledgeBase[i].input) == input) {
            copy(KnowledgeBase[i].responses, result);
            return result;
        }
    }
    return result;
}

void copy(char  *array[], vstring &v) {
    for(int i = 0;  i < MAX_RESP; ++i) {
        v.push_back(array[i]);
    }
}
Posted
Updated 8-Apr-21 20:17pm
v2
Comments
Richard MacCutchan 9-Apr-21 5:05am    
"I don't have a knowledge of C language syntax."
Then converting it to C is unlikely to be of any use to you.

Quote:
Convert this code to C as I don't have a knowledge of C language syntax.
No. We are not a code conversion service, and we aren't here to do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

But I'll give you a little advice for free: a good C++ solution that seems to meet your homework question will not produce a good C solution. C++ is a superset of C, and works on a very different basis. It's like buying a Mustang engine for your Fiat 500 and expecting it to slot right in and make a good car ...
 
Share this answer
 
v2
This is not a question of syntax really. You just can't use anything from the standard c++ library. You have to use things from the standard C library like printf and strcmp. You also have to use raw text buffers and pointers. One more thing - the text strings should be declared as "const char *".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900