6 Replies - 791 Views - Last Post: 27 February 2012 - 10:11 AM Rate Topic: -----

#1 Codemonkey00   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-September 10

Issues with if statements

Posted 27 February 2012 - 09:29 AM

here is what this project is. We are to create 4 casino games in c++ and each game has to be their own function now i have declared it in main already but i need some help with this function.For some reason when i roll the randoms and try to compare the values within an if statement to see if they match but instead of following down to the else statement and having the loose message trigger it automatically goes to the first win statement.I've tried moving the adding the reel values to after they have been displayed but i keep getting that same win message can someone tell me what i'm doing wrong?



 
int slots (int balance)//craps function start here
	{
   				int reel1 = 0;
				int reel2 = 0;
				int reel3 = 0;
				int bet;
				int reelvalue1 = 0;
				int reelvalue2 = 0;
				int reelvalue3 = 0;
				char playagain = 1;
	while(playagain !=2 && balance !=0 )
 {
	 			cout <<" Welcome to Slots!"<<endl;
				cout <<"Your Current balance is  $" << balance <<endl;//not returning balance
				if (balance == 0)
				{
					cout << " Your Balance is not sufficient to play this game"<<endl;
					cout <<"  You have a 0 balance please leave the casino we dont like bums!"<<endl;
					cout <<"(1)yes-------(2)no"<<endl;
					cin >> playagain;
				}//end of 0 balance statment
				cout <<" What would you like to bet"<<endl;
				cin >> bet;
				cout <<"Lets spin them reels!!!!"<<endl;
				reel1 =rand()%10;
				reel2 =rand()%10;
				reel3 =rand()%10;
				
		cout << "{---------|"<< reel1 << "|----------|"<< reel2<<"|---------|"<<reel3<<"|---------}"<<endl;
                                reelvalue1 = reelvalue1 + reel1;//store reel1
				reelvalue2 = reelvalue2 + reel2;//store reel2
				reelvalue3 = reelvalue3 + reel3;//store reel3
					if (reel1 == reelvalue1 && reel2 == reelvalue2 && reel3 == reelvalue3)
					{
						cout<<"congradulations You winz1!"<<endl;
						bet = bet *99;
						balance = updatebalpayout(bet,balance);
						cout << "would you like to play again?"<<endl;
						cout <<"(1)yes-------(2)no"<<endl;
						cin >> playagain;

					}
					if (reel1==reelvalue1 && reel2 == reelvalue2)//reel1 ok ...reel2 ok....reel 3 X
					{
						cout << "congratz you winz2!"<<endl;
						bet = bet * 10;
						balance = updatebalpayout(bet,balance);
						cout << "would you like to play again?"<<endl;
						cout <<"(1)yes-------(2)no"<<endl;
						cin >> playagain;
					}
					else if(reel2 == reelvalue2 && reel3 ==reelvalue3)//reel1...X....reel2 ok ..... reel 3 ok.
					{
						cout << "congratz you winz3!"<<endl;
						bet = bet * 10;
						balance = updatebalpayout(bet,balance);
						cout << "would you like to play again?"<<endl;
						cout <<"(1)yes-------(2)no"<<endl;
						cin >> playagain;
					}
					else if(reelvalue1 == reelvalue1 && reel3 ==reelvalue3)//reel1...ok....reel2 x ..... reel 3 ok.
					{
						cout << "congratz you winz4!"<<endl;
						bet = bet * 10;
						balance = updatebalpayout(bet,balance);
						cout << "would you like to play again?"<<endl;
						cout <<"(1)yes-------(2)no"<<endl;
						cin >> playagain;
					}
					else
					{
						cout << "Sorry you lost!!! wahahahha"<<endl;
						cout << "would you like to play again?"<<endl;
						cout <<"(1)yes-------(2)no"<<endl;
						cin >> playagain;
					}
}//end of main while loop
	return balance;
	 }//end of referenced slots function


Is This A Good Question/Topic? 0
  • +

Replies To: Issues with if statements

#2 Codemonkey00   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-September 10

Re: Issues with if statements

Posted 27 February 2012 - 09:36 AM

please close this question i figured out what the problem was i was comparing incorrectly its supposed to be reel1==reel2..because if i try to compare the variable that i stored reel into it will always turn out correct
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Issues with if statements

Posted 27 February 2012 - 09:37 AM

Your while-loop condition is: while(playagain !=2 && balance !=0 ) but the inside the loop you check: if (balance == 0), the loop will never enter if balance is 0 so this check is redundant.

The chain of if/else if statements all look something like: if(reel# == reelvalue# && ...) but in one of them you have: else if(reelvalue1 == reelvalue1 && reel3 ==reelvalue3). reelvalue1 == reelvalue1 is always going to be true, so I don't know if this is a mistake or if it's what you intended, but it could be causing your problem.
Was This Post Helpful? 1
  • +
  • -

#4 Codemonkey00   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-September 10

Re: Issues with if statements

Posted 27 February 2012 - 09:45 AM

thanks i changed the loop condition to just while playagain !=2 and the items i changed i just took the reelvalue variables out and just compared the rands.

This is a valid if statement correct?
if (reel1 == reel2 == reel3 )

Was This Post Helpful? 0
  • +
  • -

#5 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Issues with if statements

Posted 27 February 2012 - 09:47 AM

I would write that as if(reel1 == reel2 && reel1 == reel3).

The statement you wrote will work but if you your compiler's warning/error level is set high, it should emit a warning like this:

Quote

warning: suggest parentheses around comparison in operand of ‘==’ [-Wparentheses]

This post has been edited by vividexstance: 27 February 2012 - 09:51 AM

Was This Post Helpful? 1
  • +
  • -

#6 Codemonkey00   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-September 10

Re: Issues with if statements

Posted 27 February 2012 - 10:04 AM

Thanks a bunch you guys are really helpful. God i love this website. i just got into coding so im a noob but its quickly becoming fun and interesting. I don't know how much coding ill do as an network administrator besides occasional patches.
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Issues with if statements

Posted 27 February 2012 - 10:11 AM

Nice, if someone on here helps you, don't forget to hit the '+' button at the bottom-right of their post where it says:

Quote

Was This Post Helpful? + -

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1