Closed Thread
Results 1 to 4 of 4

Thread: how to write the code for user to input command ?

  1. #1
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    how to write the code for user to input command ?

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    MPD Psycho's Avatar
    MPD Psycho is offline Newbie
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0
    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.

  4. #3
    Freedom Doc is offline Newbie
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0
    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.

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59
    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]

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 10-25-2011, 02:00 PM
  2. CLI/EXEC Command - User Change
    By Neoburner in forum PHP Development
    Replies: 4
    Last Post: 09-07-2011, 03:27 AM
  3. Mips Assembly: Take user input and write to the console
    By morefood2001 in forum Assembly Tutorials
    Replies: 8
    Last Post: 10-20-2010, 08:50 AM
  4. write oracle command
    By royvb in forum C# Programming
    Replies: 0
    Last Post: 03-25-2010, 07:47 PM
  5. Mips Assembly: Take user input and write to the console
    By morefood2001 in forum Tutorials
    Replies: 7
    Last Post: 09-21-2008, 10:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts