Smarterchild

This is a discussion on Smarterchild within the C++ Programming forums, part of the General Programming Boards category; I just had a crazy idea to make a simple smarterchild type talking robot. All it does is remember your ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Smarterchild

    I just had a crazy idea to make a simple smarterchild type talking robot. All it does is remember your name and say hello...stuff like that...and then you can ask Do you know....and he will search his Database for any knowledge of that topic and if he doesn't know then he asks you to tell him so he can recall the knowledge. I think that will require the use of file i/o....any helpful hints on coding?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by bumfluff
    ....any helpful hints on coding?
    Start doing it... ?

    What kind of hints do you want? You know what you want to make and you have an idea on how you want to make it. So get where you can and when you run into an issue, tell us about it. What you told us is so general, I wouldn't know where to begin with hints. Be efficient... indent... be modular... I don't know what else to say.

    Oh... and give it X-ray vision and the strength of five gorillas.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Yeah, I am in a very blank mood today!

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
          string question = "";
          string response = "";
          string subject = "";
          string file = "";
          string name = "";
          string help = "";
          int keepgoing = 0;
    
          ifstream a_file ( "name.txt" );
    
          if ( !a_file.is_open() ) {
          cout<<"Hello. I have never met you before."<<endl;
          cout<<endl;
          cout<<"What is your name?"<<endl;
          cout<<endl;
          cout<<"Type your name: ";
          getline (cin, name);
          cout<<endl;
          ofstream b_file ( "name.txt" );
          b_file<<name;
          b_file.close();
          cout<<"As you are new, would you like to find out how I work?"<<endl;
          cout<<name<<"'s says: ";
          getline (cin, help);
                  if ( help == "no") {
                  help == "yes";
                  }
                  else {
                  help == "no";
                  }
          }
          else {
          cout<<"Hello "<<name<<". Nice to see you again!"<<endl;
          cout<<endl;
          cout<<"Do you still remember how I work?"<<endl;
          cout<<endl;
          cout<<name<<"'s says: ";
          getline (cin, help);
                  if ( help == "yes" ) {
                  help == "yes";
                  }
                  else {
                  help == "no";
                  }
          }
    
          if ( help == "no" ) {
          system("CLS");
          cout<<"I am the greatest revelation in computing technology since"<<endl;
          cout<<"the the invention of Microsoft DOS.  You can ask me different"<<endl;
          cout<<"questions.  However, as I am in the early stages of deveolment"<<endl;
          cout<<"I can only understand do you know and who are you questions."<<endl;
          cout<<endl;
          cout<<"For an answer to a do you know question type in 'do you know?'"<<endl;
          cout<<"I will reply and then you should type in the subject that you"<<endl;
          cout<<"would like for me to tell you about.  If I do not know then"<<endl;
          cout<<"you can tell me what it is so I will know in the future."<<endl;
          cout<<endl;
          cout<<"The other question you can ask me is who I am, simply type"<<endl;
          cout<<"'who are you' and I will tell you."<<endl;
          cout<<endl;
          cout<<"And remember, always type in lower case!"<<endl;
          cout<<endl;
          cout<<"Press enter when you are done.";
          cin.get();
          endl;
          system("CLS");
          cout<<"Now that you're ready to talk to me, what do you want to ask?"<<endl;
          }
          else {
          cout<<"So, what do you want to talk about?"<<endl;
          }
    do {
          cout<<endl;
          cout<<name<<"'s says: ";
          getline (cin, question);
          cout<<endl;
    
             if ( question == "do you know?" ) {
             cout<<"What do you want to know?"<<endl;
             cout<<endl;
             cout<<name<<"'s says: ";
             getline (cin, subject);
             endl;
             file = subject + ".txt";
             ifstream c_file ( file.c_str() );
                   if (!c_file.is_open()) {
                   cout<<"Sorry I do not know what "<<subject<<" is."<<endl;
                   cout<<endl;
                   cout<<"Please enter a definition of "<<subject<<"."<<endl;
                   cout<<endl;
                   cout<<name<<"'s says: ";
                   getline (cin, response);
                   endl;
                   ofstream d_file ( file.c_str() );
                   d_file<<subject;
                   d_file.close();
                   cout<<endl;
                   cout<<"Thank you! I now know what "<<subject<<" is."<<endl;
                   cout<<endl;
                   cout<<"How about another question!"<<endl;
                   keepgoing = 1;
                   }
                   else {
                   c_file>>response;
                   cout<<response<<endl;
                   cout<<endl;
                   cout<<"There you go! Thats all I know about "<<subject<<"!"<<endl;
                   cout<<endl;
                   cout<<"So let's get on to the next question..."<<endl;
                   keepgoing = 1;
                   }
             else if ( question == "who are you?" ) {
             cout<<"I am Smarterthanchild, the world's only completely"<<endl;
             cout<<"useless talking robot and hopefully there are many"<<endl;
             cout<<"in development"<<endl;
             cout<<endl;
             cout<<"So that's my life story, ask a question now."<<endl;
             keepgoing = 1;
             }
             else {
             cout<<"My grasp of English is so bad that I can't understand that!"<<endl;
             cout<<endl;
             cout<<"Ask me another question and we'll see if I can understand!"<<endl;
             keepgoing = 1;
             }
    } while ( keepgoing = 1 );
    
          return 0;
    }

    It has a problem in the underlined line...parse error...any help?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You have code that looks like this:
    Code:
    if(x) {}
    else {}
    else if(x) {}
    Work on your indentation.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I know that that is where the problem lies but I'mnot sure how to rectify it!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    } while ( keepgoing = 1 );
    ->
    Code:
    } while ( keepgoing == 1 );
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    That doesnt solve my problem though

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You need an extra closing brace (}) before your underlined else.

    Work on your indentation. If it was indented properly you'd see the problem immediately.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Thanks...yes I know my indenting is poor but its a work in progress

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
        do {
            cout<<endl;
            cout<<name<<"'s says: ";
            getline (cin, question);
            cout<<endl;
    
            if ( question == "do you know?" ) {
                cout<<"What do you want to know?"<<endl;
                cout<<endl;
                cout<<name<<"'s says: ";
                getline (cin, subject);
                cout << endl;  /* this used to be "endl" */
                file = subject + ".txt";
                ifstream c_file ( file.c_str() );
                if (!c_file.is_open()) {
                    cout<<"Sorry I do not know what "<<subject<<" is."<<endl;
                    cout<<endl;
                    cout<<"Please enter a definition of "<<subject<<"."<<endl;
                    cout<<endl;
                    cout<<name<<"'s says: ";
                    getline (cin, response);
                    cout << endl;  /* used to be "endl" */
                    ofstream d_file ( file.c_str() );
                    d_file<<subject;
                    d_file.close();
                    cout<<endl;
                    cout<<"Thank you! I now know what "<<subject<<" is."<<endl;
                    cout<<endl;
                    cout<<"How about another question!"<<endl;
                    keepgoing = 1;
                }
                else {
                    c_file>>response;
                    cout<<response<<endl;
                    cout<<endl;
                    cout<<"There you go! Thats all I know about "<<subject<<"!"<<endl;
                    cout<<endl;
                    cout<<"So let's get on to the next question..."<<endl;
                    keepgoing = 1;
                }
    
              /* error */
    
            else if ( question == "who are you?" ) {
                cout<<"I am Smarterthanchild, the world's only completely"<<endl;
                cout<<"useless talking robot and hopefully there are many"<<endl;
                cout<<"in development"<<endl;
                cout<<endl;
                cout<<"So that's my life story, ask a question now."<<endl;
                keepgoing = 1;
            }
            else {
                cout<<"My grasp of English is so bad that I can't understand that!"<<endl;
                cout<<endl;
                cout<<"Ask me another question and we'll see if I can understand!"<<endl;
                keepgoing = 1;
            }
        } while ( keepgoing = 1 );
    Doesn't that look better?

    My changes are in bold.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I haven't made those changes because I don't have time, however I ave a problem where it will only read the first word from the file.

    Code:
    if ( question == "do you know?" ) {
             cout<<"What do you want to know?"<<endl;
             cout<<endl;
             cout<<name<<" says: ";
             getline (cin, subject);
             endl;
             file = subject + ".txt";
             ifstream c_file ( file.c_str() );
                   if (!c_file.is_open()) {
                   cout<<endl;
                   cout<<"Sorry I do not know what "<<subject<<" is."<<endl;
                   cout<<endl;
                   cout<<"Please enter a definition of "<<subject<<"."<<endl;
                   cout<<endl;
                   cout<<name<<" says: ";
                   getline (cin, response);
                   endl;
                   ofstream d_file ( file.c_str() );
                   d_file<<response<<endl;
                   d_file.close();
                   cout<<endl;
                   cout<<"Thank you! I now know what "<<subject<<" is."<<endl;
                   cout<<endl;
                   cout<<"How about another question!"<<endl;
                   keepgoing = 1;
                   }
                   else {
                   cout<<endl;
                   c_file>> response;
                   cout<<response<<endl;
                   cout<<endl;
                   cout<<"There you go! Thats all I know about "<<subject<<"!"<<endl;
                   cout<<endl;
                   cout<<"So let's get on to the next question..."<<endl;
                   keepgoing = 1;
                   }
    its in there somewhere

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It is meant to save the information about the subject to a file and then if you ask about that subject again...it will give the definition..however it only reads the first word og teh definition e.g full definition = a loose fitting t-shirt

    all it will say is

    a

  14. #14
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    ifstream load("file.dat");
    
    load >> string1;
    load >> string2;
    load >> string3; // this will read 3 words with spaces. But you'll have to know the number of words!
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Its an undefined number of words that I will be using

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21