This `do..while` loop isn't working

int sc1,sc2,a=0,b=0;

do
{

 printf("Give the scores\n");

 scanf("%d %d", &sc1,&sc2);

 //===============================================

 if (sc1 > sc2)     
   a+=1;
 else if (sc1<sc2)
   b+=1;
 else if (sc1==sc2)
   printf("tie\n");

 //===============================================

 if (a>b)    
    printf("team 1 is in the lead\n");
 else if (a<b)
    printf("team 2 is in the lead\n");
 else if (b==a)
    printf("tie\n");      

}
while((a==3) || (b==3));

//===============================================


if (a==3)
  printf("team 1 got the cup");
else
  printf("team 2 got the cup");

I think that I wrote something wrong. I've searched it a lot but can't seem to find what is wrong with it.

(One of the two teams can win the cup and that team has to have 3 wins)

*else if(sc1

*else if(a>b)

Upvotes: 0

Views: 485

Answers (5)

Vladislav Zorov
Vladislav Zorov

Reputation: 3056

Basically, you're telling it to loop while a or b still equal 3. Which is not what you want. You want while((a<3) && (b<3))

Upvotes: 2

sean e
sean e

Reputation: 11935

Your loop condition is incorrect. It is stopping early because neither team has a score of 3. Loop until one or the other gets up to 3:

while((a < 3) && (b < 3));

Upvotes: 2

Mat
Mat

Reputation: 206909

while((a==3) || (b==3));

will loop only if a or b is three. If you want to wait until one of them is three, use:

while ((a!=3) && (b!=3));

Upvotes: 3

Oded
Oded

Reputation: 499382

Your conditional:

while((a==3) || (b==3));

States that the loop will continue so long as either a or b are equal to 3. Are you sure this is what you wanted?

Upvotes: 1

wheaties
wheaties

Reputation: 35990

If I'm reading your question properly you'd like the terminating condition to be that one of the teams "a" or "b" has a score of 3. However, in your code you've written that the only way the while can loop is if one of the teams has a score of 3. You want:

while( !( a==3 || b == 3) )

Upvotes: 1

Related Questions