-
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.
-
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>
-
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.
-
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
-
Uhhh, sorry... You just dunked my head completely underwater :\
-
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.
-
Alright, thanks! I'll give it a good read, and post if I need some extra help.