Making functions of completed codes

I am trying to make 2 functions for 2 different codes that I had previously done. One of which is this. I am trying to make a function for prompting the question and one to spit out a pseudo-random result. I know how to make a function but I would like some suggestions in how to go around.
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
#include <iostream>
#include <iomanip>
#include <cstdlib>  
#include <ctime>       
#include <string>
using namespace std;

int main()
{
	int x, seed;
	string y = " ";
	char quit = ' ';
	bool run = true;
	while (run){
			cout << "Ask me a question.\n";
			cin.ignore();
			getline(cin, y);
			srand(time(NULL));
			x = rand() % 16 + 1;
			switch (x)
			{
			case 1:
				cout << "Ask again\n";
				break;
			case 2:
				cout << "Question too vague.\n";
				break;
			case 3:
				cout << "Don't count on it.\n";
				break;
			case 4:
				cout << "Yes.\n";
				break;
			case 5:
				cout << "As I see it, yes.\n";
				break;
			case 6:
				cout << "My reply is no.\n";
				break;
			case 7:
				cout << "Cannot predict now.\n";
				break;
			case 8:
				cout << "Outlook good.\n";
				break;
			case 9:
				cout << "You may rely it.\n";
				break;
			case 10:
				cout << "You will have to wait.\n";
				break;
			case 11:
				cout << "Sure, why not?\n";
				break;
			case 12:
				cout << "Go for it!\n";
				break;
			case 13:
				cout << "Very doubtful.\n";
				break;
			case 14:
				cout << "Concetrate and ask again.\n";
				break;
			case 15:
				cout << "Who knows.\n";
				break;
			case 16:
				cout << "The world may never know.\n";
				break;
			}
			cout << "Would you like quit y/n: ";
			cin >> quit;
			if (quit == 'y')
			{
				run = false;
			}


		}
	return 0;
}
Last edited on
You say you know how to make functions, and you already have the code. So what's your question, really? It seems to me you already have everything you need! :)
I would agree on you there BasV. What I was trying to do is make a part of a program "condensed" by using functions. I finally got this program done using functions.
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
#include <iostream>
#include <iomanip>
#include <cstdlib>  
#include <ctime>       
#include <string>
using namespace std;
void question (string y);
void response (int x);
int main()
{
	int x, seed;
	string y = " ";
	char quit = ' ';
	bool run = true;
	while (run){
		    question (y);
		    response (x);
			switch (x)
			{
			case 1:
				cout << "Ask again\n";
				break;
			case 2:
				cout << "Question too vague.\n";
				break;
			case 3:
				cout << "Don't count on it.\n";
				break;
			case 4:
				cout << "Yes.\n";
				break;
			case 5:
				cout << "As I see it, yes.\n";
				break;
			case 6:
				cout << "My reply is no.\n";
				break;
			case 7:
				cout << "Cannot predict now.\n";
				break;
			case 8:
				cout << "Outlook good.\n";
				break;
			case 9:
				cout << "You may rely it.\n";
				break;
			case 10:
				cout << "You will have to wait.\n";
				break;
			case 11:
				cout << "Sure, why not?\n";
				break;
			case 12:
				cout << "Go for it!\n";
				break;
			case 13:
				cout << "Very doubtful.\n";
				break;
			case 14:
				cout << "Concetrate and ask again.\n";
				break;
			case 15:
				cout << "Who knows.\n";
				break;
			case 16:
				cout << "The world may never know.\n";
				break;
			}
			cout << "Would you like quit y/n: ";
			cin >> quit;
			if (quit == 'y')
			{
				run = false;
			}


		}
	return 0;
}
void question (string y)
{
	cout << "Ask me a question.\n";
	cin.ignore();
	getline(cin, y);
}
void response (int x)
{
	srand(time(NULL));
	x = rand() % 16 + 1;	
}

this was the assignment
Modify the two programs that you wrote for last week by encapsulating some of the code inside functions. Create at least 2 functions per program (in addition to main). Be sure to supply block comments in front of each function to tell about the parameters passed to the function, the return value of the function (if applicable) and a short description of what the function does.

I am a bit confused where I should be giving this block comments my professor is asking.
Last edited on
In your original code you should put the call to srand() outside the loop. You should only call it once.

Okay, how about these functions:

1
2
3
4
5
6
7
8
9
// Ask a question using the prompt and return the response
string askQuestion(const string &prompt);


// print the given message number. This must be between 1 and 16.
printMessage(int msgNum);

// Prompt the user and get a y/n response. If they don't answer y or n, ask them again.  Returns 'y' or 'n'.
char getYesNo(const string &prompt);


With these functions, the main program becomes something like:

1
2
3
4
5
6
7
8
int main()
{
    srand(time(NULL));
    do {
        string question = askQuestion("Ask me a question?");
        printMessage(rand()%16 + 1);
    } while (getYesNo("Would you like to quit? (y/n)") == 'n');
}


}
Topic archived. No new replies allowed.