7 Replies - 784 Views - Last Post: 19 November 2011 - 05:53 PM Rate Topic: -----

#1 Hanoble   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-November 11

Homework assignment help question.

Posted 19 November 2011 - 11:08 AM

Hello everyone, I have been in a programming course for just over a month now and I am creating a yahtzee program for one of my assignments. Things have been moving along decently for my, but I have hit a bit of a snag. It comes when calculating the scores and using the or operator. When tallying the scores for three of a kind my code looks like this:
case 7:
			if (die[0] == die[1] == die[2] || die[1] == die[2] == die[3] || die[2] == die[3] == die[4])
			{
				threeKind = (die[0]+die[1]+die[2]+die[3]+die[4]);
				cout << "Your Three of a Kind score was " << threeKind << endl;
			}
			else
			{
				threeKind = 0;
				cout << "Your Three of a Kind score was " << threeKind << endl;
			}
			break;

The problem is that this only seems to work for the very first part of code the die[0] == die[1] == die[2] section. Any time I get one of the other two matches it skips the if and goes straight to the else and gives a score of zero. Anyone got any idea what is going on here? I have tried everything I know and looked around for help, but I can't find what I need. Any help would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Homework assignment help question.

#2 Ap0C552   User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 325
  • Joined: 08-December 10

Re: Homework assignment help question.

Posted 19 November 2011 - 11:44 AM

Before the conditional print out the values of all the dice.

Also break up the conditional into three separate if statements that will print out

" dice 0 1 2 match"

"dice 1 2 3 match"

"dice 3 4 5 match"

Also keep in the original if statement as well.

This will surely help you see what is going wrong.

This post has been edited by Ap0C552: 19 November 2011 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 Hanoble   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-November 11

Re: Homework assignment help question.

Posted 19 November 2011 - 11:51 AM

View PostAp0C552, on 19 November 2011 - 11:44 AM, said:

Before the conditional print out the values of all the dice.

Also break up the conditional into three separate if statements that will print out

" dice 0 1 2 match"

"dice 1 2 3 match"

"dice 3 4 5 match"

Also keep in the original if statement as well.

This will surely help you see what is going wrong.

I have done this and the if only executes if die[0] == die[1] == die[2]. I have also got rid of the or operator and attempted to use else if for each additional statement and it still only works with the original if statement. I have no clue why, but hopefully someone here does.
Was This Post Helpful? 0
  • +
  • -

#4 Ap0C552   User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 325
  • Joined: 08-December 10

Re: Homework assignment help question.

Posted 19 November 2011 - 12:41 PM

Well if you have IDENTICAL if statements that says

if(die[0] == die[1] == die[2])
{

}

if(die[1] == die[2] == die[3])
{

}

if(die[3] == die[4] == die[5])
{

}


And only the first conditional runs I am going to venture to say that the dice are not equal in the other cases.

Print out the values of the dice right before each conditional.
Was This Post Helpful? 0
  • +
  • -

#5 Hanoble   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-November 11

Re: Homework assignment help question.

Posted 19 November 2011 - 01:04 PM

View PostAp0C552, on 19 November 2011 - 12:41 PM, said:

Well if you have IDENTICAL if statements that says

if(die[0] == die[1] == die[2])
{

}

if(die[1] == die[2] == die[3])
{

}

if(die[3] == die[4] == die[5])
{

}


And only the first conditional runs I am going to venture to say that the dice are not equal in the other cases.

Print out the values of the dice right before each conditional.

Hello Ap0C, thanks for helping. This is my new code as you suggested.
case 7:
			sort (die, die+5);
			cout << die[0] << die[1] << die[2] << die[3] << die[4] << endl;
			if (die[0] == die[1] == die[2]) 
			{
				threeKind = (die[0]+die[1]+die[2]+die[3]+die[4]);
				cout << "Your Three of a Kind score was " << threeKind << endl;
			}
			else if (die[1] == die[2] == die[3])
			{
				threeKind = (die[0]+die[1]+die[2]+die[3]+die[4]);
				cout << "Your Three of a Kind score was " << threeKind << endl;
			}
			else if (die[2] == die[3] == die[4])
			{
				threeKind = (die[0]+die[1]+die[2]+die[3]+die[4]);
				cout << "Your Three of a Kind score was " << threeKind << endl;
			}
			else
			{
				threeKind = 0;
				cout << "Your Three of a Kind score was " << threeKind << endl;
			}
			break;

I also added the way I am sorting the die at the top of the case statement just in case this has anything to do with it. Anyway, I ran this and I got 13334. This should of set off the second if statement, but it did not and instead went to the else statement and gave the roll a score of zero. However, when I rolled and got 11146 from sort it set my three of a kind score to 13 as it should. So these are the exact same results I was getting out of my original code using the OR operator.
Was This Post Helpful? 0
  • +
  • -

#6 Hanoble   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-November 11

Re: Homework assignment help question.

Posted 19 November 2011 - 05:17 PM

Any other suggestions? Still unable to get this to correctly calculate and unable to find a way around it. Any help would be greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Homework assignment help question.

Posted 19 November 2011 - 05:27 PM

In C/C++ you if statement should look more like:

if( ( (die[0] == die[1]) && (die[0] == die[2]) ) || 
    ( (die[1] == die[2]) && (die[1] == die[3]) ) )

Each section of statement must be complete.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 Hanoble   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-November 11

Re: Homework assignment help question.

Posted 19 November 2011 - 05:53 PM

View Postjimblumberg, on 19 November 2011 - 05:27 PM, said:

In C/C++ you if statement should look more like:

if( ( (die[0] == die[1]) && (die[0] == die[2]) ) || 
    ( (die[1] == die[2]) && (die[1] == die[3]) ) )

Each section of statement must be complete.

Jim


THANK YOU JIM! That fixed it and I was having this trouble throughout all of my calculations after the first if so thanks a lot. I've been stuck trying to figure this out for hours, but glad that you were able to help. I really appreciate it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1