Thread: No error, but the result is unexpected

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    No error, but the result is unexpected

    Hi,

    I really don't understand whats wrong with this condition:

    Code:
    	// validate input
    	if	(isdigit(iResponse) && (iResponse >= 1 || iResponse < 10)) {
    		printf("\nThank you\n"); 
    	} else {
    		printf("\nEnter a number between 1 and 10!\n"); 
    	}
    111 and 5 both display the "Enter a number between.." message.

    So I want the user to input a NUMBER only and BETWEEN 1 and 10. What am I doing wrong here?

    Thanks in advance.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    So I want the user to input a NUMBER only and BETWEEN 1 and 10. What am I doing wrong here?
    Code:
    (iResponse >= 1 || iResponse < 10)
    || is either
    && is only

    Think if it this way, if response is >= 1 or < 10, then it will pretty much always be true right ?
    example -

    - 1 true( < 10)
    3 true( > 1 && < 10)
    55 true( > 1)

    basically || evaluates to true if either of the two operands is true and && evaluates to true only when both of them are.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    If I change the above to:

    Code:
    if	(isdigit(iResponse) && iResponse >= 1 || iResponse < 10) {
    		printf("\nThank you\n"); 
    	} else {
    		printf("\nEnter a number between 1 and 10!\n"); 
    	}
    ..then it seems to work, but still, it takes 0 as valid whereas I want numbers >1 ? But I wonder why won't the first (posted above) condition work?

    Many Thanks

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    like I said, You need to do this -

    Code:
    (isdigit(iResponse) && (iResponse >= 1 && iResponse < 10))

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by Nimbuz View Post
    If I change the above to:

    Code:
    if	(isdigit(iResponse) && iResponse >= 1 || iResponse < 10) {
    		printf("\nThank you\n"); 
    	} else {
    		printf("\nEnter a number between 1 and 10!\n"); 
    	}
    ..then it seems to work, but still, it takes 0 as valid whereas I want numbers >1 ? But I wonder why won't the first (posted above) condition work?

    Many Thanks
    Code:
    if	(isdigit(iResponse) && iResponse >= 1 || iResponse < 10)
    What this translates to is

    if
    - iResponse is a digit and its value is greater than one
    OR
    - iResponse is greater than 10

    print thank you.
    Code:
    (iResponse >= 1 || iResponse < 10)
    and what your earlier one translats to is -
    if
    - iResponse is greater than or equal 1
    OR
    - iResponse is less than 10

    print thank you.

    What you want is -

    Code:
    (iResponse >= 1 && iResponse < 10)
    if
    - iResponse is greater than or equal 1
    AND
    - iResponse is less than 10

    print thank you.
    Last edited by Spidey; 07-24-2009 at 07:24 AM.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by Spidey View Post
    Code:
    (iResponse >= 1 || iResponse < 10)
    || is either
    && is only

    Think if it this way, if response is >= 1 or < 10, then it will pretty much always be true right ?
    example -

    - 1 true( < 10)
    3 true( > 1 && < 10)
    55 true( > 1)

    basically || evaluates to true if either of the two operands is true and && evaluates to true only when both of them are.
    Ohk. But I use &&, it then returns false for every number:

    Code:
    	if	(isdigit(iResponse) && (iResponse>1 && iResponse<10)) {
    		printf("\nThank you.\n"); 
    	} else {
    		printf("\nEnter a number between 1 and 10!\n"); 
    	}
    what would be the correct condition?

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Ok, this is weird, I'm doing you said and its still not working:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main() {
    
    	int iResponse; // initialize
    	
    	// get input
    	printf("\nPlease enter a number: ");
    	scanf("%d", &iResponse);
    	
    	// validate input
    	if	(isdigit(iResponse) && (iResponse >= 1 && iResponse < 10)) {
    		printf("\nThank you.\n"); 
    	} else {
    		printf("\nEnter a number between 1 and 10!\n"); 
    	}
    	
    	// generate random number
    	// srand(time());
    	
    return 0;
    }
    Still shows "Enter a number .." still for ANY number. Something to do with my compiler/enviornment? I'm using Xcode on Leopard.

    Thanks

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Beginner C Question Help
    Another recent poster who got completely the wrong idea as to what isdigit() actually did.

    If you've used scanf("%d"), then you have ALREADY used all the digits you're going to have anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by Salem View Post
    Beginner C Question Help
    Another recent poster who got completely the wrong idea as to what isdigit() actually did.

    If you've used scanf("%d"), then you have ALREADY used all the digits you're going to have anyway.
    Ah ok, got it now. :-)

    The book "C programming for the absolute beginner" didn't explain this very well. I guess I'll have to dump and read "C for Dummies" instead.

    Thanks anyway Salem and Spidey!

  10. #10
    Registered User
    Join Date
    Apr 2008
    Location
    Haddock, GA, United States
    Posts
    13
    Why won't you try C Primer Plus Fifth Edition by Stephen Prata? I think it's a wonderful book to understand C. It will help you a lot, and then you can start reading The C Programming Language Second Edition by Dennis Ritchie and Brian Kernighan. I wish you the best, and think very well about the two books mentioned above. If you can't afford to buy them new, try to look for used versions of them. Amazon, eBay, and other virtual stores have them available. ;-) Take care!

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You don't need to start on a whole new book on C. Just ask about the difference between the integer iResponse and the char buffer you're trying to apply isdigit() to. Unfortunately your use of scanf() makes that char buffer hard to see and understand.

    Also a review of the difference between boolean AND and OR is a good idea.

    Then continue on with whatever book you have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM

Tags for this Thread