letter guessing game for class, flipped

This is for my beginning C++ class. My teacher wants us to create a program where the computer tries to guess the players letter, usually within 5 tries. Now I keep getting this "Floating point exception (core dumped)". At first this didn't confuse me when I would choose if the letter guessed was higher or lower wrongly. However when the letter is only one away from the letter I chose, it will give me the same error. I am not using higher or lower in the sense or their int values, but as in A is higher than Z. So when I choose Z, and it guesses Y, I say its lower I get the error. If I chose higher I still should get the error since its out of scope. Well here's 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
  #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//abcdefghijklmnopqrstuvwxyz
using namespace std;

int main()
{
	//srand(time(NULL)); //set the initial random seed
	char guess;
	char begin = 'A'; //65
	char end = 'Z';   //90
	char answer;
	cout << "choose a letter in the alphabet, and I will guess it." << endl;
	int tries =0;
	
	while (answer != 'y' && answer != 'Y')
	{
	srand(time(NULL));// sets the random seed every loop
	//Make a guess
	guess = rand() % (end-begin)+begin;
	//while (guess is not correct)
	//ask if guess is too high or too low
	cout << "I guessed "<< guess << ", was I right?" << endl;
	cout << "If I was right please type y, or yes." << endl;
	cout << "If I am wrong I need to know if the correct letter was [h]igher or [l]ower." << endl;
	cin >> answer;
	//if guess is too high
	if (answer == 'h' || answer == 'H')
	{//adjust higher
		end = guess-1;
		
	}else 
	  {//adjust lower
	  begin = guess+1;
		
	  }
	tries++;
	}
	cout << "finally and it only too me " << tries << " tries." <<endl;
	
	return 0;
} 
I am not using higher or lower in the sense or their int values, but as in A is higher than Z.


not sure what you mean by this. you have begin set to 'A' and end set to 'Z', but then you lie to the program and said your letter 'Z' was lower than 'Y'?
Last edited on
no not exactly if I do lie I do get the error, and actually I just solved my issue with an if statement, here is my revised 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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//abcdefghijklmnopqrstuvwxyz
using namespace std;

int main()
{
	//srand(time(NULL)); //set the initial random seed
	char guess;
	char begin = 'A'; //65
	char end = 'Z';   //90
	char answer;
	cout << "choose a letter in the alphabet, and I will guess it." << endl;
	int tries =0;
	
	while (answer != 'y' && answer != 'Y')
	{
	srand(time(NULL));// sets the random seed every loop
	//Make a guess
	if (begin == end)
	{
		guess = begin;
	}else
	{
	guess = rand() % (end-begin)+begin;
	}
	//while (guess is not correct)
	//ask if guess is too high or too low
	cout << "I guessed "<< guess << ", was I right?" << endl;
	cout << "If I was right please type y, or yes." << endl;
	cout << "If I am wrong I need to know if the correct letter was [h]igher or [l]ower." << endl;
	cin >> answer;
	//if guess is too high
	if (answer == 'h' || answer == 'H')
	{//adjust higher
		end = guess-1;
	}else 
	  {//adjust lower
	  begin = guess+1;
	  }
	tries++;
	cout << "begin = " << begin << " end = " << end << " guess = " << guess << endl;
	}
	cout << "finally and it only too me " << tries << " tries." <<endl;
	
	return 0;
} 


the problem I was having was if the letter was one away and I didn't lie to the computer... as far as integer values A == 65 and Z == 90, but I'm lying to the user that A is higher than Z. I should change the program for does the letter come before or after so that the confusion is lessened.

At this point I do still need that if the user lies the computer tells the user, and prompts for a correction before the random calculation of the guess variable.

Correction, I just tested it again, and it seems that if you lie it still works... :)
Last edited on
btw just for curiosities sake here is my revised code including cin.ignore so that it doesn't keep guessing if the user types out a whole word other than yes

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
	int tries =0;
	char guess;
	char begin = 'A'; 
	char end = 'Z';   
	char answer;
	cout << "choose a letter in the alphabet, and I will guess it." << endl;
		
	while (answer != 'y' && answer != 'Y') //while (guess is not correct)
	{
	srand(time(NULL)); // sets the random seed every loop, if given enough time
	//Make a guess
	if (begin == end)
		guess = begin;
	else
		guess = rand() % (end-begin)+begin;
		
	//ask if guess is before or after
	cout << "I guessed "<< guess << ", was I right?" << endl;
	cout << "If I was right please type y, or yes." << endl;
	cout << "If I am wrong I need to know if the correct letter was [b]efore or [a]fter, your letter." << endl;
	cin >> answer;
	cin.ignore( 256, '\n');
	//if guess is too high
	if (answer == 'b' || answer == 'B')
		//adjust before
		end = guess-1;
	else 
		//adjust after
		begin = guess+1;
	tries++;
	}
	
	cout << "finally and it only too me " << tries << " tries." <<endl;
	
	return 0;
} 


It should take 5 tries or less, unless you lie to it. :)
Last edited on
actually, since you're making a semi-random guess, it could take more. if you make the guess the mid-point between end and begin, then you can guarantee to get it in <=5 tries.
Hmm, I haven't had a test yet that gave more than 5 tries, but I do understand your logic. If I put
1
2
3
4
 if (begin == end)
		guess = begin;
	else
		guess = rand() % (end-begin)+begin;
at the end of the while loop, and make the first guess = 'M', would that work for what you are describing? wait no that will only reset the guess to 'M'... ah if tries is less than 1, the guess = 'M'... should work right? I'll probably test it before you reply :)

ok it is working, and I believe your logic was good, so now I just need to make the computer aware to the user lying.
Last edited on
Topic archived. No new replies allowed.