number guessing game from pc side.

Hi there, can somebody explain this?
In this game I do imagine a number and the pc is guessing. I can tell lower or higher or pc did win, if pc didn't find my number in 10 moves he lose.
1)why in this code "c" at start shows as 1101, If I pus c=0 then it shows as 0101?
2) I cant understand why if write 1(let pc win) it still going for one more loop and write down the cout << "is this your number: " << x << " "; and then stops?
Sorry for my bad English.

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
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <time.h>  
using namespace std;

int main()
{	
	int x ;
    int reizes=10;// counter
    int z=10;
    int a=101; // for rand max
    int c=0; // for rand min - edidted at 11:05
    int g=0;
    
 
		  cout << "imagine a number ( no 0-100): " << endl;
		 
		  cout << " write 1 if right , if less- 2 and  if 3 more " << endl;
		  cout << "times left: "; cout << reizes; cout << " you can start:  ";
		   cout << a << " "<< c<< endl ;// just for check
	
	    do
        {
            reizes--;
           srand(time(0));
		   x = rand() % a + c;
		   cout << a << "  "<< c ; // just for check if a and c did change
		cout << "is this your number:      " << x << "  unswer:   ";
		int g=0;
		cin >> g;
		 if (reizes==0)
		 cout<<"I did win you pc?"; 
                 if( g==1)
			     cout<< "congrats you won in  "<< z-reizes <<"moves";
	
			
		else if ( g==2)
			a=x;
		else if ( g==3)
			c=x;
		else if (a<=c)
		cout << "something went wrong,restart game";
		else 
		    {
		cout << "you didn`t enter 1, 2 or 3";
		reizes++;
		    }
        }
        	while( g!=1 && reizes!=0 );


}
Last edited on
If the user should pick a number, there should be an Input.

Line 26 x = rand() % a + c
Your rand min should be 0, you can Input 0-100.

At Line 29 you take g as an integer, like in Line 13. You should only write "g = 0;".
Last edited on
1) c didn't show 1101, put an endl after your Output. It is your second Output of a you make after your first Output without whitespaces or a new line.

2) Like i said in my first post, where is your Input?
if I put int c=0; it still write down c as 0101
and there will not be input for my picked number..
I just tell if the number pc picked form rand is higher or lower or pc finded out if I write as answer 1..,


like if pc from rand gueses "'50" then I can input that my number ir bigger as 50 and program shoul change rand x= rand() % 100 +50 and guess again from that range till I write that g=1 that pc did guess or in to gueses pc lose..

in line just for check is shows that number ( I`am using web compailer for now)
an di did put " g=0; " at line 29..
like I said English os not born language so I changed "pick a number"0 for ""imagine a number"
Last edited on
1) c didn't show 1101, put an endl after your Output. It is your second Output of a you make after your first Output without whitespaces or a new line.


Your Line 20 cout << a << " "<< c ;// just for check

If you put an endl at the end of the Output, your first Problem won't be there.
Endl = New Line!

cout << a << " "<< c << endl;// just for check
Last edited on
this is the programs output:
imagine a number ( no 0-100):
write 1 if righ , if less- 2 and if 3 more
times left: 10 you can start: 101 0101 0 // why a and c looks like that( in code line 20
is this your number: 75 3 ( I did pick a number 3 that my imagined number is higher)
101 75 ( now a and c values for rand)
is this your number: 157 ( why game gueses nuber 157 if the range is from 75 to 101?

sometimes it all goes well and program is working fine except the part that it asks for one more unswer when I let pc win..
Last edited on
times left: 10 you can start: 101 0101 0 // why a and c looks like that( in code line 20


They look like that because this is not only line 20, there are two lines 20 and 27. You're not using any whitespaces or new lines between these Outputs.

sometimes it all goes well and program is working fine except the part that it asks for one more unswer when I let pc win..


Line 29: Is a double defined variable you should change it to g = 0;


I did edit as you said, but it still sometimes give a wrong answer..
why sometimes it gives x out of renge?

imagine a number ( no 0-100):
write 1 if right , if less- 2 and if 3 more
times left: 10 you can start: 101 0
is this your number: 79 unswer 2
79 0 // ( new range)
is this your number: 68 unswer 2
68 0 // ( new range)
is this your number: 53 unswer 3
68 53 // ( new range)
is this your number: 107 unswer // wrong, but why?
Last edited on
I did edit as you said, but it still sometimes give a wrong answer..
why sometimes it gives x out of renge?


You're changing the value of "c" at Line 34, so at every Modulo Operation in Line 22 it will be added to your new number.
So if you're saying biggest number is 100 and new number should be less than your last number it will add the old number up.
mm i did wrong from the begining.. sorry for false topic.
please report so it could be removed.

Last edited on
Topic archived. No new replies allowed.