Thread: do-while loop ignores break

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    do-while loop ignores break

    This a part of a battle simulation of my game. Why does this loop never ends? How do I fix it? I already placed a break after every return statement but it would just start over from 50 HP. Thanks for any kind of help.
    Code:
    int snakeBattle(int bonus)
    {
    	int you=50, snake=50, punch=0, kick=0, bite=0;
    	char attack[20];
    
    
    	do
    	{
    		printf("Your HP: %d     Snake's HP: %d\n\n", you, snake);
    		printf("Skills:\nkick\npunch\n\n");
    		printf("Enter skill: ");
    		scanf("%s", attack);
    		
    		if(strcmp("kick", attack)==0)
    		{
    			kickDamage(kick);
    			kick=kickDamage(kick);
    			biteDamage(bite);
    			bite=biteDamage(bite);
    			printf("You kicked the snake for %d damage!\n", kick);
    			printf("Snake bit you for %d damage!\n", bite);
    			snake=snake-kick;
    			you=you-bite;
            	if(you<0 && snake>you)
    			{
    				printf("You lost the battle!\n");
    				bonus=-1;
    				return bonus;
    				break;
    			}
    			if(snake<0 && you>snake)
    			{
    				printf("You won the battle!\n");
    				bonus=1;
    				return bonus;
    				break;
    			}
    
    
    		}
    		else if(strcmp("punch", attack)==0)
    		{
    			punchDamage(punch);
    			punch=punchDamage(punch);
    			biteDamage(bite);
    			bite=biteDamage(bite);
    			printf("You punched the snake for %d damage!\n", punch);
    			printf("Snake bit you for %d damage!\n", bite);
    			snake=snake-punch;
    			you=you-bite;
    			if(you<0 && snake>you)
    			{
    				printf("You lost the battle!\n");
    				bonus=-1;
    				return bonus;
    				break;
    			}
    			if(snake<0 && you>snake)
    			{
    				printf("You won the battle!\n");
    				bonus=1;
    				return bonus;
    				break;
    			}
    			
    		}
    		else
    		{
    			printf("Skill not learned!\n\n");
    		}
    	} while(bonus!=1 || bonus!=-1);
    	
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    The break after the return will never be reached. When you encounter the return, you immediately return to the calling function, no code after the return will be executed. Does one of the messages in your inner loops print each time, either "You Lost the battle!" or "You won the battle!", or "Skill not learned!"?

    Have you looked at the code in your calling function? Is this function called from within a loop in the calling function?

    Jim

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The condition of your while
    Code:
    while (bonus!=1 || bonus!=-1);
    will always be true (bonus will always be !=1 OR !=-1).
    You probably want
    Code:
    while (bonus!=1 && bonus!=-1);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    Does one of the messages in your inner loops print each time, either "You Lost the battle!" or "You won the battle!", or "Skill not learned!"?
    No. It will just appear after the battle.

    Is this function called from within a loop in the calling function?
    Yes, it's within a loop. Also, I placed getchar() to give me time before it will loop again. Here's the part of it.

    Code:
    if(b==6)
    							{
    								printf("\n\nSnake!\n\n");
    								snakeBattle(bonus);
    								bonus=snakeBattle(bonus);
    								if(bonus==1)
    								{
    									printf("Winner!\n");
    									getchar();
    									getchar();
    								}
    								if(bonus==-1)
    								{
    									printf("Loser!\n");
    									getchar();
    									getchar();
    								}
    								break;
    							}
    @oogabooga - Still not working. I'm pretty sure it should be || since bonus can only have 1 value.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Nope. It needs to either be what I posted above or this (which maybe you can understand better):
    Code:
    while (!(bonus==1 || bonus==-1));
    Note what that says: keep going WHILE it is NOT the case that bonus is either 1 or -1.

    The other way
    Code:
    while (bonus!=1 && bonus!=-1);
    says: keep going WHILE bonus is not 1 AND bonus is not -1.

    However, looking at your code it really doesn't matter what you put there since as soon as you assign bonus either 1 or -1 you return from the function. So while(1) would be just as good.

    So your mistake must be in one of your if conditions or that bite and punch are receiving the wrong values from biteDamage and punchDamage (which you haven't shown).

    Also, you don't want to do this:
    Code:
    			kickDamage(kick);
    			kick=kickDamage(kick);
    			biteDamage(bite);
    			bite=biteDamage(bite);
    but just this
    Code:
    			kick=kickDamage(kick);
    			bite=biteDamage(bite);

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    No. It will just appear after the battle.
    What just appears after the battle? What message "appears" after the battle? When do you want the loop to end?


    I would suggest that you show the smallest possible complete program that illustrates your problem.


    Jim

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    What just appears after the battle? What message "appears" after the battle? When do you want the loop to end?


    I would suggest that you show the smallest possible complete program that illustrates your problem.


    Jim
    "You won/lost the battle". I want the loop to end when "You won/lost the battle" appears. That would happen if the HP<0, which is working well in my program. The problem is, after the "You won/lost the battle" appears, it will go back to HP: 50 and the infinite loop goes on. I'll try to create a small program for this.

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    if(strcmp("kick", attack)==0)
    Code:
    else if(strcmp("punch", attack)==0)
    Dont you need to put in the newline characters when comparing your strings?

    ex
    Code:
    else if(strcmp("punch\n", attack)==0)

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    Oh found it!

    Code:
    printf("\n\nSnake!\n\n");
                                    snakeBattle(bonus);
                                    bonus=snakeBattle(bonus);
                                    if(bonus==1)
                                    {
                                        printf("Winner!\n");
                                        getchar();
                                        getchar();
                                    }
                                    if(bonus==-1)
                                    {
                                        printf("Loser!\n");
                                        getchar();
                                        getchar();
                                    }
    Should be

    Code:
    printf("\n\nSnake!\n\n");
                                    bonus=snakeBattle(bonus);
                                    if(bonus==1)
                                    {
                                        printf("Winner!\n");
                                        getchar();
                                        getchar();
                                    }
                                    if(bonus==-1)
                                    {
                                        printf("Loser!\n");
                                        getchar();
                                        getchar();
                                    }
    Thanks oogabooga and Jim! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop won't break;
    By Dagda in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2012, 12:46 PM
  2. Break the Loop.
    By Lord Slytherin in forum C Programming
    Replies: 3
    Last Post: 09-14-2011, 11:39 PM
  3. Best way to break out of loop
    By Ducky in forum C++ Programming
    Replies: 31
    Last Post: 09-07-2010, 11:22 PM
  4. do - while loop ignores getchar()
    By TheIggle in forum C Programming
    Replies: 3
    Last Post: 07-15-2010, 07:51 AM
  5. break from for loop
    By taurus in forum C Programming
    Replies: 3
    Last Post: 09-24-2009, 03:54 PM