Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-13-2005, 06:22 AM   #1 (permalink)
nvictor
Registered User
 
Join Date: Jan 2005
Posts: 9
nvictor is on a distinguished road
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.
nvictor is offline   Reply With Quote
Old 05-13-2005, 08:31 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-13-2005, 09:19 AM   #3 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Maybe you could use a call to the sleep function inbetween calls to talk?
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 05-13-2005, 10:46 AM   #4 (permalink)
nvictor
Registered User
 
Join Date: Jan 2005
Posts: 9
nvictor is on a distinguished road
ah ok the sleep function. thanks
but for the keyboard tracking, I've no idea
nvictor is offline   Reply With Quote
Old 05-13-2005, 06:51 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 05-17-2005, 10:45 AM   #6 (permalink)
nvictor
Registered User
 
Join Date: Jan 2005
Posts: 9
nvictor is on a distinguished road
I haven't got enough knowledge of C. I'm reading the book "The C book" for the moment.
nvictor is offline   Reply With Quote
Old 05-17-2005, 02:03 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-18-2005, 03:37 AM   #8 (permalink)
nvictor
Registered User
 
Join Date: Jan 2005
Posts: 9
nvictor is on a distinguished road
thanks for the help. But I don't know what a POSIX is yet
nvictor is offline   Reply With Quote
Old 05-18-2005, 04:09 AM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-18-2005, 09:49 AM   #10 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 05-19-2005, 02:16 PM   #11 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 56
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
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.";
QUantumAnenome is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB or C#, or something else Apodysophilia MS Technologies ( ASP, VB, C#, .NET ) 6 10-15-2004 10:48 AM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 03:36 AM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 01:22 AM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 01:03 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting