hello, i have some basic question about c++, don't worry, i am a newbie to it so i think the question is not difficult.
i would like to ask how to write the c++ code to analyze the msg input by the user ( i mean how to get the part of message that input by user )
my English is not good, what i mean is, for example, when the program execute, it will ask "what do you want to do?" , this is easy, by "cout" is ok, and it follows by the user freely to input something, for example, if he inputs : "drink tea", then it follows by the program could understand that his command is "drink", and then follow by a particular set of codes that handle with such command, or if he enters "eat cake", then it will execute another set of function, or if he just type in something that is meaningless like "dskfjdkff tea", then the set of code can know that such "dskfjdkff " is not a command and may be something invalid.
This is what i want, i have tried using char array, but it always goes to lots of error like "segmentation fault" (i cannot understand what it is after searching the web for days), i have tried using string, but it still seems very difficult to use, somebody suggest me using getline() function, but i don't know how to use
I hope someone would understand my question and help me, thank you very much
I personally would use the string since you only need to check the whole word rather than each letter in the word. There are a lot of tutorials out there, but the basic idea is to use getline() to read the whole sentence then you can use substring or stringtokens to split words from the sentence.
EDIT: My personal suggestion is that if you just start learning these things, don't just try to write the whole program at once. Try testing simple one(s) first to make sure you get the specific part(s) to work correctly, then you can expand or combine with other simple parts.
Last edited by MPD Psycho; 12-05-2007 at 01:46 PM.
Sounds like you might have done
char *s;
then tried to input to where s points, causing a seg fault.
Try this:
char buff[100];
cin.getline(buff,99);
This will read the response of the user into a char array called buff.
It has been pre-allocated to 100 bytes of storage, and this will contain the users entire response (if not more than 100 characters).
Unlike cin >> buff, which stops on the first white space, getline will read ALL characters up to the end of line.
Try using a combination of scanf and strcmp. That way, you can use strcmp or whatever you want to run through a database and execute the appropriate action. For example:
[HIGHLIGHT="C"]#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h> //only needed for system() in this program
void main(void)
{
char nounBuffer[32];
char verbBuffer[32];
while(strcmp(verbBuffer,"exit") != 0)
{
printf("What do you want to do? ");
scanf("%31s %31s",verbBuffer,nounBuffer);
//Here is where you need to compare the verb string to the
//verbs you can handle, like "eat" or "drink", etc.
//When you're done, continue here:
//clear buffers
memset(nounBuffer,NULL,32);
memset(verbBuffer,NULL,32);
system("cls"); //clears the screen, remove if you want
}
}[/HIGHLIGHT]
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks