Program ends unexpectedly

I'm making a grammar-based program for school. This is just the beginning of it, but once it reaches the function 'AskQuestion()' it just stops and the program terminates once you hit enter. What do I need to do to make it execute the 'AskQuestion()' function? (That would be at line 302 I think.)

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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* PROJECT: Excelcior 2.0 */

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

/* Global variables because I don't want to mess with local variables */

vector<string> Nouns, Verbs, Adjectives, Adverbs;
string ans1;

/* Function prototypes */

/* FillVectors also randomizes the vectors */
void FillVectors();

/* Set up the question and display it */
void AskQuestion();

bool CheckAns( char, string );

void WelcomeScreen( char );

/* Main function */
int main()
{
	WelcomeScreen( 1 );

	cin.get();
	return 0;
}

/* Function Definitions */

void FillVectors()
{
	/* Assign nouns */

    Nouns.push_back( "wombat" );
	Nouns.push_back( "monk" );
	Nouns.push_back( "vestry" );
	Nouns.push_back( "website" );
	Nouns.push_back( "whistles" );
	Nouns.push_back( "thistles" );
	Nouns.push_back( "roustabout" );
	Nouns.push_back( "otter" );
	Nouns.push_back( "zombie-flower" );
	Nouns.push_back( "jello" );

	/* Assign verbs */

	Verbs.push_back( "entwine" );
	Verbs.push_back( "shiver" );
	Verbs.push_back( "carve" );
	Verbs.push_back( "render" );
	Verbs.push_back( "divest" );
	Verbs.push_back( "ramble" );
	Verbs.push_back( "snipped" );
	Verbs.push_back( "snapped" );
	Verbs.push_back( "disappear" );
	Verbs.push_back( "reclaim" );

	/* Assign adjectives */

	Adjectives.push_back( "arduous" );
	Adjectives.push_back( "belated" );
	Adjectives.push_back( "threadbare" );
	Adjectives.push_back( "limber" );
	Adjectives.push_back( "akimbo" );
	Adjectives.push_back( "indolent" );
	Adjectives.push_back( "crooked" );
	Adjectives.push_back( "unparalleled" );
	Adjectives.push_back( "sprightly" );
	Adjectives.push_back( "nappy" );
	Adjectives.push_back( "magenta" );

	/* Assign adverbs */

	Adverbs.push_back( "chivalrously" );
	Adverbs.push_back( "strangely" );
	Adverbs.push_back( "ominously" );
	Adverbs.push_back( "genetically" );
	Adverbs.push_back( "comfortably" );
	Adverbs.push_back( "perfectly" );
	Adverbs.push_back( "transparently" );
	Adverbs.push_back( "superficially" );
	Adverbs.push_back( "jovially" );
	Adverbs.push_back( "deciduously" );

	/* Randomize each vector */

	random_shuffle( Nouns.begin(), Nouns.end() );
	random_shuffle( Verbs.begin(), Verbs.end() );
	random_shuffle( Adjectives.begin(), Adjectives.end() );
	random_shuffle( Adverbs.begin(), Adverbs.end() );
}

void AskQuestion()
{
    vector<string> temp;

    /* Select the first value from each already-randomized vector */
    /* and add it to the temp vector, from which the question will */
    /* be formed. */

    temp.push_back( Nouns[0] );
    temp.push_back( Verbs[0] );
    temp.push_back( Adjectives[0] );
    temp.push_back( Adverbs[0] );
    
    /* Shuffle the temp; i.e. the possible answers to the question */

    random_shuffle( temp.begin(), temp.end() );

    /* Choose which part of speech to ask for */
    
    char tchar = ( rand() % 4 ) + 1;

    switch( tchar )
    {
        case '1':
        cout << "Identify the noun from the choices below. \n\t(Enter the correct answer.)" << endl;
        cout << "\t" << temp[0] << endl;
        cout << "\t" << temp[1] << endl;
        cout << "\t" << temp[2] << endl;
        cout << "\t" << temp[3] << endl << endl;

        cin >> ans1;

        if( CheckAns( 1, ans1 ) == true )
        {
            cout << "\n\n\tCorrect!" << endl;
        }

        else
        {
            cout << "\n\n\tIncorrect." << endl;
        }

        break;

        case '2':
        cout << "Identify the verb from the choices below. \n\t(Enter the correct answer.)" << endl;
        cout << "\t" << temp[0] << endl;
        cout << "\t" << temp[1] << endl;
        cout << "\t" << temp[2] << endl;
        cout << "\t" << temp[3] << endl << endl;

        cin >> ans1;

        if( CheckAns( 2, ans1 ) == true )
        {
            cout << "\n\n\tCorrect!" << endl;
        }

        else
        {
            cout << "\n\n\tIncorrect." << endl;
        }

        break;

        case '3':
        cout << "Identify the adjective from the choices below. \n\n\t(Enter the correct answer.)" << endl;
        cout << "\t" << temp[0] << endl;
        cout << "\t" << temp[1] << endl;
        cout << "\t" << temp[2] << endl;
        cout << "\t" << temp[3] << endl << endl;

        cin >> ans1;

        if( CheckAns( 3, ans1 ) == true )
        {
            cout << "\n\n\tCorrect!" << endl;
        }

        else
        {
            cout << "\n\n\tIncorrect." << endl;
        }

        break;

        case '4':
        cout << "Identify the adverb from the choices below. \n\n\t(Enter the correct answer.)" << endl;
        cout << "\t" << temp[0] << endl;
        cout << "\t" << temp[1] << endl;
        cout << "\t" << temp[2] << endl;
        cout << "\t" << temp[3] << endl << endl;

        cin >> ans1;

        if( CheckAns( 4, ans1 ) == true )
        {
            cout << "\n\n\tCorrect!" << endl;
        }

        else
        {
            cout << "\n\n\tIncorrect." << endl;
        }

        break;
    }

    /* Finish up the function */
}

bool CheckAns( char choice, string answer )
{
    if( choice == '1' )
    {
		if( ( find( Nouns.begin(), Nouns.end(), answer ) ) == Nouns.end() )
        {
            return( false );
        }
    }

    else if( choice == '2' )
    {
        if( ( find( Verbs.begin(), Verbs.end(), answer ) ) == Verbs.end() )
        {
            return( false );
        }
    }

    else if( choice == '3' )
    {
        if( ( find( Adjectives.begin(), Adjectives.end(), answer ) ) == Adjectives.end() )
        {
            return( false );
        }
    }

    else if( choice == '4' )
    {
        if( ( find( Adverbs.begin(), Adverbs.end(), answer ) ) == Adjectives.end() )
        {
            return( false );
        }
    }

    else
    {
        return( false );
    }

    return( true );
}

void WelcomeScreen( char count = 1 )
{
	string temp1;

    cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << endl;
	cout << "   Welcome to GRAMMAR EXCELCIOR 2.0" << endl;
	cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << endl;

	cout << "\n\t[Press enter to continue]" << endl;
	cin.get();

	cout << "\n\tInstructions:" << endl;
	cout << "\t----------------------------" << endl;
	cout << "\t You will be asked to identify a certain part of speech" << endl;
	cout << "\t from a list of four words. Please enter your answer in" << endl;
	cout << "\t lowercase, just as it is shown to you." << endl;
	cout << "\n\t [Press enter to continue]" << endl;
	cin.get();

	cout << "\n\tFor example..." << endl;

	do{
		cout << "\n\t Identify the verb from the choices below." << endl;
		cout << "\n\t\tblubber" << endl;
		cout << "\n\t\tovertly" << endl;
		cout << "\n\t\tbluish-gray" << endl;
		cout << "\n\t\tsprint\n\t";

		cin >> temp1;

		if( temp1 == "sprint" )
			break;
		else
			cout << "\n\t 'Fraid not. Try again." << endl;
	} while( temp1 != "sprint" );

	cout << "\n\tGood job. Now it's time for the real game. (You get points.)" << endl;
	cout << "\n\t[Press enter to continue]" << endl;
	cin.get();

	FillVectors();

	cout << "\n\t--------------------" << endl;
	cout << "\n\t  Question " << count << endl;
	cout << "\n\t--------------------" << endl;

	AskQuestion();
}
Your main problem is that you're assigning tchar a numeric value, but then testing it against a character value in the switch. Remove the single quotes around the numbers in the case statements. Do the same for the conditions of the ifs in CheckAns.

BTW, Excelsior is spelled with an "s". And in the first example, both "blubber" and "sprint" can be both nouns or verbs. And you have some overly long lines; split them. And you use endl when a simple \n within the string will do. And you're using chars as ints for no good reason. And you're using C-style (multi-line) comments for single line comments. And AskQuestion has a lot of repetition that could easily be removed. And:

/* Global variables because I don't want to mess with local variables */

I guess that's okay, as long as you're not planning to ever become a real programmer.
Another problem you will run into is that you aren't seeding your random number function. Add this line before you generate your random number.

srand( static_cast<unsigned>( time( 0 ) ) );

Even with this though, don't get too used to generating random numbers this way, it's the poor man's way.
Last edited on
/* Global variables because I don't want to mess with local variables */

............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\
Even with this though, don't get too used to generating random numbers this way, it's the poor man's way.


Um, what? The only thing it isn't good enough for is cryptography, in which case you would be using something like boost's random generator for it.
Um, what? The only thing it isn't good enough for is cryptography, in which case you would be using something like boost's random generator for it.


I'm just saying that the numbers that you get from this approach are far from random. You can do things without using boost that would improve the "randomness" of your number generation that aren't even that complicated, a few lines. I know for this kind of stuff, it wouldn't matter, maybe I'm just picky.
Facepalm! LOL!
Topic archived. No new replies allowed.