String issues

This is a discussion on String issues within the C++ Programming forums, part of the General Programming Boards category; The following paragraph contains my main problem, though I have four others that I am unable to find help with ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    4

    String issues

    The following paragraph contains my main problem, though I have four others that I am unable to find help with in simple tutorials at the bottom of this post, I will greatly appreciate any help.

    Hello, I'm relatively new to c++ and may therefore may have a few foolish questions while I am attempting to work on a program that is a sort of basic AI interface. I would like to know how to have the program respond to user input that is set to a string variable as an entire sentence, or rather, how to equate an entire sentence with a string. As the user inputs data, I use cin >> my_string to attempt to set the sentence or words input as my_string. Then, when I tell the program to evaluate the string, it has no problem if it is a single word if I have already set this specific character arrangement to a string it should be looking for... If will almost certainly be easier for the problem to be seen in my block of code, which is only a very basic structure at present. it is as follows:
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    int main (int nNumberofArgs, char* pszArgs[])
    {
    //define some strings (present tense)
    //punctuation
    string period = ".";
    string comma = ",";
    string semicolon = ";";
    string sp = " ";
    //greetings (meant to be single line or sentence)
    string Hello = "Hello";
    string hello = "hello";
    string Good_morning = "Good morning";
    string good_morning = "good morning";
    //dismissals
    string Goodbye = "Goodbye";
    string goodbye = "goodbye";
    //articles
    string a = "a";
    //nouns
    string cat = "cat";
    //verbs
    string jump = "jump";
    //sentences (need to determine classes, and how to select from classes)
    //unser input sentences
    string s1;
    string s2;
    string s3;
    string s4;
    string s5;
    string s6;
    string s7;
    string s8;
    string s9;
    string s10;
    //output sentence strings
    string o1 = Hello;//tell to choose random greeting eventually (non-time based)
    string o2;
    string o3;
    string o4;
    string o5;
    string o6;
    string o7;
    string o8;
    string o9;
    string o10;
    // loop ints and such
    int l1 = 0;
    //begin program
    cin >> s1;
    //a newline must not be started until the first cin is reflected upon.
    if (s1==Hello+period||s1==Hello||s1==hello+period||s1==hello)  //if s1 is a greeting, then output nothing additional, otherwise reflect. (move to loop below)
    {
    cout << Hello+period << endl;
    }
    else
    {
    cout << Hello+semicolon;
    }
    //must insert something similar to loop here, to finish else statement evaluation of input
    while ( l1 < 10 ) {// loop stops if l1 is greater than 10
    cin >> s2;
    s3=s2;
    s4=s3;
    s5=s4;
    s6=s5;
    s7=s6;
    s8=s7;
    s9=s8;
    s10=s9;
    if (s3==Goodbye+period||s3==Goodbye||s3==goodbye||s3==goodbye+period) // begin with the most common input type.
    {
    l1 = 11;
    }
    
    else {
    cout << "I am quite sorry, but I am unable to understand your sentence, either\n";
    cout << "because of too great a number of grammatical errors, or because I simply\n";
    cout << "am unable to comprehend the structure or intent of the sentence input.\n";
    }// I now need to move to a whole bunch of if/then statements. inside a loop, these
    }// statements must be able to reflect upon every possible instance of input in
    // reasonably correct english. (FUN!)
    cout << Goodbye+period <<endl;
    
    
    
    system ("PAUSE");
    
    return 0;
    }
    when I input something other than Hello or goodbye if gives me a number of copies of the error sentence corresponding to the number of words.

    I would also like to ask:
    1. are classes able to be refrenced like strings, for example, if I tell the program to respond to the word "animal", and I have a class animal, subclasses dog and cat, would there be any way to take the highest order class, and tell the program to ask which of the sub branches that user wishes to speak about?
    (other than directly writing a response, which cannot automatically update itself as more subclasses are added)
    2.how can I tell the program to create new subclasses based on the input of the user, or is not classes, then strings atleast, for whords which are not already placed into a class (at this point i only want the program to be able to catagorically sort words based on classes I create, or tell it to create)
    3.as for the refrence mentioned in 1, how would I tell the program to respond to a single word input in a sentence, or several words when found togther, not necessarily in the exact same order and syntax as used every time.
    4. How would I tell the program to look for relative symmetry between strings (where a one letter mistake per five words perhaps might seem reasonable in human error, the program only knows exeact symmetry, or none at all, this is usefull in many aspects, but can become (though less than a major concern) a minor annoyance in my particular instance.
    5.where I have s3=s2,s4=s3 and such, should I reverse those? I just realized, that while I meant to keep some eight sentences in a sort of short-term memory, that line of equation may just be immediately putting the value of s2 in all of those variables, and resetting each time the loop makes a...loop. (by reverse I mean to put s10=s9 first, s9=s8, etc.)

    I appreciate the time of anyone who helps to answer any of the preceeding questions.
    Last edited by The_professor; 06-10-2007 at 09:17 PM. Reason: another question.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Use getline to read in an entire sentence.

    1. I don't think so, if I understand your question properly. There are ways to do similar things, but you pretty much always have to update the code whenever a subclass is added.

    2. You cannot create new subclasses based on user input. You should rethink how you want to design your program. You can have the program do different behavior based on the user input, but you have to add that logic in yourself.

    4. You have to program it to do that.

    5. Yes you should reverse those, but if I understand you correctly a better choice would be to have a container of some sort and just add new sentences on to the back (and erase from the front if you'd like). This is what a queue does, and there is already a queue container available for you to use rather than separate variables.

    I'll be honest that I didn't look too closely at your code (it's a little hard to follow at a quick glance), but hopefully that will help you a bit.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Arrays, btw..... Arrays....

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    if I cannot create new sbclasses based on user input, can I rename subclasses based on user input. self-editing code is possible, I know that much, (Hoffstader tells me so =D). It may be terribly difficult to do though, I may have to tell the code to simply add an interger to some variable, and have a loop go through and check the variable, renaming classes depending on the interger value (which maybe corresponds to the numbers of letters of the alphabet).

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    I figured out how to rename classes based on user input: ill type it out before I forget as a refrence.

    I tell the program to store some variable x, then tell the program to change the value of x based on user input, for example, if the user gives input "I would like to speak about dogs" The part of the string "I would like to spaek about" is read, and if a class for dogs is not currently listed as a subclass of animals, I add that section by simply renaming key parts of the program code (string name/definition) based on the part of the string following the trigger portion, in this case: "dogs". I would obviously have to tell the program to ask the user to select from class categories in order to correctly place the new class (rename some dummy class put there for intentional expansion). That should not pose a huge problem. I may also ask the user to provide a definition, which will for now be stored verbatim, until I decided to tell the program to analyze the input sentences more usefully, or rather to work on its rewording skills.

    I sort of lost my train of thought there, excuse me. The variable x comes in, because I tell the program to change a string value connected to the newly entered subject to the value of that entered subject if that variable x (ill call it an int) changes to some specified value (it may serve to delete or edit classes with different values, if my thought process makes its way into the program)

    this brings me to another question though: can a class name also be the definition of a string, or an interger that I would be able to change? I suppose the name does not matter so much as the strings it contains, which should be alterable.

    I may wish to post an updated portion of the code if anyone wishes to look it over and make soem suggestions, It has been changed around from the version above, and works better to a relative extent at this point (though I have certainly not spent the necessary time to do what I wish just yet).
    Last edited by The_professor; 06-11-2007 at 10:29 PM.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    88
    There are two things you need to familiarize yourself with:
    #1. Arrays (as MacGyver suggested)
    #2. The TAB key

    After you you've learned about these, here's a random nitpick for you:
    Code:
    int main (int nNumberofArgs, char* pszArgs[])
    //That's fine if you like writing code that alienates pretty much every other programmer
    //on the face of the planet
    
    int main(int argc, char* argv[])
    > can a class name also be the definition of a string
    Essentially, no. But, with proper design, it really isn't necessary.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Stuff like self-modifying code and creating new classes/subclasses at run-time is probably the wrong way to go.

    You haven't really shown anything of your code you are asking about, but may-be a general class such as Topic could help you. Then the user can create a Topic object (I mean, you'll code that possibility), set a string that represents the theme to "dogs" - for example -, fill in some other members that represent what is known about dogs and push it onto a list of available Topics (which can be saved to a file).

    Code:
    if (key$ == "I would like to speak about" && !topic_database->find(theme)) {
        Topic* temp = new Topic(theme);
        temp->get_more_info();
        topic_database->push_back(temp);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    Quote Originally Posted by anon View Post
    Stuff like self-modifying code and creating new classes/subclasses at run-time is probably the wrong way to go.

    You haven't really shown anything of your code you are asking about, but may-be a general class such as Topic could help you. Then the user can create a Topic object (I mean, you'll code that possibility), set a string that represents the theme to "dogs" - for example -, fill in some other members that represent what is known about dogs and push it onto a list of available Topics (which can be saved to a file).

    Code:
    if (key$ == "I would like to speak about" && !topic_database->find(theme)) {
        Topic* temp = new Topic(theme);
        temp->get_more_info();
        topic_database->push_back(temp);
    }
    yeah, this looks like a much better idea, thanks for that. self-modifying code would be fun, but this certainly would be much less complicated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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