if (mynumber > computersnumber)
cout << "I have the higher number";
elseif (mynumber < computersnumber)
cout << "The computer has the higher number";
else
cout << "The computer and I have the same number";
// A simple number game
#include <iostream>
#include <stdlib.h>
#include <ctime>
usingnamespace 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();
constint MAX=100;
int number=(randNumber%MAX)+1;
return number;
}
void displayRandom(int number)
{
cout << "\nThe computer number is "<<number;
cout << " \n";
}
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();
constint 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.
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.
// A simple number game
#include <iostream>
#include <stdlib.h>
#include <ctime>
usingnamespace 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";
}
elseif (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();
constint MAX=100;
int number=(randNumber%MAX)+1;
return number;
}
void displayRandom(int number)
{
cout<< "\nThe computer number is "<<number;
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";
}
return number; //<- add this here! you forgot it...
}