Thread: Problems with a Poem Generator

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Belfast
    Posts
    2

    Problems with a Poem Generator

    I am currently in the process of writing a random poetry generator and have come up against some problems for which I cannot find a solution. My main problem is being unable to assign a string from an array of strings, followed by returning this value and assigning it to another variable for display. I am currently recieving the following errors:
    • 50 incompatible types in assignment
    • 51 [Warning] return makes integer from pointer without a cast
    • 51 [Warning] function returns address of local variable
    • 59 incompatible types in assignment


    Here is the code I am using, I apologise for any hideous coding...

    EDIT: Apologies for not posting the full code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    char adjective1[15], adjective2[15], adjective3[15], noun1[20], noun2[20], noun3[20],
         verb1[15], verb2[15], verb3[15], prep1[50], prep2[50], adverb1[15];
    
    char randomadj[15], randomnoun[20], randomverb[15], randomadv[15], randomprep[50];
    FILE *myfile;
    
    void initrand()
    {
         srand((unsigned)(time(0)));
    }
    
    int randint(int max)
    {
        int random = (max*rand()/(RAND_MAX+1.0));
        return (random);
    } 
    
    char randadj()
    {
         char string[15];
         char stringOut[15];
         myfile = fopen("adjective.txt", "r");
         int endval = 0;
         while ((string, 15, myfile) != NULL)
         {
               endval++;
         }
         return endval;
         int i;
         char array[endval][50];
         for (i = 0; i<=endval-1; i++)
         {
             fgets(string, 15, myfile);
             int len = strlen(string) - 1;
             if (string[len] == '\n')
                string[len] = '\0';
             int j;
             for (j = 0; j<=len; j++)
             {
                 array[i][j] = string[j];
             }
         }
         int randnum = 0;
         randnum = randint(endval-1);
         stringOut = array[randnum];
         return stringOut;
         
    }
         
    int main(int argc, char *argv[])
    {
      initrand();
      
      randomadj = randadj;
      strcpy(adjective1, randomadj); 
      strcpy(adjective2, "feeding");
      strcpy(adjective3, "flawless");
      strcpy(noun1, "Book");
      strcpy(noun2, "tongue");
      strcpy(noun3, "mountain");
      strcpy(verb1, "shines");
      strcpy(verb2, "drools");
      strcpy(verb3, "regurgitates");
      strcpy(prep1, "in harmony with");
      strcpy(prep2, "without thinking about");
      strcpy(adverb1, "feeding");
      
      printf (" A %s %s", adjective1, noun1);
      printf ("\n------------------------------------------------------");
      printf ("\n A %s %s %s %s the %s %s.", adjective1, noun1, verb1, prep1, adjective2, noun2);
      printf ("\n While %s the %s %s.", adverb1, noun1, verb2);
      printf ("\n The %s %s %s a %s %s.\n", noun2, verb3, prep2, adjective3, noun3);
      
      system("PAUSE");	
      return 0;
    }
    Last edited by HughHefner; 01-04-2011 at 09:53 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Take the first warning or error you get. Go to the line it tells you. Look around. Repeat until you have them all fixed.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Location
    Belfast
    Posts
    2
    I understand how error checking works, as part of the error correction process I have been unable to find a suitable solution to the particular problems I am faced with, I appreciate the advice which has already been provided and welcome any other assistance that can be provided.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your code is incomplete. You are asking us to guess at where unspecified warnings and errors are, by looking at incomplete code. Furthermore, you aren't even showing us all of the example you're just doing "//etc...". You can't seriously expect people to troubleshoot your half-ass example.
    Code:
    while ((string, 15, myfile) != NULL)
    This however, is not what you want. Or if that's what you want, it's not going to do what you think you want.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by HughHefner View Post
    I am currently in the process of writing a random poetry generator and have come up against some problems for which I cannot find a solution. My main problem is being unable to assign a string from an array of strings, followed by returning this value and assigning it to another variable for display. I am currently recieving the following errors:

    Here is the code I am using, I apologise for any hideous coding...

    EDIT: Apologies for not posting the full code
    Code:
         return endval;
         int i;
         char array[endval][50];
         for (i = 0; i<=endval-1; i++)
         {
             fgets(string, 15, myfile);
             int len = strlen(string) - 1;
             if (string[len] == '\n')
                string[len] = '\0';
             int j;
             for (j = 0; j<=len; j++)
             {
                 array[i][j] = string[j];
             }
         }
         int randnum = 0;
         randnum = randint(endval-1);
         stringOut = array[randnum];
         return stringOut;
         
    }
    
    }
    It's hard to know were to start. Other that what's been noted thus far, one glaring
    mistake is that none of the code after the return statament will get executed.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is clearly a lot you don't understand about strings in C, and plenty to learn about C in general. Try reading the tutorial on strings we have here, and read through the C FAQ section on strings.

    Then, start with some practice programs to get a handle for string manipulation, file I/O, loop structure, etc. Work those skills into your poem generator program in small increments.

    A few general tips:
    • If you don't want a function to take any parameters, be explicit about it: int foo(void). Leaving the parameter list empty () means that you can pass anything to the function and the compiler wont complain about you misusing a function.
    • Global variables are evil. Move adjective1, myfile, etc into main or the appropriate function and pass them to functions as needed.
    • Check the return values of fopen, fgets and any other standard functions to avoid reading from an unopened file or reading past the end.
    • Close your files when you're done with them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ 2010 express problems
    By dnj23 in forum Windows Programming
    Replies: 6
    Last Post: 08-10-2010, 06:16 AM
  2. Most Common problems in C
    By Bayint Naung in forum C Programming
    Replies: 18
    Last Post: 06-02-2010, 08:20 PM
  3. Random Poem Generator
    By ineedmunchies in forum C++ Programming
    Replies: 29
    Last Post: 03-26-2010, 09:31 AM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM