how to pass vectors through functions

Hello,

I was wondering how I can pass vectors. I am just finished my first hangman game and wanted to fine tune it. As you can see, I declared 3 global vectors.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

vector<char> board;
vector<string> body (6);
vector<char> wrongChar (6);

How can I pass it through functions? Full program is below and any criticism is appreciated. Thanks in advance!
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

vector<char> board;
vector<string> body (6);
vector<char> wrongChar (6);
int correct=-1;
void menu();

string wordSelect()
{
    string wordBank[10]={"campaign","idiom","converse","ignorance","manufacture","document","individual","literate","assistance","execution"};
    string word;
    srand(time(0));
    int rD=rand()%(10+1)+0;
    word=wordBank[rD];
    return word;
}

void pressEnter(string conWord)
{
    cout << string(3,'\n'); // MAKES SCREEN CLEANER AND MORE READABLE
    cout << "Press 'Enter' to " << conWord << "." << endl;  //conWord is the action
    cin.get(); //TAKES USER'S INPUT
    system("clear"); //CLEARS SCREEN
}

void help()
{
    system("clear");
    cout << "**********" << endl;
    cout << "***Help***" << endl;
    cout << "**********" << endl;
    string(3,'\n');

    cout << "\t WHERE U PUT THE FUCKING RULES" << endl;
    cin.ignore();
    pressEnter("go back to menu");
}
void promptLose(string word)
{
    system("clear");
    cin.ignore();
    cout << "Sorry... You died because of your stupidity!  The word was " << word << "!" << endl;
    pressEnter("go back to menu");
}

void promptWin()
{
    cout << string(3, '\n');
    cout << "Congrats!  You got the word so you didn't die!" << endl;
    pressEnter("go back to menu");
}

char setBoard(int length)
{
    for(int i=0; i<6; i++)
    {
        wrongChar[i]=' ';
    }

    board.resize(length);
    for(int i=0; i<board.size(); i++)
    {
        board[i]='_';
    }
    for(int i=0; i<body.size(); i++)
    {
        body[i]=' ';
    }
}

void printBoard(int incorrect)
{
    if (incorrect>0)
    {
        if (incorrect==1)
        {
            body[0]="0";
        }
        if (incorrect==2)
        {
            body[1]="|";
        }
        if (incorrect==3)
        {
            body[2]="/";
        }
        if (incorrect==4)
        {
            body[3]="\b\\ ";
        }
        if (incorrect==5)
        {
            body[4]="/";
        }
        if (incorrect==6)
        {
            body[5]="\\";
        }
    }
    system("clear");
    cout << "\t " << "|========||" << endl;
    cout << "\t " << "|        ||" << endl;
    cout << "\t " << body[0] << "        ||" << endl;
    cout << "\t" << body[2] << "";
    cout << body[1] << " ";
    cout << body[3] << "      ||" << endl;
    cout << "\t" << body[4] << " ";
    cout << body[5] << "       ||" << endl;
    cout << "\t" <<"          ||" << endl;
    cout << "\t" <<"     =========" << endl;

    cout << endl;

    for(int i=0; i<board.size(); i++)
    {
        cout << board[i];
        cout << " ";
    }
    cout << endl;
    cout << endl;
    cout << "Incorrect Letters" << endl;
    cout << "___________________" << endl;
    cout << "| "<< wrongChar[0] <<"| "<< wrongChar[1] <<"| "<< wrongChar[2] <<"| "<< wrongChar[3] <<"| "<< wrongChar[4] <<"| "<<wrongChar[5]<<"| "<< endl;
    cout << "-------------------" << endl;
}

int replaceChar(string word,char guess,int tempCorrect,int incorrect)
{
    int numLetter=0;
    int length=word.length();
    vector <char> guessRep (length);
    for(int i=0; i<length; i++)
    {
        if (word[i]==guess)
        {
            numLetter++;
            board[i]=guess;
        }
    }
    if (numLetter==1)
    {
        cout << "There is " << numLetter << " " << guess << "!" << endl;
        tempCorrect++;
        correct=0;
    }
    if (numLetter==0)
    {
        cout << "There are no " << guess << "'s!" << endl;
        correct=1;
        if (incorrect<=6)
        {
        wrongChar[incorrect]=guess;
        }
    }
    if (numLetter>=2)
    {
        cout << "There are " << numLetter << " " << guess << "'s!" << endl;
        tempCorrect=tempCorrect+numLetter;
    }
    cin.ignore();
    pressEnter("continue");
    return tempCorrect;
}

int playGame()
{
    char guess;
    string word=wordSelect();
    int length=word.length();
    int tempCorrect=0;
    int incorrect=0;
    int counter=-1;
    setBoard(length);

    while(true)
    {
        printBoard(incorrect);
        cout << "Guess: ";
        cin >> guess;
        tempCorrect=replaceChar(word,guess,tempCorrect,incorrect);
        if (correct==1)
        {
        incorrect++;
        }

        if(tempCorrect==length)
        {
            promptWin();
            break;
        }
        if(incorrect==6)
        {
            promptLose(word);
            break;
        }
    }

    if(tempCorrect>length || tempCorrect<0)
    {
        cout << "Uh Oh..." << endl;
        cout << "~~~~~~~~" << endl;
        cout << "The program came across a problem." << endl;
        cout <<"Exiting...";
        cout << "tempCorrect: " << tempCorrect << endl;
        cout << "---------------" << endl;
        cout << "incorrect: " << incorrect << endl;
        cout << "---------------" << endl;
        exit(0);
    }

}

void menu()
{
    int tempChoice;

    cout << "**************" << endl;
    cout << "*****MENU*****" << endl;
    cout << "**************" << endl;
    string(3,'\n');

    cout << "\t 1) Play" << endl;
    cout << "\t 2) Help" << endl;
    cout << "\t 3) Exit" << endl;
    cin >> tempChoice;

    if(tempChoice==1)
    {
        playGame();
    }

    if(tempChoice==2)
    {
        help();
    }

    if(tempChoice==3)
    {
        exit(0);
    }
    system("clear");
}

int main()
{
    cout << "#####################" << endl;
    cout << "#*******************#" << endl;
    cout << "#******Hangman******#" << endl;
    cout << "#*******************#" << endl;
    cout << "#####################" << endl;
    pressEnter("continue");

    while(true)
    {
        menu();
    }
    return 0;
}
Last edited on
Same way you pass any variable to a function. I see you passing some variables already. Like that. If you want the function to have a copy of the vector, exactly the same. If you want the function not to have a copy, but to work on the original, pass by reference ( http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/ )

exit(0); This is awful. Don't do this. End your program by returning from main.
Last edited on
Same way you pass any variable to a function. I see you passing some variables already. Like that. If you want the function to have a copy of the vector, exactly the same. If you want the function not to have a copy, but to work on the original, pass by reference ( http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/ )

exit(0); This is awful. Don't do this. End your program by returning from main.


Thank you for your response. Can you tell me why it's bad to use exit();? When would be an acceptable time to use it?

Thanks again.
Topic archived. No new replies allowed.