Me vs Computer

I want to see who got the high number me or the computer but I don't know how to see who got the higher number.

How do I check to find out who got the higher number?

Thank you in advance.
1
2
3
4
if(mynumber > computersnumber)
  cout << "I have the higher number";
else
  cout << "The computer has the higher number";
1
2
3
4
5
6
if (mynumber > computersnumber)
  cout << "I have the higher number";
else if (mynumber < computersnumber)
  cout << "The computer has the higher number";
else
  cout << "The computer and I have the same number";
I tryed both of them codes and none of them worked probley.

When the window was open I would put in my number it wouldn't work then I do it again and it would work but I want ti too work on teh first time.
Could you show us your code? Oh, and use code tags, like this:
[code] your code here [/code]
Last edited on
There my whole 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
52
53
54
55
56
57
58
// A simple number game

#include <iostream>
#include <stdlib.h>
#include <ctime>
 
using namespace std;
void welcome();
int player();
int computer();
void displayRandom(int number);
 
int main() 
{ 
    int number;
	welcome();
	player();
	number=computer();
	displayRandom(number);  
	if (player()>computer())
		cout << "\nI have the higher number";
	else
		cout << "The computer has the higher number";
	return 0;
}
void welcome()
{
	cout<<"Get a higher number then the computer to win.\n";
	cout<<" \n";
}
int player()
{
	int number;
	cout<<"\nPick a number between 1 and 100: ";
	cin>> number;
	cin.ignore();
	if (number<100,number>1)
	{
        cout<<"\nYour number is " <<number;
    }
    if (number>100,number<1)
    {
        cout<<"The number you have entered is not between 1 and 100\n";
    }
}
int computer()
{
    srand(time(0)); 
	int randNumber=rand(); 
	const int MAX=100; 
	int number=(randNumber%MAX)+1;
	return number;
}
void displayRandom(int number)
{
	cout << "\nThe computer number is "<<number;
	cout << " \n";
}
Ok, here are some things I have to say...

In your main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main() 
{ 
    int number;
	welcome();
	player();
	number=computer();
	displayRandom(number);  
	if (player()>computer())
		cout << "\nI have the higher number";
	else
		cout << "The computer has the higher number";
	return 0;
}

don't forget that player(); is a function! It will be called every time your write player(). As you see you call it two times. Same with computer, but you don't realize it's called twice since it doesn't print anything. To solve this, create two integer variables, my_pick, com_pick and set their values using player() and computer(). Then work with these variables.

In your player function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int player()
{
	int number;
	cout<<"\nPick a number between 1 and 100: ";
	cin>> number;
	cin.ignore();
	if (number<100,number>1)
	{
        cout<<"\nYour number is " <<number;
    }
    if (number>100,number<1)
    {
        cout<<"The number you have entered is not between 1 and 100\n";
    }
}

if (number<100,number>1) should be if (number<100 && number>1)
and if (number>100,number<1) should be if (number>100 || number<1)
Take a look at && and || logical operators here somewhere -> http://cplusplus.com/doc/tutorial/operators/

EDIT:
In your computer function:
1
2
3
4
5
6
7
8
int computer()
{
    srand(time(0)); 
	int randNumber=rand(); 
	const int MAX=100; 
	int number=(randNumber%MAX)+1;
	return number;
}

When you use random numbers it's good to call srand() only once in your program. If you call it multiple times you "break" the number distribution. I usually put it first thing inside main.
Last edited on
Sorry I'm very new to C++ Could you explain what you mean by this a bit more?

don't forget that player(); is a function! It will be called every time your write player(). As you see you call it two times. Same with computer, but you don't realize it's called twice since it doesn't print anything. To solve this, create two integer variables, my_pick, com_pick and set their values using player() and computer(). Then work with these variables.
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
int main()
{
    int player_pick;
    int computer_pick;

    player_pick=player();
    computer_pick=computer();

    displayRandom(computer_pick);

    if (player_pick>computer_pick)
    {
        cout << "You win!" << endl;
    }
    else if (player_pick<computer_pick)
    {
        cout << "Computer wins!" << endl;
    }
    else
    {
        cout << "It's a draw!" << endl;
    }

	return 0;
}

Also, in your player() function you forgot to return a value!
Last edited on
Works now Thank you :)
It was working but now I win everytime. :L

BTW I didn't chance the srand(time(0)); because I didn't know what you ment so could that be the problem?
No, I don't think it's that. Show me what you have done.
Heres my whole code again.


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
// A simple number game

#include <iostream>
#include <stdlib.h>
#include <ctime>
 
using namespace std;
void welcome();
int player();
int computer();
void displayRandom(int number);
 
int main()
{
    int player_pick;
    int computer_pick;
    welcome();
    player_pick=player();
    computer_pick=computer();
	displayRandom(computer_pick);
    if (player_pick>computer_pick)
    {
        cout << "\nYou win!\n\n";
    }
    else if (player_pick<computer_pick)
    {
        cout<<"\nComputer wins!\n\n";
    }
    else
    {
        cout<<"\nIt's a draw!\n\n";
    }
    system("PAUSE");
	return 0;
}

void welcome()
{
	cout<<"Get a higher number then the computer to win.\n";
	cout<<" \n";
}
int player()
{
	int number;
	cout<<"\nPick a number between 1 and 100: ";
	cin>> number;
	cin.ignore();
	if (number<100 && number>1)
	{
        cout<<"\nYour number is " <<number;
    }
    if (number>100 || number<1)
    {
        cout<<"The number you have entered is not between 1 and 100\n";
    }
}
int computer()
{
    srand(time(0)); 
	int randNumber=rand(); 
	const int MAX=100; 
	int number=(randNumber%MAX)+1;
	return number;
}
void displayRandom(int number)
{
	cout<< "\nThe computer number is "<<number;
	cout<< " \n";
}


player_pick is undefined - look at your code again.
Player() only deals with the int number, but doesn't return a value.
Last edited on
Yeap, what bluezor said, fix it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int player()
{
	int number;
	cout<<"\nPick a number between 1 and 100: ";
	cin>> number;
	cin.ignore();
	if (number<100 && number>1)
	{
        cout<<"\nYour number is " <<number;
    }
    if (number>100 || number<1)
    {
        cout<<"The number you have entered is not between 1 and 100\n";
    }

    return number; //<- add this here! you forgot it...

}

Okay, it now works. Thank you, just one more question

How do I check if my number is with in 10 range of the computers number?
1
2
3
4
5
6
7
8
#include <cmath>

//...

if ( abs(player_pick-computer_pick)<=10 )
    cout << "your pick is within 10 range of the computer's pick!" << endl;

//... 

abs() returns the absolute value of a number.
Last edited on
Okay, now I'm done thank you.
Topic archived. No new replies allowed.