|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 |
| Sponsored Links |
|
|
|
|||
|
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:
C Code:
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to write the code to delete the whole binary search tree ?? | worried_student | C and C++ | 1 | 11-23-2007 10:09 AM |
| How to write a Simulator for SML code | debug | C and C++ | 3 | 10-30-2007 11:14 AM |
| How to write such code??? | stephen0606 | C and C++ | 5 | 10-10-2007 08:24 PM |
| Please Help With A C Program!! | siren | C and C++ | 7 | 04-17-2007 08:45 AM |
Goal: 100,000 Posts
Complete: 68%