Question:
C++: Array of strings vs array of pointers?
Aria
2011-05-02 18:49:57 UTC
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
#include
#include
#include
#include
#include
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!
Three answers:
Cubbi
2011-05-02 20:24:18 UTC
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

#include

#include

#include

#include

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( 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
2011-05-03 02:39:00 UTC
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
2011-05-03 03:29:11 UTC
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

#include

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();

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...