Wierd behavior when running this code

hello,

I have recently been working on a game where two players battle by guessing a number. But with not a lot of succes. In my first testrun I choose easy mode but the number was higher than 100 (ended up around 8912). The wierd part is that when the number given by the player was higher than 8192 it outputed both
number is higher
and
number is lower
Is there somthing wrong with this function? Or might it be my main function?

The function I think is wrong:
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
void play(int pl1, int pl2){
int i, p1,p2,mode,p1money = 50, p2money = 50;
	bool x = true,z = true;
	while (x){
	cout<<"for easy mode press 1 "<<endl;
	cout<<"for normal mode press 2 "<<endl;
	cout<<"for hard mode press 3 "<<endl;
	cout<<"for beast mode press 4 "<<endl;
	cout<<"choise: ";
	cin>>mode;
	switch (mode)
	{
		case 1:
			i = rand() %100;
			cout<<"number between 1 and 100"<<endl;
			x=false;
			break;
		case 2:
			i = rand() %500;
			cout<<"number between 1 and 500"<<endl;
			x=false;
			break;
		case 3:
			i = rand() %2500;
			cout<<"number between 1 and 2500"<<endl;
			x=false;
			break;
		case 4:
			i = rand() %10000;
			cout<<"number between 1 and 10000"<<endl;
			x=false;
			break;
		default:
			cout<<"please choose again"<<endl;
			break;
	}
	while(z){
		cout<<"player 1: ";
		cin>>p1;
		if(p1 << i)
		cout<<"number is higher"<<endl;
		if(p1 >> i)
		cout<<"number is lower"<<endl;
		cout<<"player 2: ";
		cin>>p2;
		if(p2 << i)
		cout<<"number is higher"<<endl;
		if(p2 >> i)
		cout<<"number is lower"<<endl;
		if (p1 == i){
			cout<<"player 1 has won!!"<<endl;
			Abbo[pl1].won++;
			Abbo[pl2].lost++;
			Abbo[pl1].money = Abbo[pl1].money + p1money;
			z = false;
			} else {
			p1money=p1money-5;
		}
		if (p2 == i){
			cout<<"player 2 has won!!"<<endl;
			Abbo[pl2].won++;
			Abbo[pl1].lost++;
			Abbo[pl2].money = Abbo[pl2].money + p2money;
			z = false;
			} else {
			p2money=p2money-5;
		}
		if (p1money == 0 || p2money == 0){
			Abbo[pl1].same++;
			Abbo[pl2].same++;	
			}
		}
	}
}


Rest of 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
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string.h>
#include <fstream>

using namespace std;

struct RecAbbo{
		std::string name;
		std::string surname; 
		int games;
		int won;
		int same;
		int lost;
		int money;
	}Abbo[100];
	void play(int pl1, int pl2);
int main(){
char name1[20],name2[20],surname1[20],surname2[20];
	int i1, i2, i = 0;
	bool check = false, check2 = false ;
	while (check == false){	
		i1 = rand() % 100;
		i2 = rand() % 100;
		while (check2 == false) 
			if (i1 == i2){
				i2 = (i2 + rand() % 100) - 100;
			} else {
				if (Abbo[i1].name.empty() == 0 || Abbo[i2].name.empty() == 0){
					check2 = false;
				} else {
					check2 = true;
					check = true;
				}
			}
		}
	while (i < 20){
			name1[i] = Abbo[i1].name[i];
			name2[i] = Abbo[i2].name[i];
			surname1[i] = Abbo[i1].surname[i];
			surname2[i] = Abbo[i2].surname[i];
			
			i++;
	}
	Abbo[i1].games++;
	Abbo[i2].games++;
	cout<<"       player 1:     player 2: \n";
	cout<<"game: "<<name1<<surname1<<" VS :"<<name2<<surname2<<endl;
	play(i1,i2);
}


This is part of a bigger program. I just created a new file to reproduce the error. There are also a few errors in that file but that is for an other toppic
Last edited on
This
1
2
3
4
		if(p1 << i)
		cout<<"number is higher"<<endl;
		if(p1 >> i)
		cout<<"number is lower"<<endl;
makes no sense. << is bit shifting in this case.

What you want is certainly this:
1
2
3
4
		if(p1 < i) // Note: < not <<
		cout<<"number is higher"<<endl;
		else // Note
		cout<<"number is lower"<<endl;
That work thank you. now I'll just need to fix some errors on my big program (See other topic I made just after this one) and then I'll be fine.
Well, sorry, it needs to be this:
1
2
3
4
		if(p1 < i)
		cout<<"number is higher"<<endl;
		else if(p1 > i) // Note
		cout<<"number is lower"<<endl;
otherwise equal 'is lower'
coder777 no it works
Pay more attention to the details. That something compiles does not mean it works.

Can you tell the difference between the two variants? And why I gave you the second variant
Topic archived. No new replies allowed.