Thread: simple question

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    simple question

    i have this random sentence generator code...it is running juts fine but why if I declare the variables in main it generates run time errors??

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>


    int a,n,v,p,a2,n2,counter;
    const char *articles[5]= {"the", "a", "one", "some", "any" };
    const char *nouns[5]= {"boy", "girl", "dog", "town", "car" };
    const char *verbs[5]= {"drove", "jumped", "ran", "walked", "skipped" };
    const char *preps[5]= {"to", "from", "over", "under", "or" };
    char sentences[6];


    int main ()


    {



    srand ( time ( NULL ) );

    printf("Random Sentence Generator by JFL.\n");
    printf("Type ANYTHING to start the program\n\n");

    getch();

    for ( counter = 0; counter < 20; counter++ ) {

    a = rand() %5;
    n = rand() %5;
    v = rand() %5;
    p = rand() %5;
    a2 = rand() %5;
    n2 = rand() %5;

    sprintf ( sentences, "%s %s %s %s %s %s.", articles[ a ], nouns[ n ],
    verbs[ v ], preps[ p ], articles [ a2 ], nouns[ n2 ] );

    sentences[ 0 ] = toupper ( sentences[ 0 ] );

    printf ( "\n%s\n", sentences );

    }


    return 0;

    }

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>

    int main ()

    int a,n,v,p,a2,n2,counter; <--why generates errors if is here?
    const char *articles[5]= {"the", "a", "one", "some", "any" };
    const char *nouns[5]= {"boy", "girl", "dog", "town", "car" };
    const char *verbs[5]= {"drove", "jumped", "ran", "walked", "skipped" };
    const char *preps[5]= {"to", "from", "over", "under", "or" };
    char sentences[6];


    {



    srand ( time ( NULL ) );

    printf("Random Sentence Generator by JFL.\n");
    printf("Type ANYTHING to start the program\n\n");

    getch();
    ................................
    pd: any suggestions to make this program better?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int main ()

    int a,n,v,p,a2,n2,counter; <--why generates errors if is here?
    const char *articles[5]= {"the", "a", "one", "some", "any" };
    const char *nouns[5]= {"boy", "girl", "dog", "town", "car" };
    const char *verbs[5]= {"drove", "jumped", "ran", "walked", "skipped" };
    const char *preps[5]= {"to", "from", "over", "under", "or" };
    char sentences[6];


    {
    Because you declared them after 'main()' and before the opening braces. You want this instead:
    Code:
    int main () 
    { 
        int a,n,v,p,a2,n2,counter; <--why generates errors if is here? 
        const char *articles[5]= {"the", "a", "one", "some", "any" }; 
        const char *nouns[5]= {"boy", "girl", "dog", "town", "car" }; 
        const char *verbs[5]= {"drove", "jumped", "ran", "walked", "skipped" }; 
        const char *preps[5]= {"to", "from", "over", "under", "or" }; 
        char sentences[6];
    Big difference.

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

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    i placed them where you say...compiles but when is run it generates the 20 sentences..then, three errors appears...if i place the declarations before int main() its compiles and runs well... why is that?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char sentences[6];

    This should be a lot bigger than six characters. It has to store a whole sentence. Try 100.

    char sentences[100];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM