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?