Help refining code

Hi, i was trying to do some homework , i.e a program that constructs sentences due to random number generated. I think its working corrctly but i have a feeling theres an easier and efficient way to do it.. please give me some ideas thanks.!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Program to construct sentences due to random number
*/

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

//Function prototypes, Implemented at bottom!
void createSentence(int);
int randNum(int);
void printSentence(int);

int main()
{
    int seed = 0;
    cout<<"Enter the seed\n";// basicly this will force my randFunction to take a paramete!
    cin>>seed;// user to enter seed for random number generator
    printSentence(seed);
}

int randNum(int a)
{
    srand(a);
    return (rand()%5);
}

void createSentence(int b)
{
    int count  = 0;
    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[80] = {};  //array to carry full sentence!

    //first word should be from article
    if(count ==0) {
        count++;
        strcat(Sentence, article[randNum(b)]);
    }
    //second word should be from noun
    if(count == 1) {
        count++;
        strcat(Sentence, noun[randNum(b)]);
    }
    //third word should be from verb
    if(count == 2) {
        count++;
        strcat(Sentence, verb[randNum(b)]);
    }// Prgram works properly up to HERE!

    //This last two bits of statements fire an error, i dont understand why!
    //fourth word should be from prepostition
    if(count == 3) {
        count++;
        strcat(Sentence, preposition[randNum(b)]);
    }

    //fifth word should be from article
    if(count == 4) {
        count++;
        strcat(Sentence, article[randNum(b)]);
    }

    //sixth word should be from noun
    if(count == 5) {
        count++;
        strcat(Sentence, noun[randNum(b)]);
    }

    cout<<Sentence;

}

void printSentence(int c)
{
    createSentence(c); //call create sentence
}
closed account (Dy7SLyTq)
well... as a c++11 fan i would use <rand>
instead of char * []'s i would use string arrays
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Program to construct sentences due to random number
*/

#include <iostream>
//#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
//using namespace std;

//Function prototypes, Implemented at bottom!
//void createSentence(int);
std::string createSentence() ; // generate a random sentence and return it
int randNum(int); // return pseudo ranom number in [0,n-1]
//void printSentence(int);
void printSentence(std::string);

int main()
{
    int seed = 0;
    std::cout << "Enter the seed: ";// basicly this will force my randFunction to take a paramete!
    std::cin >> seed ;// user to enter seed for random number generator

    // seed the rng once, in main
    std::srand(seed) ;

    // generate and print four random sentences
    for( int i = 0 ; i < 4 ; ++i )
    {
        const std::string sentence = createSentence() ;
        printSentence(sentence);
    }
}

int randNum( int n )
{
    //srand(a);
    // return (rand()%5);

    return std::rand() % n ;
}

std::string createSentence()
{
    //int count  = 0;
    static const int NUM_OPTIONS = 5 ; // avoid magic numbers
    static const char* const article[NUM_OPTIONS] = {"the", "a", "one", "some", "any"};
    static const char* const noun[NUM_OPTIONS] = {"boy", "girl", "dog", "town", "car"};
    static const char* const verb[NUM_OPTIONS] = {"drove", "jumped", "ran", "walked", "skipped"};
    static const char* const preposition[NUM_OPTIONS] = {"to", "from", "over", "under", "on"};

    //char Sentence[80] = {};  //array to carry full sentence!

    const std::string space = " " ;

    //first word should be from article
    std::string sentence = article[ randNum(NUM_OPTIONS) ] ;

    //second word should be from noun
    sentence +=  space + noun[ randNum(NUM_OPTIONS) ] ;

    //third word should be from verb
    sentence +=  space + verb[ randNum(NUM_OPTIONS) ] ;

    //fourth word should be from prepostition
    sentence +=  space + preposition[ randNum(NUM_OPTIONS) ] ;

    //fifth word should be from article
    sentence +=  space + article[ randNum(NUM_OPTIONS) ] ;

    //sixth word should be from noun
    sentence +=  space + noun[ randNum(NUM_OPTIONS) ] ;

    //return the sentence with a final period
    return sentence + '.' ;
}

void printSentence( std::string s )
{
    static const char quote = '\'' ;
    std::cout << "sentence: " << quote << s << quote << '\n' ;
}
createSentence could be rewritten like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void createSentence(int b)
{
    int count  = 0;
    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[80] = {};  //array to carry full sentence!

    strcpy(Sentence, article[randNum(b)]);
    strcat(Sentence, noun[randNum(b)]);
    strcat(Sentence, verb[randNum(b)]);
    strcat(Sentence, preposition[randNum(b)]);
    strcat(Sentence, article[randNum(b)]);
    strcat(Sentence, noun[randNum(b)]);

    cout<<Sentence;
}


count and the associated if statements are completely superfluous.

Also, the way you're using srand and rand, only 5 unique random sentences are possible from createSentence.
Awesome!,, Thank you guys.. really appreciate your responses, didnt think to use the std::string class it looks way bettter than the cstring,,

Thanks again!
:)
Topic archived. No new replies allowed.