Random sentence generator

simple generator

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 10292 Views - Last Post: 14 October 2005 - 03:25 PM Rate Topic: -----

#1 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Random sentence generator

Posted 11 October 2005 - 01:53 PM

I've been working on a program over the last few days that asks to create an array of pointers to char called article noun verb and preposition. Basically I need to create random sentences in the format article noun verb preposition article noun verb. The sentences need to start with a capital letter and end with a period.

It sounded pretty simple at first but I seem to be getting lost in how to generate random sentences where the pointers have different rows and collums. There is nothiong similar to this in my book. This s what I have so far, any "pointers" would be greatly appreciated! (bet you haven't heard that on before. :D )

#include <stdio.h>
#include <string.h>


void sentenceGenerator(  char article[],  char noun[],  char verb[],
         char preposition[], char article[],  char noun[] );

int main()
{
	const char *article[5]=    { "the", "a", "one", "some", "any",  };
	const char *noun[5]=       { "boy", "girl", "dog", "town", "car", };
	const char *verb[5]=       { "drove","jumped", "ran", "walked", "skipped", };
	const char *preposition[5]={ "to", "from", "over", "under", "on",  };
	int i;

    
	for ( i = 0; i < 6; i++ );
	
  *( article)     = i;
  *( noun )        = i;
  *( verb )        = i;
  *( preposition ) = i;

      srand( time( 0 ) );

      void sentenceGenerator( artical, noun, verb, preposition, article, noun )

    }
}

void sentenceGenerator(  char article[],  char noun[],  char verb[],
         char preposition[], char article[],  char noun[] )
{

	int i;
	for (i = 0; i <20; i++)
	{
  printf( "%s%s%s%s%s%s.\n", *article, *noun, *verb, *preposition, *article, *noun );
	}
}


This post has been edited by jsbeckton: 11 October 2005 - 04:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Random sentence generator

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Random sentence generator

Posted 11 October 2005 - 04:21 PM

What exactly is giving you the trouble? Once you've set your arrays, you'll implement an algorithm that allows to to obtain members of the arrays in the sequence required...you've got a start on it...is there something in particular that is causing problems?
Was This Post Helpful? 0
  • +
  • -

#3 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Random sentence generator

Posted 11 October 2005 - 04:28 PM

i can tell you that you're going to get an error with this line:

*( artical )     = i;

Was This Post Helpful? 0
  • +
  • -

#4 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: Random sentence generator

Posted 11 October 2005 - 05:02 PM

in my prototype, its giving me errors because of "redefinition" in "article" and "noun" but I need to use them twice in my alogarithm.
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Random sentence generator

Posted 11 October 2005 - 07:19 PM

Actually, you need to access those arrays twice...just use two separate variables, one for each access...you can even make sure you don't repeat.
Was This Post Helpful? 0
  • +
  • -

#6 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: Random sentence generator

Posted 11 October 2005 - 07:51 PM

I removed the initializers and modified the pointer arrays to include additional article and noun pointer arrays, however it seems that there must be another way to use those pointer arrays twice.

updated code:
#include <stdio.h>
#include <string.h>


void sentenceGenerator(  char article1[],  char noun1[],  char verb[],
         char preposition[], char article[],  char noun[] );

int main()
{
    const char *article1[5]=    { "the", "a", "one", "some", "any",  };
	const char *noun1[5]=       { "boy", "girl", "dog", "town", "car", };
	const char *article[5]=    { "the", "a", "one", "some", "any",  };
	const char *noun[5]=       { "boy", "girl", "dog", "town", "car", };
	const char *verb[5]=       { "drove","jumped", "ran", "walked", "skipped", };
	const char *preposition[5]={ "to", "from", "over", "under", "on",  };
    int i;

    
	

      srand( time( 0 ) );

      void sentenceGenerator( article, noun, verb, preposition, artical, noun )

    }
}

void sentenceGenerator(  char article1[],  char noun1[],  char verb[],
         char preposition[], char article[],  char noun[] )
{
    int i;
	
	for (i = 0; i <20; i++)
	{
        
  printf( "%s%s%s%s%s%s\n", article, noun, verb, preposition, article, noun );
	}
}


This post has been edited by jsbeckton: 11 October 2005 - 07:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Random sentence generator

Posted 11 October 2005 - 11:06 PM

Good to see that you cleaned up your loop. However your printf statement will continue showing the same values everytime.Make the following change.
printf( "%s%s%s%s%s%s\n", article, noun, verb++, preposition++, article++, noun++);


Increment the article and noun only later because if done before, the article and noun value printed from the same printf statement will be different as it would have been incremented.
Was This Post Helpful? 0
  • +
  • -

#8 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: Random sentence generator

Posted 12 October 2005 - 09:58 AM

After changing the print function to :
printf( "%s%s%s%s%s%s\n", article, noun, verb++, preposition++, article++, noun++ );


I am getting error messages saying "++ needs 1-value" for all 4 strings.
Was This Post Helpful? 0
  • +
  • -

#9 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Random sentence generator

Posted 13 October 2005 - 01:13 AM

My Mistake.
printf( "%s%s%s%s%s%s\n", *article, *noun, verb++, *preposition++, *article++, *noun++ );



I'm not close to a compiler now so please tell me if that works or not.
Was This Post Helpful? 0
  • +
  • -

#10 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: Random sentence generator

Posted 13 October 2005 - 06:50 AM

Same error message saying that "++" needs 1-value.
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Random sentence generator

Posted 13 October 2005 - 07:03 AM

Can you repost your entire program? It will be easier to debug.
Was This Post Helpful? 0
  • +
  • -

#12 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: Random sentence generator

Posted 13 October 2005 - 07:34 AM

just made a few more changes, down to one error but cant remove one of the } without big problems:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void sentenceGenerator(  char ,  char ,  char , char , char ,  char  );
        

int main()
{
 
	const char *article[5]=    { "the", "a", "one", "some", "any",  };
	const char *noun[5]=       { "boy", "girl", "dog", "town", "car", };
	const char *verb[5]=       { "drove","jumped", "ran", "walked", "skipped", };
	const char *preposition[5]={ "to", "from", "over", "under", "on",  };
   

    
	
	
      

       void sentenceGenerator( article, noun, verb, preposition, artical, noun );

}
}

void sentenceGenerator(  char article[],  char noun[],  char verb[],
         char preposition[], char article[],  char noun[] )
{
    int i;
	
	for (i = 0; i <20; i++)
	{
        
  printf( "%s%s%s%s%s%s\n", rand(article), rand(noun),  rand(verb++), rand(preposition++), 
  	rand(article++), rand(noun++) );
	}
}



This post has been edited by jsbeckton: 13 October 2005 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#13 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: Random sentence generator

Posted 13 October 2005 - 07:38 AM

If i remove the second "}" i get a bunch of errors.

This post has been edited by jsbeckton: 13 October 2005 - 07:48 AM

Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Random sentence generator

Posted 13 October 2005 - 08:28 AM

I'm going to take a look at it, but you should know that leaving an extra bracket in the code does not get rid of errors, it only stops the compiler from seeing other errors. It will have to be removed.
Was This Post Helpful? 0
  • +
  • -

#15 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: Random sentence generator

Posted 13 October 2005 - 08:32 AM

I was afraid you'd say that!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2