Made this as an assignment for my instructor
but the if conditions don't work at all
i m a novice so please help me out!!
# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;
class player{
public:
int i;
player (int a, int b){
i=a+b;
}
int operator > (player &ob2);
};
int player::operator > (player &ob2){
if(i>ob2.i){
return i;
}else{
return ob2.i;
}
}
int dice (){
int roll=0;
srand((unsigned)time(0));
roll=rand()%6+1;
roll=rand()%6+1;
return roll;
}
int main(){
int a,b,c,d;
char e;
cout <<"Player 1 do you want to roll your first dice (Y/N)? :"; cin >>e;
if(e='y' || 'Y'){
a=dice();
cout << a <<endl;
}else{
cout << "Game Over";
}
cout <<"Player 1 do you want to roll your second dice (Y/N)? :"; cin >>e;
if(e='y'||'Y'){
b=dice();
cout << b <<endl;
}else{
cout<<"Game Over";
}
cout <<"Player 2 one do you want to roll your first dice (Y/N)? :"; cin >>e;
if(e=='y'||'Y'){
c=dice();
cout << c <<endl;
}else{
cout<<"Game Over";
}
cout <<"Player 2 one do you want to roll your second dice (Y/N)? :"; cin >>e;
if(e='y'||'Y'){
d=dice();
cout << d <<endl;
}else{
cout<<"Game Over";
}
player ob1(a,b),ob2(c,d);
if(ob1>ob2){
cout << "player 1 wins" << endl;
}else{
cout << "player 2 wins" << endl;
}
When you want to test for multiple values you have to write a test for each value:
if( (e=='y') || (e=='Y') )
if (e='y' || 'Y')
Two errors here. First, it should be ==
rather than =
.
Second, it is interpreted as two conditions separated by "or"
if ((e=='y') || ('Y'))
first condition: (e=='y')
result depends on value of e
second condition: ('Y')
Any non-zero value is interpreted as true
The result is if ((e=='y') || true)
which is always true
It should look like this:
if ((e=='y') || (e=='Y'))
hey thank guyz....that fixed it!!