Thread: Confusion using the DO-WHILE LOOP in C,to assign a control.. Refer to my source code.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    10

    Confusion using the DO-WHILE LOOP in C,to assign a control.. Refer to my source code.

    I'm writing a small program which asks a user to input 6 numbers.

    It then displays it on the screen..

    (*)Then, it asks the user again if he wants to input any more numbers..

    If the user enters 'y'..he can input another 6 numbers..this goes on and on until he enters 'n' for the question in (*) and the program will end..

    below is the code

    Code:
    int u_num[6];
        char answer;
        int count=0;
    
      do
          
          {   
             printf("Please input any six numbers (from 1 to 40) \n");
        
             for (i=0; i<6; i++)
                 {
                       scanf("%d", &u_num[i]);
                 }
           
             printf(" Your Numbers are:");
                    
             for (i=0; i<6; i++)
                 {
                       printf(" --%d--", u_num[i]);
                 }
                 
                
               
           
                 printf("\n\n Do you want to input any more number? \n\n");
                 scanf("%c", &answer);
           }
               while(answer!='n');
    }
    The problem that is occurring is that I'm getting the message

    "Please insert your 6 numbers" along with
    "Do you want to input any more??"

    [Refer to attachment]

    I just want this "Do you want to input any more??" to be displayed..

    Any help or suggestion???
    Attached Images Attached Images Confusion using the DO-WHILE LOOP in C,to assign a control.. Refer to my source code.-attachement-jpg 

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    I don't know if there is a more elegant way, but I find scanf is picking up the return as a character or something,
    so just add another scanf to pick that up, then scanf again so it does not fall through.
    In other words, the 'return' after the sixth number is in the buffer (I think) and that is causing it to fall through.
    There is probably some kind of flush command I have not learned yet.
    I couldn't make your code work at first, there was some things missing.

    Code:
    #include <stdio.h>
    int u_num[6];
    char answer;
    int count=0;
    int i;
    int main (){
    
    
    	do {
    		
    		printf("Please input any six numbers (from 1 to 40) \n");
    	
    		for (i=0; i<6; i++)
    		{
    			scanf("%d", &u_num[i]);
    		}
    	
    		printf(" Your Numbers are:");
    	
    		for (i=0; i<6; i++)
    		{
    			printf(" --%d--", u_num[i]);
    		}
    	
    		printf("\n\n Do you want to input any more number? \n\n");
    		scanf("%c", &answer);
    		scanf("%c", &answer);
    	}
    	while(answer!='n');
    }

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Referring to your first post... line 26 should be...
    Code:
    scanf(" %c",&answer);
    Note the space between the " and the % ... this tells scanf to skip over unseen characters and wait for your entry.

    Note also that n and N are two different things... you should probably check for both, as in line 28...
    Code:
    while ((answer != 'n') && (answer != 'N'));

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    I thank you all for your time.. My problem is solved..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  2. Simple code: Can 2 references refer to the same object?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2008, 08:27 PM
  3. Replies: 4
    Last Post: 01-18-2008, 07:05 PM
  4. Source Code Control Systems
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-06-2002, 04:46 AM

Tags for this Thread