0
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)

2
  • 2
    It would help if you describe exactly what is going wrong. Commented May 1, 2011 at 19:44
  • why not use cin and cout instead of scanf and printf? Commented May 1, 2011 at 19:47

5 Answers 5

3
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)); 
Sign up to request clarification or add additional context in comments.

Comments

2

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)); 

Comments

2

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))

Comments

1

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) ) 

3 Comments

@Seth Carnegie not is a gcc extension, ! is proper. Good catch.
Oh sorry, I don't use g++ so I'd never heart of not. Sorry about that.
not is an alias for !, not an extension.
1

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?

3 Comments

yes I want to enter scores until one of the two teams (team1 or team20 reaches 3 (wins).
@Nikos Angelis Junior - I suggest you check your code. What happens if team1 wins 4 times?
It can't reach 4, the do..while will terminate (increment is fixed to 1).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.