Run time error?

A bit longwinded: I am trying to create a program which is a sort of guessing game from Stroustrups book (chapter 4, question 4). The user thinks of a number, and my program attempts to guess it 7 guesses. The way my program is meant to work is by halving the range of numbers which the answer could be in each time.

E.g. You choose 70. Program says closer to (0,100) ---> (50,100) ---> (50,75) ----> (62.5,75) until there is such a small range there can only be 1 integer value. I understand there could be other ways to do this with only integers as it is an integer game, but for the purpose of my code I have used doubles for the hell of it. Wondering if anyone could shed any light on why I am getting the following message after my asking whether closer to 0 or 100: "Run Command: line 1: 18371 Segmentation fault: 11 ./"$2" "${@:3}".

If possible, a corrected version would be greatly appreciated as this book is the only experience I have with coding.

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
#include <iostream>
#include <vector>
#include <cmath>

//This program is a guessing game from 1-100

using namespace std;

int main()
{
	double max = 100, low = 1;
	int guess;
	vector<double> loworhigh1,loworhigh2;
	double answer;
	int i=0;
	 
	cout << "Think of a number between " << low << " and " << max <<"\n";
	cin >> guess;
	
	for (i=0;i<7;++i)
	{
		cout << "Is your number closer to " << low << " or " << max <<"?\n";
		if (answer == low)
		{
		loworhigh1.push_back(low);
		loworhigh2.push_back(max - (max-low)/2);
		} 			
		else if (answer == max)
		{
		loworhigh1.push_back(max -(max-low)/2);
		loworhigh2.push_back(max);
		}
		low = loworhigh1[i];
		max = loworhigh2[i];
	}
	
return 0;
	
}


Last edited on
Could you please use the code tags when posting your code?
Example cout<<"Hello World"; It is shown like this because it's between [code] and ['/code] but without the '
Last edited on
answer is an uninitialized variable.

When you compare answer to low that's going to be false. Then you compare answer to high, that is also going to be false. As a result, you never push anything to lowhigh1 or lowhigh2.
Your vectors are going to be empty. Any reference to vector elements[i] are going to be invalid and cause a segmentation fault.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
error C4700: uninitialized local variable 'answer' used


Since it isn't initialized or assigned any sort of value it is unlikely it is equal to either low or max, meaning that nothing will be appended to loworhigh1 or loworhigh2 and indexing those empty containers will result in undefined behavior.

Pay attention to the warnings/errors generated by your compiler.

Thanks - ended up realising confused myself with my double variable answer, and I also assumed I had added a string variable called response. Final 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
#include <iostream>
#include <vector>
#include <cmath>

//This program is a guessing game from 1-100

using namespace std;

int main()
{
	double max = 100, low = 0;
	int guess;
	vector<double> loworhigh1,loworhigh2;
	
	double answer=0;
	string response;
	int i=0;
	 
	cout << "Think of a number between " << low << " and " << max <<"\n";
	cin >> guess;
	
	for (i=0;i<7;++i)
	{
		cout << "Is your number closer to " << low << " or " << max <<"?\n";
		cout << "Press l for lower, u for upper.\n";
		cin >> response;
		if (response == "l")
		{
		loworhigh1.push_back(low);
		loworhigh2.push_back(max - (max-low)/2);

		} 			
		else if (response == "u")
		{
		loworhigh1.push_back(max -(max-low)/2);
		loworhigh2.push_back(max);
		}
		low = loworhigh1[i];
		max = loworhigh2[i];
		
	}
	
	cout << "Your answer is: " << round((loworhigh1[6]+loworhigh2[6])/2);
	
return 0;
	
}
	

	

Last edited on
Topic archived. No new replies allowed.