|
 |
|
 |
 |
05-13-2005, 05:22 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Jan 2005
Posts: 9
|
Kaat a talking bot in c
Hi all, I'm new to c.
This is my first project, a talking bot named Kaat.
There are some stuff I don't know how to do. In order to make the program complete, I put the code here to get some help.
My main problem is the talk function, it will ouput sentences from time to time (because c is so fast, but I don't know how to do that....) and also track the keyboard for a key pressed event.
This is what I call the skeleton:
HTML Code:
/*
KaaT - (Kaa talking program)
Version: 0.0
By Victor NOAGBODJI
2005-05-13
Notes:
- This talking program will focus on the way of talking and answering
Todo:
- complete the program
- add talking attitude (polite, rude, cool ....)
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAXLINE 1000 /* maximum input line size */
/* Prototypes */
void talk (char *sentence_pointer);
char *getline(char *line, int maxline);
/* Globals */
/* About sentences*/
char about[3];
about[0][] = "My name is KaaT, version 0.0";
about[1][] = "Victor is my creator.";
about[2][] = "I don't know my age yet.";
/* Special sentences, that conduct the program */
char specials[6];
specials[0][] = "So, I was saying that";
specials[1][] = "Are you going to say something?";
specials[2][] = "Can I finish please?";
specials[3][] = "I was going to say that";
specials[4][] = "I've forgotten what I was saying.";
specials[5][] = "Maybe I talk too much";
/* Some subjects sentences */
int main(void)
{
/* variables */
int rc = EXIT_SUCCESS;
/* do stuff */
/* is everything fine? */
if(!) {
rc = EXIT_FAILURE;
}
return rc;
}
/* getline: read a line into s, return s (from C_dreamer)*/
char *getline(char *line, int maxline)
{
int c, i, j;
for(i = 0, j = 0; (c = getchar())!=EOF && c != '\n' && c != '.'; ++i)
{
if(i < lim - 1)
{
*line++ = c;
}
}
if(c == '\n')
{
if(i <= lim - 1)
{
*line++ = c;
}
++i;
}
line[j] = '\0';
return *line;
}
/* output sentences time from time while the user is not entering anything*/
void talk (char *sentence_pointer)
{
/* variables */
/* talking loop */
/* get the next sentence */
/* output the next sentence if the user is not writing something*/
}
Thanks you all for help in advance.
__________________
|
|
|
05-13-2005, 07:31 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Quote:
|
My main problem is the talk function, it will ouput sentences from time to time (because c is so fast, but I don't know how to do that....) and also track the keyboard for a key pressed event.
|
hmmm.. let me see if I get this right, you want it to be able to, like answer every 2 or 3 seconds, without it spilling out what ever it has to say, at the speed of a jiffy right ?
And you want some form of action uppon keypress, that beeing any key, not just some form of waiting for enter to be pressed?
Sounds rather advanced, If his was supposed to be like a talking 'bot of some sort, I would have it spawn a thread, listening on some given port/socket, when it detects anythign there, it should send a random quote to the desired socket.. But that sounds way too advanced for what you're showing here..
I'll be back later tonight looking more closely at your code.
|
|
|
05-13-2005, 08:19 AM
|
#3 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 595
|
Maybe you could use a call to the sleep function inbetween calls to talk?
__________________
Stop intellectual property from infringing on me
|
|
|
05-13-2005, 09:46 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Jan 2005
Posts: 9
|
ah ok the sleep function. thanks
but for the keyboard tracking, I've no idea
__________________
|
|
|
05-13-2005, 05:51 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Yes, that is API related so I moved it.
However, what you want (keypress events) are quite basic and well covered once you learned to http://www.google.com for it.
__________________
|
|
|
05-17-2005, 09:45 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Jan 2005
Posts: 9
|
I haven't got enough knowledge of C. I'm reading the book "The C book" for the moment.
__________________
|
|
|
05-17-2005, 01:03 PM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
For nonblocking read in C, if it's a POSIX environment, then this can be of use:
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
int stdin_init(int fd)
{
/* set file to non-blocking */
int flags = fcntl(fd, F_GETFL, 0);
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
return -1;
return 0;
}
/* Read bytes from stdin, using non-blocking if possible. */
int stdin_read(char *buf, int len, int interactive, int fd)
{
fd_set rfds;
int count;
count = read(fd, buf, len);
if (count < 0) { /* error or EWOULDBLOCK */
if (errno != EAGAIN)
return -1;
/* Wait until at least one byte is available */
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
select(1, &rfds, NULL, NULL, NULL);
count = read(fd, buf, len);
if (count < 0)
count = 0; /* EOF */
}
return count;
}
And it can be used in your code instead of your getc() loop, something like this:
Code:
...
int init_return;
char buffer[SOME_LENGTH];
...
if( ! init_return = init_stdin(stdin) )
{
while(0 < stdin_read(buffer, 1, init_return, stdin))
{
/* do something with buffer[0] */;
}
}
...
This will only work in a POSIX environment, and on some systems, like the AIX, the stdout stream must be set to non blocking aswell.
|
|
|
05-18-2005, 02:37 AM
|
#8 (permalink)
|
|
Registered User
Join Date: Jan 2005
Posts: 9
|
thanks for the help. But I don't know what a POSIX is yet 
__________________
|
|
|
05-18-2005, 03:09 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Quote:
|
Originally Posted by nvictor
I don't know what a POSIX is yet
|
POSIX is the environment used in the *nix world, it is not compatible with dos/windows not even compatible within different sections of the *nix world.
So some POSIX compliant code which will work on *BSD, might not work on the Linux platform.
But thats what you venture into, when dealing with system dependant coding.
|
|
|
05-18-2005, 08:49 AM
|
#10 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 595
|
To be more accurate there are multiple levels of POSIX compliance and the actual certification for POSIX compliance costs a pretty penny. The boring details are here http://www.opengroup.org/onlinepubs/009695399/toc.htm , but basically it just defines a unix like view of the world for programs so that the programmer can make som assumptions about the system.
With unix services for windows (MS SFU) on either windows 2000 or XP, you can get have access to most POSIX features on the windows platform.
__________________
Stop intellectual property from infringing on me
|
|
|
05-19-2005, 01:16 PM
|
#11 (permalink)
|
|
Code Monkey
Join Date: Mar 2005
Posts: 55
|
Does this work for you?
Code:
char about[3];
about[0][] = "My name is KaaT, version 0.0";
about[1][] = "Victor is my creator.";
about[2][] = "I don't know my age yet.";
I think you mean to do this:
Code:
char *about[3];
about[0] = "My name is KaaT, version 0.0";
about[1] = "Victor is my creator.";
about[2] = "I don't know my age yet.";
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:07 AM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|