Printing paragraphs using random strings (Style)

So my program does what it's supposed to do. I'm just wondering if there is anything I can do to make it look better.

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
84
85
86
87
88
89
90
91
  #include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    int count, ncount, vcount, pcount, i;
    string article[7], noun[10], verb[9], prepostion[7];
    string sentence[100];

    ifstream infile("Article2.txt");
    ifstream infile2("Noun2.txt");
    ifstream infile3("Verb2.txt");
    ifstream infile4("Preposition2.txt");

    ofstream outfile("Assignment6p2.txt");

    if( !infile.is_open()) {
        cout << endl << "ERROR: Unable to open file" << endl;
        system("PAUSE");
        exit(1);
    }

    if( !infile2.is_open()) {
        cout << endl << "ERROR: Unable to open file2" << endl;
        system("PAUSE");
        exit(1);
    }

    if( !infile3.is_open()) {
        cout << endl << "ERROR: Unable to open file3" << endl;
        system("PAUSE");
        exit(1);
    }

    if( !infile4.is_open()) {
        cout << endl << "ERROR: Unable to open file4" << endl;
        system("PAUSE");
        exit(1);
    }

    //reads in all the arrays and counts each seperately
        count = 0;
        ncount = 0;
        vcount = 0;
        pcount = 0;

        while (!infile.eof() && infile >> article[count]) {
            count++;
        }
        while (!infile2.eof() && infile2 >> noun[ncount]) {
            ncount++;
        }
        while (!infile3.eof() && infile3 >> verb[vcount]) {
            vcount++;
        }
        while (!infile4.eof() && infile4 >> prepostion[pcount]) {
            pcount++;
        }

        infile.close();
        infile2.close();
        infile3.close();
        infile4.close();

    outfile << "\n\t"; //indentation

    for (i = 0; i < 10; i++) {

        sentence[i] = (article[rand() % count] + ' ' +
                       noun[rand() % ncount] + ' ' +
                       verb[rand() % vcount] + ' ' +
                       prepostion[rand() % pcount] + ' ' +
                       article[rand() % count] + ' ' +
                       noun[rand() % ncount]);

        sentence[i][0] = toupper(sentence[i][0]);

        outfile << sentence[i] << ". ";
        if (i % 2 == 0)
            outfile << endl << endl;
        if (i == 4)
            outfile << "\n\t";
    }
    system("PAUSE");
    return 0;
}
The four if-file-not-opens could be combined into one with ||
1
2
3
4
5
    if(!infile.is_open() || !infile2.is_open() || !infile3.is_open() || !infile4.is_open()) {
        cout << endl << "ERROR: Unable to open file" << endl;
        system("PAUSE");
        exit(1);
    }

But I would just store them in an array instead and loop over them and tell the user the name of the file that could not be opened. This would also simplify the four while loops, because you could also have an array of counts. It would be even better if you could use std::vector and store an arbitrary amount of each part of speech. std::vector would also eliminate the need to store counts separately.

Why store sentences to an array if this program does not use that array later?

Also I think the period on line 83 should be added with the rest of the words on 79.
Topic archived. No new replies allowed.