Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-05-2007, 03:07 PM
worried_student worried_student is offline
Newbie
 
Join Date: Nov 2007
Posts: 11
Rep Power: 0
worried_student is on a distinguished road
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-05-2007, 03:39 PM
MPD Psycho's Avatar   
MPD Psycho MPD Psycho is offline
Newbie
 
Join Date: Nov 2007
Posts: 20
Rep Power: 4
MPD Psycho is on a distinguished road
Default

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 03:46 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-21-2007, 12:03 PM
Freedom Doc Freedom Doc is offline
Newbie
 
Join Date: Dec 2007
Posts: 8
Rep Power: 0
Freedom Doc is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-26-2007, 08:11 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 509
Last Blog:
Programs Under the Hoo...
Rep Power: 10
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <memory.h>
  4. #include <stdlib.h//only needed for system() in this program
  5.  
  6. void main(void)
  7. {
  8.     char nounBuffer[32];
  9.     char verbBuffer[32];
  10.  
  11.     while(strcmp(verbBuffer,"exit") != 0)
  12.     {
  13.         printf("What do you want to do? ");
  14.         scanf("%31s %31s",verbBuffer,nounBuffer);
  15.         //Here is where you need to compare the verb string to the
  16.         //verbs you can handle, like "eat" or "drink", etc.
  17.         //When you're done, continue here:
  18.  
  19.         //clear buffers
  20.         memset(nounBuffer,NULL,32);
  21.         memset(verbBuffer,NULL,32);
  22.         system("cls"); //clears the screen, remove if you want
  23.     }
  24. }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 09:55 AM.

Contest Stats

Xav ........ 162.68
Sacback ........ 120.05
alearb8 ........ 120.05
amr2107 ........ 120.05
tfusion ........ 120
d3s!gn ........ 120
Qoolman21 ........ 120
Pillager ........ 108
antihero ........ 106
donfrench ........ 100.05

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 68%

Ads