Results 1 to 14 of 14

Thread: If else- letters not allowed

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    7

    If else- letters not allowed

    [c]
    Just want to be able to prevent my program from crashing when i input a letter.
    I thought that when i use if else statement the program will take into account all the keys and not the numbers. This seems not to be the case. Then, i thought of declaring value as char instead of double. This does not work.




    Value is of type double.

    scanf("%lf",&value);

    if(value < 1 || value > 50|| )
    {
    printf("not allowed\n");

    printf("Please choose value between 0 and 90");

    }
    else
    {

    }

    I thought that when i use 'if else' statement the program will take into account all the keys and not the numbers only. This seems not to be the case. Then, i thought of declaring 'value' as 'char' instead of 'double'. This does not work.
    What i want is that ONLY the values between 0 and 50 are allowed.

    [c]

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: If else- letters not allowed

    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?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    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?

  4. #4
    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.

  5. #5

    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.

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

    Re: If else- letters not allowed

    in that case, i request you to please post the whole code. if thats not possible, then please post the function code in which it is running, because it is running on my PC!!
    Show Appreciation. Rate Posts.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    7

    Re: If else- letters not allowed

    Quote Originally Posted by Harsh Gupta
    in that case, i request you to please post the whole code. if thats not possible, then please post the function code in which it is running, because it is running on my PC!!
    #include <stdio.h>

    int main()
    {

    double value;
    int leaving =1;
    while (leaving == 1)
    {

    printf("enter");
    scanf("%lf",&value);

    if(value > 1 && value < 50 )
    {
    leaving = 0;
    }
    else
    {
    printf("not allowed\n");

    printf("Please choose value between 0 and 50");
    }
    }
    }

    I thought by using this code, the program will ask me again and again to enter a value between 1 and 50 if my value is out or I pressed a letter.

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

    Re: If else- letters not allowed

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    double value;
    int leaving =1;
    while (leaving == 1)
    {
    
    printf("enter");
    scanf("%lf",&value);
    
    if(value > 1 && value < 50 )
    {
    leaving = 0;
    }
    else
    {
    fflush(stdin);
    printf("not allowed\n");
    
    printf("Please choose value between 0 and 50");
    }
    }
    }
    my bad, thats was an abnormal behavior generally showed by C. now it will ask again even if you press a character key. just add the highlighted line to your code and give it a try.

    and please revert back if it helps you. i just want to be sure if i am right.
    Show Appreciation. Rate Posts.

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    7

    Re: If else- letters not allowed

    Quote Originally Posted by Harsh Gupta
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    double value;
    int leaving =1;
    while (leaving == 1)
    {
    
    printf("enter");
    scanf("%lf",&value);
    
    if(value > 1 && value < 50 )
    {
    leaving = 0;
    }
    else
    {
    fflush(stdin);
    printf("not allowed\n");
    
    printf("Please choose value between 0 and 50");
    }
    }
    }
    my bad, thats was an abnormal behavior generally showed by C. now it will ask again even if you press a character key. just add the highlighted line to your code and give it a try.

    and please revert back if it helps you. i just want to be sure if i am right.
    Yes. Indeed, it works. I do not know this line. Could you tell me what this line does? In my book there are many 'f' but few explanations!

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

    Re: If else- letters not allowed

    for details on fflush(), please take a look here.
    Show Appreciation. Rate Posts.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: If else- letters not allowed

    Note also that this is non-standard and unportable behaviour! For more details, see this FAQ entry.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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

    Re: If else- letters not allowed

    Quote Originally Posted by CornedBee
    Note also that this is non-standard and unportable behaviour! For more details, see this FAQ entry.
    yes, you are right.

    so is there any other way to do this instead of using fflush()?

    thank you.
    Show Appreciation. Rate Posts.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: If else- letters not allowed

    You can read the remaining line into a scratch buffer with fgets(). In C++, you can also use istream::ignore(), so you don't need a buffer.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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

    Re: If else- letters not allowed

    sorry, could not get them. could you please provide an example?

    EDIT: i am familiar with fgets() and gets(). but never used ignore(). so could you please explain with a small example how to use it?

    thank you.
    Last edited by Harsh Gupta; Jan 8th, 2006 at 04:01 AM.
    Show Appreciation. Rate Posts.

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