Initializing string array

Hey everyone. I'm writing this code for class and seem to have trouble initializing a string array. The program is supposed to ask a user if they'd like advice. If they say yes, I'm supposed to use a function to randomly cout an element from the initial global array.

My compiler is telling me that I didn't name a type from cliche[0]-[9]

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
#include <iostream>

using namespace std;

string cliche[9];
cliche[0] = "Try to be a rainbow in someone's cloud.";
cliche[1] = "Never miss a good chance to shut up.";
cliche[2] = "Here's some advice. Stay alive.";
cliche[3] = "Never ruin an apology with an excuse.";
cliche[4] = "Don't ever take a fence down until you know why it was put up.";
cliche[5] = "Write with the door closed, rewrite with the door open.";
cliche[6] = "Always speak politely to an enraged dragon.";
cliche[7] = "A wise man gets more use from his enemies than a fool from his friends.";
cliche[8] = "Don't try to solve serious matters in the middle of the night.";
cliche[9] = "Only boring people get bored.";

string advice()
{
    const int a = rand() %10 + 0;
    string adv = cliche[a];
    return adv;
}

int main()
{
    string answer = yes;
    string randAdv;
    while (answer == yes)
    {
        cout << "Would you like some advice?" << endl;
        cin >> answer;
        randAdv = advice();
        cout >> randAdv;
    }


    return 0;
}


Any hints would be greatly appreciated. Thanks.
All the assignments should be done inside a function, and you don't need the global variable either, and by the way your array size is one too small. You have 10 string assignments but your array only has room for 9.

You are also missing an include file which is required to use the string class.

Jim
Last edited on
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
#include <iostream>
#include <string>

using namespace std;

string cliche[10];

cliche[0] = "Try to be a rainbow in someone's cloud.";
cliche[1] = "Never miss a good chance to shut up.";
cliche[2] = "Here's some advice. Stay alive.";
cliche[3] = "Never ruin an apology with an excuse.";
cliche[4] = "Don't ever take a fence down until you know why it was put up.";
cliche[5] = "Write with the door closed, rewrite with the door open.";
cliche[6] = "Always speak politely to an enraged dragon.";
cliche[7] = "A wise man gets more use from his enemies than a fool from his friends.";
cliche[8] = "Don't try to solve serious matters in the middle of the night.";
cliche[9] = "Only boring people get bored.";


string advice()
{
    const int a = rand() %10 + 0;
    string adv = cliche[a];
    return adv;
}

int main()
{
    string answer = yes;
    string randAdv;
    while (answer == yes)
    {
        cout << "Would you like some advice?" << endl;
        cin >> answer;
        randAdv = advice();
        cout >> randAdv;
    }


    return 0;
}


I fixed a couple of the things you pointed out. My professor specifically asked for us to declare the array as global, how would I still be able assign it within a function?
YOu can initialize the global array as follows:
1
2
3
4
5
string cliche[10] = 
{   "Try to be a rainbow in someone's cloud.",
     "Never miss a good chance to shut up.",
// etc
};


Or you can put lines 8-17 inside a function:
1
2
3
4
5
6
 
void initialize_cliches ()
{  cliche[0] = "Try to be a rainbow in someone's cloud.";
    cliche[1] = "Never miss a good chance to shut up.";
// etc
}



Topic archived. No new replies allowed.