Thread: While loop not working

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    23

    While loop not working

    My while loop is not working. When it says "Would you like to play another game?", it allows unlimited inputs and doesn't respond to the inputs "1" or "0". can anyone help me find the error here?

    Code:
    #include <stdio.h>
    int main(void)
    {
        int i;
        int j;
            int contest();
        int personalityTest();
        int guessedWeight();
    
        printf("+_+_+_+_+WELCOME TO THE FALL CARNIVAL!_+_+_+_+_\n\n +_+_+_+_+_Which Carnival Game Would You Like To Play?_+_+_+_+_+\n");
        
        printf("1. Pumpkin Contest\n 2. guess your weight\n 3. personality test\n Enter the integer corresponding to your choice:\n");
    
        scanf("%i", &i);
    
        if(i==1)
        {
            contest();    
        }
    
        else if(i==2)
        {
            guessedWeight();
        }
    
        else if(i==3)
        {
             personalityTest();
        }
        else
        {
            printf("invalid input!\n\n");
        }
    
    
        printf("\nWould you like to play another game?\n (Enter 1 for Yes, 0 for No)\n");
    
        scanf("%i", &j);
    
    
            while(j==1);
            {
                printf("+_+_+_+_+_Which Carnival Game Would You Like To Play?_+_+_+_+_+\n");
    
                printf("1. Pumpkin Contest\n 2. guess your weight\n 3. personality test\n Enter the integer corresponding to your choice:\n");
    
                scanf("%i", &i);
    
    
                if(i==1)
                {
                    contest();    
                 }
    
                else if(i==2)
                {
                    guessedWeight();
                }
    
                else if(i==3)
                {
                    personalityTest();
                }
                else
                {
                    printf("invalid input!\n\n");
                }
    
    
                printf("\nWould you like to play another game?\n\n (Enter 1 for Yes, 0 for No)\n");
                scanf("%i", &j);
        
            }
            
            if(j==0)
            {
                return 0;
            }
        
    }

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    11
    This program is quite a mess here. For starters, you don't even have modules, so why are you inserting instructions for references to modules. Second, you use %d to print integers not %i. That is all I can find at the moment.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    Quote Originally Posted by tivanenk View Post
    This program is quite a mess here. For starters, you don't even have modules, so why are you inserting instructions for references to modules. Second, you use %d to print integers not %i. That is all I can find at the moment.
    I have the modules, but they are not the problem. The problem is the while loop seems to be ignored. %i is all that is necessary for this program. I'm just trying to get the while loop to work.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    The following code is a fairly generic "Do you want to continue?" code. This code is untested.

    Code:
    #include <stdio.h>
    
    void PlayAGame(void)
    {
    	/* Replace wth the code for playing a game */
    }
    
    int main(void)
    {
    	int PlayAgain;
    
    	do {
    		PlayAGame();
    
    		printf("\nWould you like to play another game?\n (Enter 1 for Yes, 0 for No)\n");
    		scanf("%i", &PlayAgain);
    
    	} while (PlayAgain==1);
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop stops working when loop >45 ??
    By Zul56 in forum C Programming
    Replies: 3
    Last Post: 12-11-2012, 03:40 PM
  2. Loop not working
    By Muppetlol in forum C Programming
    Replies: 12
    Last Post: 07-22-2012, 04:18 AM
  3. Can't get do while loop working
    By FernandoBasso in forum C Programming
    Replies: 8
    Last Post: 10-05-2011, 06:36 PM
  4. Why my while loop is not working ??? pls help
    By spurs01 in forum C Programming
    Replies: 22
    Last Post: 11-11-2009, 05:11 AM