LATEST ADDITIONS:



 

C++: Array of strings vs array of pointers?

Home > Computers & Internet > Programming & Design > C++: Array of strings vs array of pointers?
MiaMia


These are the instructions for my assignment:

Write an application that uses random-number generation to create sentences. Use four arrays of strings called article, noun, verb and preposition. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is randomly picked, concatenate it to the previous words in the sentence. When the final sentence is output, it should start with a capital letter and end with a period. The application should generate and display 20 sentences.

The article array should contain the articles "the", "a", "one", "some" and "any".

The noun array should contain the nouns "boy", "girl", "dog", "town" and "car".

The verb array should contain the verbs "drove", "jumped", "ran", "walked" and "skipped".

The preposition array should contain the prepositions "to", "from", "over", "under" and "on".

I have this code using an array of pointers

#include "stdafx.h"

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

#include <ctype.h>

using namespace std;

int _tmain()

{

// initialize array of *pointers* //

char *article[] = { "the" , "a", "one", "some", "any" };

char *noun[] = { "boy", "girl", "dog", "town", "car" };

char *verb[] = { "drove", "jumped", "ran", "walked", "skipped" };

char *preposition[] = { "to", "from", "over", "under", "on" };

char sentence[ 100 ] = ""; //completed sentence//

int i;

for ( i = 1; i <= 20; i++ )

{

//choose random parts of sentence//

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, article[ rand () %5 ] );

strcat( sentence, " " );

strcat( sentence, noun[ rand() % 5 ] );

//capitalize first letter//

putchar( toupper ( sentence [0] ));

//add period at end of sentence//

printf( "%s.\n", &sentence[1] );

sentence[ 0 ] = '\0';

}

return 0;

}

How can i rewrite this code to make it have an array of strings instead of pointers?

Any help/hints will be greatly appreciated!

Cubbi
Cubbi

Simply use "string" where you use "char *" (incorrectly, by the way, the proper types for your arrays were "arrays of pointers to const char", such as const char* articles[]), and then use the functions and operators that work with strings (you're using the functions that work with arrays of char instead)

// #include "stdafx.h" // disable precompiled headers in your project options to get rid of this

#include <iostream>

#include <string>

#include <cstdlib>

#include <ctime>

#include <cctype>

const std::string article[] = { "the" , "a", "one", "some", "any" };

const std::string noun[] = { "boy", "girl", "dog", "town", "car" };

const std::string verb[] = { "drove", "jumped", "ran", "walked", "skipped" };

const std::string preposition[] = { "to", "from", "over", "under", "on" };

int main()

{

     std::srand(static_cast<unsigned int>( std::time(NULL) ));

     std::string sentence;

     // initialize array of strings

     for(int i = 1; i <= 20; i++ )

     {

         //choose random parts of sentence//

         sentence = article[ rand() % 5 ] + " "

                      + noun[ rand() % 5 ] + " "

                      + verb[ rand() % 5 ] + " "

                      + preposition[ rand() % 5 ] + " "

                      + article[ rand () %5 ] + " "

                      + noun[ rand() % 5 ];

         //capitalize first letter//

         sentence[0] = std::toupper(sentence[0]);

         //add period at end of sentence//

         sentence += '.';

     }

     std::cout << sentence << '\n';

}

test: https://ideone.com/84Tis

The Phlebob
The Phlebob

Look into the "new" operator as it pertains to arrays, then use it to allocate a new character array for each entry in an array of pointers-to-char. This might work:

char* sentence[ 20 ];

....

sentence[ i ] = new char[ 100 ]; // Not sure of the syntax here.

Hope that helps.

Blackcompe
Blackcompe

I do not recommend using the 'new' keyword, unless you are prepared to clean up your memory. It's not even necessary here; use it when you don't know how big of an array you need (dynamic allocation).

#include <iostream>

#include <string>

using namespace std;

#define STR_LEN 10

int main() {

//Ex1

char strings[][STR_LEN] = {"one", "two", "three", "four"};

//Ex2

string strings2[STR_LEN] = {"one", "two", "three", "four"};

const char* string2Ptr = strings2[0].c_str();

}


Other Questions & Answers

Windows Server 2003...?
I'm in the process of installing Windows Server 2003. I have an old XP machine, which works great, and I'd like to put 2003 on that machine. ... Read more (1 answers)
How do I turn off Ubuntu fancy graphics in 11.04?
This is no rocket science question...I use to use ubuntu 10.10then I saw 11.04.. ooohh goddie :) I thoughtI don't k ... Read more (3 answers)
What is wrong with my wall?
I have a very strange wall in my house. Sometimes it sings. Sometimes it screams. It yawns, meows, makes exploding noises, threatens to kill me, and o ... Read more (2 answers)
How much could I sell my HP mini net book?
It is in great, working condition.The thing is.. the side of the key board are full of teeth marks, because when my puppy used to always che ... Read more (2 answers)
Where to learn javascript?
I wanted to learn javascript to start programming a game but i have no idea where to start. Any suggestions or links to tutorials would be greatly app ... Read more (2 answers)