He Returns..... :3

B

blackneos940

Guest
I'm makin' an AI Program!..... ^^ I thought of Cleverbot, and also Betabot, and, well, here!:

Code:
//This Program is Free Software, released under the GPLV2, or, at your option, any LATER Version

/*Ok... This is my first attempt at making a basic Machine-Learning AI*/

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main(void)

{
  printf ("Say anything, then press Enter!\n");  //Now, for some preliminary preparations for the first Execution of the Program

  char user_input[200];

  fgets (user_input, 200, stdin);

  FILE *ptr;  //We open her "Memory" for Writing

  chdir ("/home/name_expunged/Lucca_Brain");

  ptr = fopen("Memory.txt", "a+");

  fprintf (ptr, "%s", user_input); //This is where we Write to her "Memory"

  fclose(ptr);

  printf ("Greetings!! My name is Lucca, the Intelligent Lifebot! Ask me anything!\n");  //First, we greet the User

  return 0;

}
She has a Text File that serves as her "Memory", which will soon be read from..... I hope.


Anyway, what d'ya think.....? :) Not too bad for a start, eh.....?

I can't thank you guys enough for helping me get this far....... :D May God (or whatever you call the great All), richly Bless you and keep you..... :3

~blackneos940
 


B

blackneos940

Guest
BTW, this is TOTALLY not a reference to my favorite brainiac from Chrono Trigger..... ;)
 
R

rstanley

Guest
A few comments to make your code better.

You should check for the following errors:

chdir() - If successful, will return 0, if the directory does not exist, or some other problem, will return -1.

fopen() - If successful will return a valid pointer, NULL if file can not be opened.

Code:
printf ("Greetings!! My name is Lucca, the Intelligent Lifebot! Ask me anything!\n");  //First, we greet the User
I assume that this line is in preperation for additional code after his.

Code:
return 0;
I usually recommend the use of "EXIT_SUCCESS", and "EXIT_FAILURE". Some O/S's return a different value if successful. These constants will always work.
Code:
return EXIT_SUCCESS;
Start with this then continue!
 
B

blackneos940

Guest
A few comments to make your code better.

You should check for the following errors:

chdir() - If successful, will return 0, if the directory does not exist, or some other problem, will return -1.

fopen() - If successful will return a valid pointer, NULL if file can not be opened.

Code:
printf ("Greetings!! My name is Lucca, the Intelligent Lifebot! Ask me anything!\n");  //First, we greet the User
I assume that this line is in preperation for additional code after his.

Code:
return 0;
I usually recommend the use of "EXIT_SUCCESS", and "EXIT_FAILURE". Some O/S's return a different value if successful. These constants will always work.
Code:
return EXIT_SUCCESS;
Start with this then continue!
Thanks for the advice, good sir!!..... :3 But..... Whenever I use the EXIT_SUCCESS thing, I think it always treated it as an undeclared Variable...... :< At least, GCC does, as well as Codepad.org..... :< Nevertheless, I hope there's some way to make it work..... :) Maybe I'm doing it wrong.....? :) Anyway, I'm stuck at the part where Lucca reads from "Memory" and Prints out what's in the Text File...... :( Turns out I don't even know HOW to use fopen("example.txt", "r"); to read from and show the contents of Files!...... D:
 
R

rstanley

Guest
You only need to "#include <stdlib.h>" to use "EXIT_SUCCESS", etc... You have done this in the above code.
View the changes I made to the code below. You should start to think about slitting the program into separate functions.
Code:
#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#define DIM 200  // Avoid "Magic Numbers"" in your code.  If you change this value, you only need to do it once!

int main(void)

{
  char *retval = NULL;

  printf ("Say anything, then press Enter!\n");  // Now, for some preliminary preparations for the first Execution of the Program

  char buffer[DIM];  // Note I changed the name to a more generic name

  fgets (buffer, DIM, stdin);

  FILE *ptr = NULL;  // Define a pointer to a FILE

  chdir ("/home/name_expunged/Lucca_Brain");

  ptr = fopen("Memory.txt", "a+");  // We open her "Memory" for Writing

  fprintf (ptr, "%s", buffer);  // This is where we Write to her "Memory"

  fclose(ptr);

  printf ("Greetings!! My name is Lucca, the Intelligent Lifebot! Ask me anything!\n");  //First, we greet the User

  /**
   ** I added the following code to process each line of "Memory.txt"
   **/

  ptr = fopen("Memory.txt", "r");

  if(ptr == NULL)  // Test to insure the file was opened correctly!
  {
  printf("Memory.txt cannot be opened!\n");
  return EXIT_FAILURE;  // Nothing to do but quit!
  }

  retval = fgets(buffer, DIM, ptr); // Get the first line

  while(retval != NULL)
  {
  // Add code here to process each line of the file

  retval = fgets(buffer, DIM, ptr); // Get the next line, if any.
  }

  fclose(ptr);

  /**
   ** End of my code
   **/


  return EXIT_SUCCESS;

}
 
B

blackneos940

Guest
You only need to "#include <stdlib.h>" to use "EXIT_SUCCESS", etc... You have done this in the above code.
View the changes I made to the code below. You should start to think about slitting the program into separate functions.
Code:
#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#define DIM 200  // Avoid "Magic Numbers"" in your code.  If you change this value, you only need to do it once!

int main(void)

{
  char *retval = NULL;

  printf ("Say anything, then press Enter!\n");  // Now, for some preliminary preparations for the first Execution of the Program

  char buffer[DIM];  // Note I changed the name to a more generic name

  fgets (buffer, DIM, stdin);

  FILE *ptr = NULL;  // Define a pointer to a FILE

  chdir ("/home/name_expunged/Lucca_Brain");

  ptr = fopen("Memory.txt", "a+");  // We open her "Memory" for Writing

  fprintf (ptr, "%s", buffer);  // This is where we Write to her "Memory"

  fclose(ptr);

  printf ("Greetings!! My name is Lucca, the Intelligent Lifebot! Ask me anything!\n");  //First, we greet the User

  /**
   ** I added the following code to process each line of "Memory.txt"
   **/

  ptr = fopen("Memory.txt", "r");

  if(ptr == NULL)  // Test to insure the file was opened correctly!
  {
  printf("Memory.txt cannot be opened!\n");
  return EXIT_FAILURE;  // Nothing to do but quit!
  }

  retval = fgets(buffer, DIM, ptr); // Get the first line

  while(retval != NULL)
  {
  // Add code here to process each line of the file

  retval = fgets(buffer, DIM, ptr); // Get the next line, if any.
  }

  fclose(ptr);

  /**
   ** End of my code
   **/


  return EXIT_SUCCESS;

}
Thanks for the help, buddy..... :) The Code Compiled successfully, and it ran, seemingly without issue..... I still need to work on learning how to Read from Files, and Print their contents to the Screen..... *Sigh*..... :( I will NEVER give up, but..... It seems that the more I learn, the more I find out I don't understand....... :(
 
R

rstanley

Guest
Worry if you don't think there is more to learn! ;^) I am mostly self taught, and lack a lot of knowledge I would have gotten with a full CS degree.
 


Staff online

Members online


Top