Thread: A C program that generates random sentences

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    A C program that generates random sentences

    Hi,
    just started programming in c and I was looking over this question i found in a c programming book, and it generates random sentences with four arrays, Article that should contain all the articles like a , the etc. noun that contains all the noun, verb that contains all verbs, and prepositon which contains the prepositions. the program should contain an array to concatinate all the words ,and should also print the first letter to be upper case and end the sentence with a period.
    heres my code( note: I know i am not really good at c right now but i want to improve, and not sure what i am doing is right)

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    # include <ctype.h>
    # include <time.h>
    
    int main()
    {
    	char sentence[1024];
    	char *article = {"the", "a", "one", "some", "any"};
    	char *noun = {"boy", "girl", "dog", "town" "car"};
    	char *verb = {"drove", "jumped", "ran", "walked", "skipped"};
    	char *preposition = {"to", "from", "over", "under", "on"};
       
            srand(time(NULL));
    	while(sentence != NULL)
    	{
    
              char **sent = {article, noun, verb preposition};
              sent = rand() % 4;
    	  strcpy (sentence, (*article+1);
    	  strcat (sentence, (*noun+1);
    	  strcat (sentence, (*verb+1);
    	  strcat (sentence, (*prepostion+1);
     	  strcat (sentence, (*article+1);
    	  strcat (sentence, (*noun+1);
    	
    	  sprintf(sentence,"%s %s %s %s %s %s",article, noun, verb, prepostion,    article, noun);
    	  printf("%s\n", sentence);    		
    	  article++;
    	  noun++;
              prepostion++;
     	  verb++;
    	}
    		      
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So sent can either be a char **, or an array of four char *, or an int (rand()%4); it cannot be all three at the same time. You also probably want article, noun, verb, and preposition to be arrays too.

    You need to use rand to pick a random article; then use rand again to pick a random noun; then use rand again to pick a random verb; etc.

    There's no reason to use article+1 instead of article[1].

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There's also the fact that compiling this points out a few obvious mistakes.

    C:\Documents and Settings\Owner>gcc -c -Wall -ansi -pedantic foo.c
    foo.c: In function 'main':
    foo.c:10:2: warning: excess elements in scalar initializer
    foo.c:10:2: warning: (near initialization for 'article')
    foo.c:10:2: warning: excess elements in scalar initializer
    foo.c:10:2: warning: (near initialization for 'article')
    foo.c:10:2: warning: excess elements in scalar initializer
    foo.c:10:2: warning: (near initialization for 'article')
    foo.c:10:2: warning: excess elements in scalar initializer
    foo.c:10:2: warning: (near initialization for 'article')
    foo.c:11:2: warning: excess elements in scalar initializer
    foo.c:11:2: warning: (near initialization for 'noun')
    foo.c:11:2: warning: excess elements in scalar initializer
    foo.c:11:2: warning: (near initialization for 'noun')
    foo.c:11:2: warning: excess elements in scalar initializer
    foo.c:11:2: warning: (near initialization for 'noun')
    foo.c:12:2: warning: excess elements in scalar initializer
    foo.c:12:2: warning: (near initialization for 'verb')
    foo.c:12:2: warning: excess elements in scalar initializer
    foo.c:12:2: warning: (near initialization for 'verb')
    foo.c:12:2: warning: excess elements in scalar initializer
    foo.c:12:2: warning: (near initialization for 'verb')
    foo.c:12:2: warning: excess elements in scalar initializer
    foo.c:12:2: warning: (near initialization for 'verb')
    foo.c:13:2: warning: excess elements in scalar initializer
    foo.c:13:2: warning: (near initialization for 'preposition')
    foo.c:13:2: warning: excess elements in scalar initializer
    foo.c:13:2: warning: (near initialization for 'preposition')
    foo.c:13:2: warning: excess elements in scalar initializer
    foo.c:13:2: warning: (near initialization for 'preposition')
    foo.c:13:2: warning: excess elements in scalar initializer
    foo.c:13:2: warning: (near initialization for 'preposition')
    foo.c:19:11: warning: initialization from incompatible pointer type
    foo.c:19:11: warning: excess elements in scalar initializer
    foo.c:19:11: warning: (near initialization for 'sent')
    foo.c:19:11: warning: excess elements in scalar initializer
    foo.c:19:11: warning: (near initialization for 'sent')
    foo.c:19:46: error: expected '}' before 'preposition'
    foo.c:20:16: warning: assignment makes pointer from integer without a cast
    foo.c:21:34: error: expected ')' before ';' token
    foo.c:36:1: warning: passing argument 2 of 'strcpy' makes pointer from integer w
    ithout a cast
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/string.h:45:39: note:
    expected 'const char *' but argument is of type 'int'
    foo.c:36:1: error: expected declaration or statement at end of input
    foo.c:36:1: error: expected declaration or statement at end of input
    foo.c:13:8: warning: unused variable 'preposition'
    What's a scalar?
    Scalar values are usually quantities. You didn't intend to initialize a scalar value here, but you didn't exactly follow the syntax for initializing an array. So that's why the compiler complains about those variables.

    What's an initializer?
    An initializer is some value used in an initialization statement. Every array element could have an initializer, but scalars are only supposed to have one so the others aren't used. That's another reason the compiler was able to say what it said.

    What about the other compiler messages?
    They stem from syntax errors in your code. I see some unbalanced parentheses.

    Like tabstop, I also noticed things like this:
    Code:
     (*p + 1)
    This dereferences p, and adds one to the thing it points to. The compiler thinks it's of expression type int.

    Of course, it's right. This is not the same as p[1]. Instead:
    p + 1 is also a pointer type, pointing to the first object after p.
    *(p + 1) is the first object after p.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I would also advise doing some bound checking on "sentence" before strcating it a million times with unknown size strings..

    Same goes with the rand % 4, why not write a simple macro that calculates the size of the array you are running rand() against, and use that - 1 for your rand % C expression...then you can freely change/the contents of the array without having to manually adjust all the "magic" size numbers you use...the preprocessor is you friend

    ex:
    Code:
    #define ARR_SIZ(a) (sizeof (a) / sizeof *(a))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program that generates a "random walk" across 10*10 array
    By danieldcc in forum C Programming
    Replies: 37
    Last Post: 07-23-2011, 01:19 AM
  2. Random Number Generator Always Generates 0 First
    By LyTning94 in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2011, 03:08 PM
  3. string generates
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 10-02-2005, 08:23 AM
  4. Replies: 2
    Last Post: 12-25-2003, 01:31 AM