Results 1 to 14 of 14

Thread: If else- letters not allowed

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    7

    Re: If else- letters not allowed

    Quote Originally Posted by CornedBee
    Well? What's the expected result, the actual result? Any compile errors (for example from that extra logical OR in the if)?
    As it is now, the else will be only executed if the number is between 1 and 50 inclusive. Isn't that what you want?

    Yes, i rectify what i have written. There should not be any extra ||. I forgot to erase it. What i was doing was the following:
    if(value < 1 || value > 50|| (char) value)
    So,when i type letters, the characters should not be accepted. Unfortunately, this does not work.
    There is no compile errors if the extra|| is taken out.
    But suppose by mistake instead of typing values between 1 and 50 I type a letter. Then, the program crashes.
    So i want some flexibility so that no letters are allowed. Only the values between 1 and 50. Is that possible?

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: If else- letters not allowed

    Hello,

    Welcome to Forums

    what you are trying to accomplish can be acheived by playing with ASCII values but i won't suggest you for this. you may try reversing the if-then-else statement. like:
    Code:
    scanf("%lf",&value);
    
    if(value > 1 && value < 50 )
    {
    //do your stuff here
    }
    else
    {
    printf("not allowed\n");
    
    printf("Please choose value between 0 and 50");
    }
    hope it helps you.
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    7

    Re: If else- letters not allowed

    Quote Originally Posted by Harsh Gupta
    Hello,

    Welcome to Forums

    what you are trying to accomplish can be acheived by playing with ASCII values but i won't suggest you for this. you may try reversing the if-then-else statement. like:
    Code:
    scanf("%lf",&value);
    
    if(value > 1 && value < 50 )
    {
    //do your stuff here
    }
    else
    {
    printf("not allowed\n");
    
    printf("Please choose value between 0 and 50");
    }
    hope it helps you.
    No this does not help. A lot of ' Not allowed' appears on the window when i typed a letter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width