code giving unintended result

Hi
I need to create a cup and ball guessing game. I am to create a random nbr btw 1 - 6 inclusive. Tries allowed is 3. See my code below ... seemed not to work. Appreciate the help

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 <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main ()
{
char ok = 'y';
char quit = 'q';
int RNbr;
int input;
char responds;

cout <<"Press 'y' to continue OR 'q' to quit: ";
cin >> responds;

if (responds != 'q'){
	cout << "Input an integer between 1 - 6 inclusive: ";
	cin >> input;
}

srand(time(0));	
RNbr = rand()%6 + 1;

if (input = RNbr){
	cout << "You win! ";
	return 1;
}
if (input != RNbr){
	cout << "Incorrect!. Correct number is: " << RNbr << endl;
	int count = 3;
	count--;
	return 2;
}

	return 0;
}
Ok first problem I saw, line 33 is count = 3, then you count --; the next line.

why not just do count =2 ?

Anyway, fix that and let me know what area of the code your having trouble with.
Hi Mr Adams
thank you for the responds.

I have change count to "2" but my real problem was whatever input the user enter, its always a WIN... That was not the intention as the user input is to be verified against the random number generated. I missed a code...have now inserted bolded

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

using namespace std;

int main ()
{
char ok = 'y';
char quit = 'q';
int RNbr;
int input;
char responds;

cout <<"Press 'y' to continue OR 'q' to quit: ";
cin >> responds;

if (responds == 'q'){
return 1;
}

if (responds != 'q'){
cout << "Input an integer between 1 - 6 inclusive: ";
cin >> input;
}

srand(time(0));
RNbr = rand()%6 + 1;

if (input = RNbr){
cout << "You win! ";
return 1;
}
if (input != RNbr){
cout << "Incorrect!. Correct number is: " << RNbr << endl;
int count = 2;
count--;
return 2;
}

return 0;
}
you should use code tags so I can respond with line numbers.

after RNbr = rand()%6 + 1;
you need a while loop like
while ( count !=0)

int count = 2;
should be moved to the top and should be
int count = 3;

Every time a guess is made
count --;

If they guess incorrectly you need
cin >> input;
Topic archived. No new replies allowed.