Random number generator

I'm trying to create a magic 8-ball style program for class. This program meets all the specifications our professor set forth except for this: If an answer has already been used by the program, the program continues to randomly select an answer until it selects an unused answer. I would like this to be implemented in chooseAnswer. Any help would be appreciated.


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
  /*	Author: Brad Beaubien
	Created on: 08.07.2015
	"Magic 8-Ball"
	This program will allow a user to ask a total of 10
	questions, each yielding a different answer.
*/

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

void welcome()
{
	cout << "         o----------------o\n";
	cout << "         |  Magic 8-Ball  |\n";
	cout << "         o----------------o\n";
	cout << "This program will allow you to ask a total \nof 10 questions, each time giving you an answer.\n\n";
}

int inputQuestion(int questionNumber)
{
	string userQuestion;
	cout << "\nQuestion #" << questionNumber << ":\n";
	cout << "What question would you like to know the answer to?\n";
	getline(cin, userQuestion);
	questionNumber += 1;
	return(questionNumber);
}

int chooseAnswer(int randomAnswer)
{
	randomAnswer = rand() % 10 + 1;
	return(randomAnswer);
}

void outputAnswer(int randomAnswer)
{
	if (randomAnswer == 1)
		cout << "Yes.\n";
	else if (randomAnswer == 2)
		cout << "No.\n";
	else if (randomAnswer == 3)
		cout << "It is highly unlikely.\n";
	else if (randomAnswer == 4)
		cout << "Maybe.\n";
	else if (randomAnswer == 5)
		cout << "It's hard to tell.\n";
	else if (randomAnswer == 6)
		cout << "Ask again later.\n";
	else if (randomAnswer == 7)
		cout << "I can't answer that.\n";
	else if (randomAnswer == 8)
		cout << "Absolutely.\n";
	else if (randomAnswer == 9)
		cout << "Possibly.\n";
	else if (randomAnswer == 10)
		cout << "Deffinitely not.\n";
}

void farewell()
{
	cout << "\nThank you for using 'Magic 8-Ball!' Goodbye!\n";
}

int main()
{
	int randomAnswer;
	welcome();
	for (int questionNumber = 1; questionNumber < 11; ++questionNumber)
	{
		inputQuestion(questionNumber);
		chooseAnswer(randomAnswer);
		outputAnswer(randomAnswer);
	}
	farewell();
	system("pause");
	return 0;
}
My thoughts on the code is incrementing the question count with a function is a bit of an overkill and the chooseAnswer should return an answer.

You would need to record if an answer has been used if you don't want it repeated.

1
2
3
int answers[9];   // answer[0] not used
for (i=0;i<9;i++)
    answers[i]=0;


then
1
2
3
4
5
6
randomAnswer = chooseAnswer();
while (answers[randomAnswer] != 0)
{
    randomAnswer = chooseAnswer();
}
answer[randomAnswer]=1;   // flag the question has been used 

Last edited on
closed account (NUj6URfi)
You should use a switch instead of all those else ifs.
You should use a lookup table instead of all those else ifs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const char* responses[] =
{
	"Yes.\n",
	"No.\n",
	"It is highly unlikely.\n",
	"Maybe.\n",
	"It's hard to tell.\n",
	"Ask again later.\n",
	"I can't answer that.\n",
	"Absolutely.\n",
	"Possibly.\n",
	"Definitely not.\n"
};

void outputAnswer(int randomAnswer)
{
	if ((0 < randomAnswer) && (randomAnswer <= 10))
		cout << responses[ randomAnswer - 1 ];
}

Make sure to watch your spelling when turning in homework.

Hope this helps.

[edit] Oops! Fixed as per andywestken
Last edited on
As randomAnswer is between 1 and 10 for an array of size 10, need to subtract one when accessing an array element.

1
2
3
4
5
void outputAnswer(int randomAnswer)
{
	if ((0 < randomAnswer) && (randomAnswer <= 10))
		cout << responses[ randomAnswer - 1 ];
}


Andy
Thank you all for the awesome responses! The lookup table really helped simplify the code.
Topic archived. No new replies allowed.