Learn C++ Programming By Examples by Skudaev Sergey

Learn C++ Programming By Examples by Skudaev Sergey

Author:Skudaev, Sergey
Language: eng
Format: epub
Tags: C++ programming, code examples
Publisher: ComeToPro Consulting
Published: 2014-01-17T00:00:00+00:00


Figure 52. Rhymes game.

AN APPLICATION THAT WRITES POEMS

Let´s practice more with string manipulation and create a program that writes poems.

As the application that writes stories the poet application will take words from arrays of adjectives, nouns and verbs and compose sentences. The difference is that the last words of the sentences must rhyme. An example of C++ poem:

Figure 53. A C++ poem

I created 4 functions:

void Initialize(char noun1[][20],char verb1[][20],char adj1[][20]);

char *ReturnThreeFromTheRight(char*string);

int IsRhymes(char *word1, char *word2);

void WritePoem(char noun1[][20], char verb1[][20], char adj1[][20]);

The Initialize function creates arrays of words from which a poem is constructed.

For each noun you include in the noun array you have to include a rhyme.

You can see it from my array: switch – witch, goat-toad, deal-meal, wolf-golf and so on.

char * noun[] = { "switch", "grain", "goat", "witch", "deal"," food", "stool", "toad", "fate", "feet", "knish", "meal", "meat", "mood", "wolf", "house", "rain", "sleet", "train", "golf", "skate", "tool", "wood", "wish", "peat", "mouse", "pitch", "chain", "boat", "squeal", "drool", "pool", "plate", "beet", "catfish", "seat","rolf","grouse","coat","woad","road", "food"};

Then you are creating the array of verbs and adjectives from any words. I included verbs in the past tense.

char *verb[] = { ….};

char *adj[] = { ….};

The srand () function gives the random function a new seed.

You should include <ctime> header file.

srand( time(0) );

Then I generate a random number from 0 to 41 because I have 42 nouns..

i = rand()% 41;

I generate an index for the noun1[0], noun1[1], noun1[3], noun1[4] and copy nouns with that index from the noun array to the noun1[0], noun1[1], noun1[3], noun1[4].

strcpy_s(noun1[0], noun[i]);

In the next step I walked through the array of nouns and looked for one that is the rhyme for noun1[1].

When I found a rhyme, I copied it to the noun1[2]. The IsRhymes function is similar to one from the previous example.

for (k = 0; k<27; k++)

{

if (IsRhymes(noun1[1], noun[k]))

strcpy_s(noun1[2], noun[k]);

}

There is the possibility that I will find for noun1[2] the same word as noun1[1] in the loop.

So, I check if the words are the same. If they are, then I will walk through the noun array again but this time from the end of the array to the beginning and look for a rhyme. When I find it, I will copy it to the noun1[2]. There is still the possibility that noun1[1] and noun1[2] are the same word, but it is lower than before.

if (strcmp(noun1[1], noun1[2]) == 0)

{

for (n = 41; n >0; n--)

{

if (IsRhymes(noun1[1], noun[n]))

strcpy_s(noun1[2], noun[n]);

}

}

You can try to find a better solution to eliminate such possibility. In the same way you generate noun1 [5] that is a rhyme for noun1 [4].

In the last function WritePoem(), there are the final 4 lines of code that construct a poem.

void WritePoem(char noun1[][20], char verb1[][20], char adj1[][20])

{

std::cout << "A " << adj1[0] << " " << noun1[0] << " like a " << adj1[1] << " " << noun1[1] << std::endl;

std::cout << verb1[0] << " a " << adj1[2] << " " << noun1[2] << std::endl;

std::cout << " A " << adj1[3] << " " << noun1[3] << " like a " << adj1[4]



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.