Mistery Close

Hello,
I am unsure why my program is closing.

First this function is ran
1
2
3
4
5
6
7
void display()
{
	optionDisplay();
	string inPut;
	getline( cin, inPut );
	optionInput( inPut );
}


It goes through repeatedly on every option so far except when a the prime selection is selected.
It goes through the prime function and at the end returns back to the display function just like the rest.

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
//prime option
void prime()
{
	//variables
	int number;
	bool isPrime = true;

	cout << "P R I M E   N U M B E R   T E S T\n" << setw(26) << setfill('=')
		<< "=\n" << setfill(' ') << "Enter int > 0: ";

	cin >> number;

	if ( number < 2 )
	{
		isPrime = false;
	}

	if ( number > 2 && ( number % 2 ) == 0 )
	{
		isPrime = false;
	}
	
	for(int i = 2; i < number; i++ )
	{
		if ( ( number % i ) == 0)
		{
			isPrime = false;
			break;
		}
	}

	if( isPrime == true )
	{
		cout << number << " is a prime number\n" << endl;
	}else{
		cout << number << " is NOT a prime number\n" << endl;
	}

	display();//Return to program start
}


Except it automatically crashes after. I followed debug & it went straight through the display function & never stopped at getline. Any ideas?
What calls prime() ? Is it optionDisplay()? Post it.
Though it would be best if you posted your whole program, or a compilable relevant subset that has the same problem, if there is a lot of code.
Last edited on
Your question is not clear! what's the relation of top code with the down one?
Here is all the code
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
/*
Name: Mark Andrews
G-Number: G03611789
Assignment: Lab 4
Version: 1.0
Team: Self
*/

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

//Title, display, input function declorations
void title();
void display();
void optionDisplay();
void optionInput( string inPut );

//Option function declorations
void prime();
void square();
void cube();
void mult();
void mersenne();
void distance();
void palidrome();
void count();
void add();
void subtract();
void quit();

//Start the program
int main ()
{
	title();//Display title
	display();//Beguine the programs operations

	return 0;
}

//Title display function
void title()
{
	cout << setw(80) << setfill('=') << "=\n"
		<< setw(52) << "WELCOME TO MR.CALCULATOR"<< setw(28) << "=\n"
		<< setw(80) << setfill('=') << "=\n" << endl;
}

//Program display menu & operations
void display()
{
	optionDisplay();
	string inPut;
	getline( cin, inPut );
	optionInput( inPut );
}

//Display options function
void optionDisplay()
{
	cout << "Available options:\n\
 0. PRIME - Determine if a number is prime\n\
 1. SQUARE - Determine if a number is a perfect square\n\
 2. CUBE - Determine if number is perfect cube\n\
 3. MULT - Display a multiplication table\n\
 4. MERSENNE - List Mersenne prime numbers\n\
 5. DISTANCE - Calculate distance between two points\n\
 6. PALIDROME - Determine if string is palindrome\n\
 7. COUNT - Count words in a file\n\
 8. ADD - Add two big numbers\n\
 9. SUBTRACT - Subtract two big numbers\n\
10. QUIT - Exit the program\n\n\
	Enter keyword or option index: ";
}

//Funcrion to find what option the char relates to
string inPutCharFix( string inPut )
{
	//variable for string return
	string optionReturn;

//No options have three identical starting characters
	inPut.resize( 3 );
	//Transform the name to uppercase.
	transform(inPut.begin()
		, inPut.end()
		, inPut.begin()
		, ::toupper);	

	//I KNOW IT'S A BLOODY MESS! But I didn't feel like an array...
	if( inPut == "PRI" )
	{
		optionReturn = "0";
	}else if( inPut == "SQU" ){
		optionReturn = "1";
	}else if( inPut == "CUB" ){
		optionReturn = "2";
	}else if( inPut == "MUL" ){
		optionReturn = "3";
	}else if( inPut == "MER" ){
		optionReturn = "4";
	}else if( inPut == "DIS" ){
		optionReturn = "5";
	}else if( inPut == "PAL" ){
		optionReturn = "6";
	}else if( inPut == "COU" ){
		optionReturn = "7";
	}else if( inPut == "ADD" ){
		optionReturn = "8";
	}else if( inPut == "SUB" ){
		optionReturn = "9";
	}else if( inPut == "QUI" ){
		optionReturn = "Q";
	}else{
		optionReturn = "Invalid";
	}
	
	return optionReturn;//return the correct option
}

//Option selection function
void optionInput( string inPut )
{
//Variables
	//copy over the original input just incase input is invalid
	string originalInput = inPut;
	char firstChar = inPut.at(0);

//First fix the input if it is not numeric
	if( '0' > firstChar || '9' < firstChar )
	{
		inPut = inPutCharFix( inPut );
		firstChar = inPut.at(0);
	}

//If 10 (quit) is selected
	if( inPut == "10" )
	{
		firstChar = 'Q';
	}

//Now figure out & call the function selected
	switch ( firstChar ) {
		case '0':
			prime();
			break;
		case '1':
			square();
			break;
		case '2':
			cube();
			break;
		case '3':
			mult();
			break;
		case '4':
			mersenne();
			break;
		case '5':
			distance();
			break;
		case '6':
			palidrome();
			break;
		case '7':
			count();
			break;
		case '8':
			add();
			break;
		case '9':
			subtract();
			break;
		case 'Q':
			break;
		default:
			cout << "?Unrecognized option entered: " << inPut
				<< "\n" << endl;
			display();//Return to program start
	}
}

//prime option
void prime()
{

	//variables
	int number;
	bool isPrime = true;

	cout << "P R I M E   N U M B E R   T E S T\n" << setw(26) << setfill('=')
		<< "=\n" << setfill(' ') << "Enter int > 0: ";

	cin >> number;/*

	if ( number < 2 )
	{
		isPrime = false;
	}

	if ( number > 2 && ( number % 2 ) == 0 )
	{
		isPrime = false;
	}
	
	for(int i = 2; i < number; i++ )
	{
		if ( ( number % i ) == 0)
		{
			isPrime = false;
		}
	}

	if( isPrime == true )
	{
		cout << number << " is a prime number\n" << endl;
	}else{
		cout << number << " is NOT a prime number\n" << endl;
	}
*/
	display();//Return to program start
}

//square option
void square()
{

	display();//Return to program start
}

//cube option
void cube()
{

	display();//Return to program start
}

//mult option
void mult()
{

	display();//Return to program start
}

//mersenne option
void mersenne()
{

	display();//Return to program start
}

//distance option
void distance()
{

	display();//Return to program start
}

//palidrome option
void palidrome()
{

	display();//Return to program start
}

//count option
void count()
{

	display();//Return to program start
}

//add option
void add()
{

	display();//Return to program start
}

//subtract option
void subtract()
{

	display();//Return to program start
}
Last edited on
I posted all of the code now. I can't even make a simple program that gets this error.

***SOLVED***

If I replace getline with cin >> inPut;
Then everything works. I have no Idea why it freaks out otherwise.
Last edited on
That's because cin >> leaves the end of line in input stream. This end of line is found by getline so it looks as if the input was skipped. In this situation getline reads an empty string and the program crashes when you try to access its non existent first character.

By the way, to use long strings you can write
1
2
3
cout << "Available options:\n\"
        " 0. PRIME - Determine if a number is prime\n\"
        " 1. SQUARE - Determine if a number is a perfect square\n\" 
Now you can align the strings as you like.

Also, using recursion to repeat things is not a good idea. In most cases it won't harm you, but at times it may be hard to debug and is generally disliked. Note that in some cases use of recursion may cause a crash due to stack overflow. You could easily avoid recursion by wrapping lines 54-57 in a do while loop.
Topic archived. No new replies allowed.