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]
#include <iostream>
usingnamespace 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()
{
constint 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;
}
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.
#include <iostream>
#include <string>
usingnamespace 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()
{
constint 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?