Thread: wizzard...(!)

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    22

    wizzard...(!)

    I have this:

    Code:
    #include <stdio.h>
    
    main()
    {
    
                     int d,choice;
    
    
    		printf("gIVE 1 or 2 or 3 or 4 or 5 \n ");
    		
    		do{	
    			scanf("%d",&choice);
    			
    			
    			if(choice!=1 && choice!=2 && choice!=3 && choice!=4 && choice!=5)
    				printf("Plz, becareful ");
    
    		}while(choice!=1 && choice!=2 && choice!=3 && choice!=4 && choice!=5);
    
    }
    if i give an integer (7, 56,8..), it prints the message and it's waiting for a new entry. OK
    if i give a character, it prints the message continuesly! why????????

    thx!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The character takes the newline that scanf() leaves behind and says "I've got my char", and keeps on looping.

    it's always best to pull off the left over newline, as soon as possible (or as you need to if you know the in's and out's of scanf().

    add:
    somevariable = getchar() ready after the scanf() and you'll see the difference (don't use choice as your variable, of course).

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    22
    Quote Originally Posted by Adak View Post
    The character takes the newline that scanf() leaves behind and says "I've got my char", and keeps on looping.

    it's always best to pull off the left over newline, as soon as possible (or as you need to if you know the in's and out's of scanf().

    add:
    somevariable = getchar() ready after the scanf() and you'll see the difference (don't use choice as your variable, of course).
    ok...

    but it prints the message many time, as the characters i give.

    for example
    i give sdf
    it prints the message 3 times...

    can i avoid this?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You lost me.

    Do you want to get a string of char's or a number, or ??

    for a string, use: scanf("%s", someCharArrayName)

    Before you use scanf() for a char, or for a string, you should have a clean keyboard buffer. for numbers, it won't matter - scanf() skips over "whitespace" when it's looking for a number.

    not true for char's or strings, though.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    22
    I just want to ensure, that the user will able to give only these entries : 1 or 2 or 3 or 4 or 5.

    Any other entry, is not acceptable.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    try this, it is a little bit shorter

    Code:
    if( (choice <= 0) || (choice > 5) )

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    and what is

    Code:
    int d
    for?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Victor_November View Post
    and what is

    Code:
    int d
    for?
    Planning for the future, no doubt.

    I would use Victor's code in parentheses, but make it choice < 1 || choice > 5 instead, and remove the if part. Just use it for the while() test at the bottom of the loop.
    Last edited by Adak; 11-11-2009 at 05:49 PM.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    22
    Planning for the future, no doubt.
    exaclty!!!

    somevariable = getchar() ready after the scanf() and you'll see the difference (don't use choice as your variable, of course).
    what do you mean?

    i wrote this, but i have the same problem..
    Code:
    #include <stdio.h>
    
    main()
    {
    
                     int choice;
    
    
    		printf("gIVE 1 or 2 or 3 or 4 or 5 \n ");
    		
    		do{	
    			choice = getchar();
    			
    			choice = choice -48;
    			
    			
    			if(choice!=1 && choice!=2 && choice!=3 && choice!=4 && choice!=5)
    				printf("Plz, becareful\n ");
    
    		}while(choice!=1 && choice!=2 && choice!=3 && choice!=4 && choice!=5);
    
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by johnybe View Post
    exaclty!!!

    i wrote this, but i have the same problem..
    This is what I meant:
    Code:
    #include <stdio.h>
    
    main()
    {
                     int choice, gar;
    
    		printf("gIVE 1 or 2 or 3 or 4 or 5 \n ");
    		
    		do{	
    			
                            scanf("%d", &choice);
      			
    			if(choice <1 || choice >5) {
    				printf("  Plz, becareful\n ");
                                    gar = getchar();
                            }
    		}while(choice < 1 || choice >5);
    
    }
    Last edited by Adak; 11-11-2009 at 08:06 PM.

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    22
    Quote Originally Posted by adak View Post
    this is what i meant:
    Code:
    #include <stdio.h>
    
    main()
    {
                     int choice, gar;
    
    		printf("give 1 or 2 or 3 or 4 or 5 \n ");
    		
    		do{	
    			
                            scanf("%d", &choice);
      			
    			if(choice <1 || choice >5) {
    				printf("  plz, becareful\n ");
                                    gar = getchar();
                            }
    		}while(choice < 1 || choice >5);
    
    }
    thxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx

Popular pages Recent additions subscribe to a feed