Just started learning...

This is a discussion on Just started learning... within the C++ Programming forums, part of the General Programming Boards category; I've gone through a few of the tutorials here on the sight. I figured with my limited knowledge, I would ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    6

    Just started learning...

    I've gone through a few of the tutorials here on the sight. I figured with my limited knowledge, I would experiment around a little bit and write my own program.

    What I'm trying to do is make a simple "conversation" program.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       string name;
    
       cout << "***Do you know who made this program?: \n";
       cin >> name;
       if (name == "Yes" ||name== "yes" ||name== "yeah" ||name== "ya" ||name== "Yeah" ||name== "yuh huh" ||name== "Ya") {
          cout << "***Good! Let's move on then!\n";
       }
       else if (name == "No" || name == "no" ||name== "nah" ||name== "na" ||name== "Nah" ||name== "nuh uh" ||name== "Na") {
    	cout << "***Let me tell you then! Zac created it!\n";
       }
       else {
    	cout << "***The game.\n";
       }
    
        string theirname;
    
        cout << "\n";
        cout << "***So what is your name? \n ";
        cin >> theirname;
        if (theirname == "Anything") {
            cout << "***Well, nice to meet you, "+theirname+". I am your computer!\n";
        }
        else if (theirname == "Miranda" ||theirname== "miranda") {
           cout << "***Oh, my! It's the love of Zac's life -- Miranda!\n";
        }
        else {
           cout << "***Well, nice to meet you, "+theirname+". I am your computer!\n"; 
        }
    }
    Now I know this may look pretty bad (like I said, I'm just learning on my own). The problem I'm having is in the "if" and "else if" responses. I was trying to cover as many possible inputs as I could think of, which is why there is "yeah," "yuh huh," "Ya," etc.

    The problem arises when one of the inputs has a space in it. The program is reading the second word as the input for the next question. For example, when I do this as a test...

    ***Do you know who made this program?
    yuh huh
    ***The game
    ***Well, nice to meet you, huh. I am your computer!


    So how can I stop it from doing this? It does it for every input that has a space.




    Also, is there a command that makes the program recognize a specific keyword from an input? For instance, if I wanted the program to say the same thing for every string that has the keyword "apples" in it...

    ***What is your favorite food?
    I like apples!
    ***I like apples too!

    or

    ***What is your favorite food?
    I enjoy consuming copious amounts of apples!
    ***I like apples too!



    Thanks for any help in advance!

    Edit: And if it makes any difference, I'm using Code Blocks and the GNU GCC compiler.
    Last edited by Ignoramus; 12-07-2009 at 08:24 PM.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Code:
    char name[100];
    cin>> name; // this will only accept up to 100 continuous characters
                // a space is read as a separate input if you were to do something like the following...
    
    int x, y, z;
    cout<<"gimme x y z: ";
    cin>> x >> y >> z
    // if you typed "5 4 6" it would assign x = 5, y = 4, z = 6
    You, however, want to get the entire line.
    Code:
    char name[100];
    cin.ignore(); // this gets rid of any return characters left in the input buffer that
                // would cause the next line to be skipped
    cin.getline(name, 100); // this reads in everything until a return is entered, including spaces
    edit: i think you have to include <string>

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    Alright, so how would I implement that...? Sorry, I'm just not quite sure what I would replace in my original code with the

    Code:
    char name[100];
    cin.ignore(); 
    cin.getline(name, 100);
    Thanks for the quick reply.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    You need to do some research on recursive descent parsing.
    Recursive descent parser - Wikipedia, the free encyclopedia

    Or, at the very least, some form of Backus Naur form.
    http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    Uhhh, sorry... You just dunked my head completely underwater :\

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Sorry. You are trying to write a response for answers at the word level without having any form for your commands, responses, etc. While this may work for small stuff you will find it nearly impossible to use for large scale interpretation of commands.

    Check out the links I posted and read through them. I think you will find them helpful. It will at least give you some idea of how to do it. Once you get that then you can utilize some third party tools and libraries that make this sort of thing a snap.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    Alright, thanks! I'll give it a good read, and post if I need some extra help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is learning C/C++ worth it?
    By C/C++ Learner in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 11-05-2008, 02:55 PM
  2. Best Approach for Learning
    By UCnLA in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 02:35 AM
  3. Advice for learning C without a teacher
    By tvsinesperanto in forum C Programming
    Replies: 10
    Last Post: 03-16-2006, 03:05 PM
  4. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  5. Need help getting started
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2001, 11:08 PM

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