Hello,
I am a c++ program beginner, I am trying to make a program that generates sentences. This program should take a word from each array and turn it into sentences using each word in the array.
As for example the output should be somehthing like this. The sentences does not need to make sense as long it takes each word and turn it into sentences.
I ran the program that and it does not generate correctly. Can u please help me and tell me what I need to do? THank you.
The girl jumped over.
A boy drove on.
.....
#include <iostream>
#include <ctime> //in order to use the function time();
#include <cstdlib> //contains function prototype for rand
using namespace std;
int main()
{
int index, i;
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"};
char sentence[100];
srand (time(0));
strcpy(sentence, "\0");
index=rand()%5;
for (i=index; i<index+10; i++)
{
strcat(sentence, article[ rand() % 5 ] );
strcat(sentence, " " );
strcat(sentence, noun[ rand() % 5 ] );
strcat(sentence, " " );
strcat(sentence, verb[ rand() % 5 ] );
strcat(sentence, " " );
strcat(sentence, preposition[ rand() % 5 ] );
strcat(sentence, " " );
strcat(sentence, "." );
}
sentence[0] = toupper(sentence[0]);
cout<<sentence<<endl;
cout<<endl;
return 0;
}
I am a c++ program beginner, I am trying to make a program that generates sentences. This program should take a word from each array and turn it into sentences using each word in the array.
As for example the output should be somehthing like this. The sentences does not need to make sense as long it takes each word and turn it into sentences.
I ran the program that and it does not generate correctly. Can u please help me and tell me what I need to do? THank you.
The girl jumped over.
A boy drove on.
.....
#include <iostream>
#include <ctime> //in order to use the function time();
#include <cstdlib> //contains function prototype for rand
using namespace std;
int main()
{
int index, i;
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"};
char sentence[100];
srand (time(0));
strcpy(sentence, "\0");
index=rand()%5;
for (i=index; i<index+10; i++)
{
strcat(sentence, article[ rand() % 5 ] );
strcat(sentence, " " );
strcat(sentence, noun[ rand() % 5 ] );
strcat(sentence, " " );
strcat(sentence, verb[ rand() % 5 ] );
strcat(sentence, " " );
strcat(sentence, preposition[ rand() % 5 ] );
strcat(sentence, " " );
strcat(sentence, "." );
}
sentence[0] = toupper(sentence[0]);
cout<<sentence<<endl;
cout<<endl;
return 0;
}