//Odd or Even!
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string qString;
int qNum [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, };
cout << "Ask me a yes or no question!" << '\n';
cout << "Question: ";
cin >> qString;
//This is where I need it to set a number to random!
switch (qNum)
{
case qNum == 1:
cout << "Yes." << '\n';
break;
case qNum == 2:
cout << "No." << '\n';
break;
case qNum == 3:
cout << "Maybe." << '\n';
break;
case qNum == 4:
cout << "Out look good." << '\n';
break;
case qNum == 5:
cout << "Out look not soo good." << '\n';
break;
case qNum == 6:
cout << "Question too hazy." << '\n';
break;
case qNum == 7:
cout << "Possible but not probable" << '\n';
break;
default:
cout << "I have no idea!";
return -1;
}
return 0;
}
I helped you use your case statement "randomly" and moved it to a function. I am not clear how you want to use an array. I think you should restate what you are trying to do.
//Odd or Even!
#include <iostream>
#include <string>
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
usingnamespace std;
int aFunction(int qNum)
{
switch (qNum)
{
case 1:
cout << "Yes." << '\n';
break;
case 2:
cout << "No." << '\n';
break;
case 3:
cout << "Maybe." << '\n';
break;
case 4:
cout << "Out look good." << '\n';
break;
case 5:
cout << "Out look not soo good." << '\n';
break;
case 6:
cout << "Question too hazy." << '\n';
break;
case 7:
cout << "Possible but not probable" << '\n';
break;
default:
cout << "I have no idea!";
return -1;
}
}
int main ()
{
//This is where I need it to set a number to random!
srand (time(NULL)); // seeding random number
int timeToRunLoop = 100;
for (int i = 0; i < timeToRunLoop; i ++)
{
aFunction(rand()%7);
}
return 0;
}
Ask me a yes or no question!
Question: r
No.
Question too hazy.
I have no idea!No.
I have no idea!Maybe.
Question too hazy.
I have no idea!Out look not soo good.
Out look good.
Yes.
Maybe.
I have no idea!I have no idea!No.
... 100 times...
#include <iostream>
#include <ctime>
#include <string>
usingnamespace std;
int main()
{
srand((unsigned)time(0));
string qString;
string qNum[] { "Yes.", "No.", "Maybe.", "Outlook good.", "Outlook, not so good.", "Question, too hazy.", "Possible, but not probable", "I have no idea!","Ask again, later" };
int rand_answer;
cout << "Ask me a yes or no question!" << '\n';
cout << "Question: ";
cin >> qString;
rand_answer = rand() % 9;
//This is where I need it to set a number to random!
cout << qNum[rand_answer] << endl;
return 0;
}
I think you meant cout << qNum[rand() % 9] << endl;
I put it with getting the random number first, then using that variable to cout the result, because I figured it would be easier for FireCoder to understand, at this point. But now, (s)he knows 2 ways it can be done.
#include <iostream>
#include <ctime>
#include <string>
usingnamespace std;
int main()
{
srand((unsigned)time(0));
const string qNum[] { "Yes.", "No.", // now wrapped to avoid ugly long line
"Maybe.", "Outlook good.", // and note C++11 style init.
"Outlook, not so good.", "Question, too hazy.",
"Possible, but not probable", "I have no idea!",
"Ask again, later" };
constint answer_count = sizeof(qNum)/sizeof(qNum[0]); // calc num of elems
cout << "Ask me a yes or no question!" << '\n';
cout << "Question: ";
string qString;
cin >> qString;
int rand_answer = rand() % answer_count; // better than magic number!
//This is where I need it to set a number to random!
cout << qNum[rand_answer] << endl;
return 0;
}