Thread: Program to generate a random sentence

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    Question Program to generate a random sentence

    so I'm creating a program that generates random sentences with structs but i need some help... I'm trying to use structs and and create 4 different groupings article, noun, verb, and preposition. Then I'm trying to use "r = rand() % ;" to randomly pick one one word from each group to make a sentence.

    this is what i have

    Code:
    typedef enum article
    {
        the = 1, a, one, some, any
    } article;
    
    
    typedef enum noun
    {
        boy = 1, girl, dog, town, car
    } noun;
    
    
    typedef enum verb
    {
        drove = 1, jumped, ran, walked, skipped
    } verb;
    
    
    typedef enum preprositions
    {
        to = 1, from, over, on
    } preprositions;
    
    typedef enum noun
    {
        boy = 1, girl, dog, town, car
    } noun;

    Did I do that part correctly? is there a better way?

    then something like this?....

    Code:
    for (i =0, i <20, i++)// i want to generate 20 random sentences
    {
    r= rand () % 5
    printf ("%c", r->article);
    
    r= rand () % 5
    printf("%c", r->noun);
    
    ........
    }
    what do i do next? what did i miss?

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Are you missing the strings you want to print?

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Yeah, it doesn't look like you want enums for this (and the initial 1 in the enums is wrong anyway).
    You want an array of char*, like:
    Code:
    char *article[] = {"a", "one", "some", "any"};
    
    printf("%s", article[rand() % 4]);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by oogabooga View Post
    You want an array of char*, like:
    const char*
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by oogabooga View Post
    You want an array of char*, like:
    const char*
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by ktype View Post
    what did i miss?
    Just the part about actually learning the C language...no harm done through - once you've grasped the concept of lookup tables you should be good.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to generate a random number in c?
    By blogchama in forum C Programming
    Replies: 2
    Last Post: 01-20-2011, 10:39 AM
  2. Random Sentence Generator
    By jlangfo5 in forum General AI Programming
    Replies: 1
    Last Post: 10-13-2010, 01:22 PM
  3. Random Sentence From A File
    By gandil in forum C Programming
    Replies: 3
    Last Post: 01-08-2006, 09:50 AM
  4. random sentence generator
    By jsbeckton in forum C Programming
    Replies: 19
    Last Post: 10-14-2005, 04:23 PM
  5. Replies: 11
    Last Post: 07-16-2002, 11:39 AM

Tags for this Thread