Thread: Not allowing numbers to be entered.

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    8

    Not allowing numbers to be entered.

    Hey,

    Can anyone tell me how I can prevent a user entering letters and symbols into this program.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #define metersinakilo 1000
    
    void main()
    
    {
    
    	int numberofkilos,numberofmeters;
    
    	printf("Please Enter The Number Of Meters: ");
    	scanf("%d",&numberofmeters);
    
    	numberofkilo=numberofmeters / metersinakilo;
    
    	printf("\n%d meters is %d kilometres",numberofmeters,numberofkilo);
    
    }
    Thankyou for any help.

    Andy

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    retry:	printf("Please Enter The Number Of Meters: ");
    	 scanf("%d",&numberofmeters);
             fflush(stdin);
               if(numberofmeters<0||numberofmeters>100000)goto retry;// change the range to suit you

    just make sure your complier supports fflush(stdin) ,just like mine.

  3. #3
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    how about isalpha() ?
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > just make sure your complier supports fflush(stdin) ,just like mine.
    Just make sure you learn how to do it properly - then the "works for me compiler" issue becomes irrelevant.

    > if(numberofmeters<0||numberofmeters>100000)goto retry;
    1. Don't use goto
    2. If scanf fails, then the result is at best unmodified and at worst undefined. Either way, your test is horribly broken.

    > void main()
    Ditto for this - use int main and be compatible with the whole universe.


    All of you need to read http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    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.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Especially this section:
    To clear the input buffer, you have to read through the input until there is no more. Sometimes you'll see people recommending the use of fflush(stdin), but this is undefined behaviour, and is therefore bad practice. Instead, use something like the following, a looping input function that eats characters until the desired end of line marker is reached:
    Code:
    while (getchar() != '\n');
    And this:
    Code:
    #include <stdio.h> 
    
    int main(void)
    {
      int temp;
      
      printf ("Input your number: ");
    
      while (scanf("%d", &temp) != 1)
      {
        while (getchar() != '\n');
        printf ("Try again: ");
      }
    
      printf ("You entered %d\n", temp);
      
      return(0);
    }


    how about isalpha() ?
    isdigit() might work.

    But it would be easier to use scanf() or something else covered in the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by qqqqxxxx
    fflush(stdin) is not undefined behavior,some compilers support it and its behavior is very defined,only the c standard says that its behavior should be undefined,but thats all there is to it.
    Fortunately, this forum is called "C programming" not "The idiosyncracies of XYZ compiler", so the C standard saying the behaviour is undefined is very relevant to this forum. Stop recommending people use things that are undefined.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > only the c standard says that its behavior should be undefined
    That's the only thing that matters.
    What your tin-pot compiler allows is entirely up to whoever wrote the compiler.

    I see a lot of responses to my posts are "thanks, that worked". Why is that, because I stick to the standard, which works EVERYWHERE.

    I see a lot of responses to your posts being "but I don't have .... magic compiler specific function".

    > LONG LIVE THE C LANGUAGE. (lmao)
    Next stop - loonyville - please depart the train now if you don't want to go there.
    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.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Mine works all the time.

    Yours only works so long as you or the person you're talking to use the same compiler.
    Come upgrade time, you'll both have to learn the same stuff all over again.
    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
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Originally Posted by qqqqxxxx
    fflush(stdin) is not undefined behavior,some compilers support it and its behavior is very defined,only the c standard says that its behavior should be undefined,but thats all there is to it.
    Fortunately, this forum is called "C programming" not "The idiosyncracies of XYZ compiler", so the C standard saying the behaviour is undefined is very relevant to this forum. Stop recommending people use things that are undefined.
    Yes, I must agree. I'm still a newbie to C but during the first 2 weeks of my C experience when I still had no idea what was going on, I came across that whole fflush(stdin) issue and it really threw me for a loop (I'm still not 100% sure I understand what is going on with it).

    The reason I got so confused was because my teacher plus several other C "authorities" that I questioned insisted that fflush(stdin) was OK to use and should always work. However, it didn't work on my computer (I use a Mac and the rest of the class were using PC). I lost a lot of time trying to figure out who to trust and, as a result, ended up behind the class for several weeks (I had to put in a lot of extra hours to catch up).

    Misinformation like this can be incredibly damaging to newbies who don't know any better. I can say this with authority since I learned the hard way. If you are going to suggest something like this, please, at the very least, point out that it is not transportable and is undefined under the ANSI C standard. Saying that it is defined because some compliers support it is as I understand it (and please correct me if I am wrong), not only incorrect but incredibly misleading.

    TV

  10. #10
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by qqqqxxxx
    so why didnt the rest of your class have problems?

    Because they were all using the same compiler. I was the only one using a different complier.

    Actually, to be more accurate, we were all, in theory, using the same compiler (GCC) but, for some reason that I still do not fully understand, it worked on the Windows machines using Dev-C but didn't work for me running MacOSX and Xcode.

    I suppose that, logically, the only explanation is that, despite the fact that both types of computer are supposed to be using GCC (at least that is what my teacher, who has proven to be somewhat unreliable, says), there is a subtle difference between the two compilers that I do not know about.

    EDIT - Yes, I do want to stick to the standard because I want to learn C. I do not want to learn some non-standard, non portable version of C that only SOME compilers can understand. I want my code to be able to complie on ALL compilers. That's why standards exist.
    Last edited by tvsinesperanto; 03-16-2006 at 03:28 AM. Reason: qqqqxxxx edited his question so I edited my answer to suit

  11. #11
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Sorry, but I'm afraid that I don't understand what you are talking about qqqqxxxx

  12. #12
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by qqqqxxxx
    lmao how could u forget,ritchie and kernigham,,THE VERY FOUNDERS OF THE LANGUAGE,
    AND do u have relations in the ANSI C commitee?

    and r u going to throw your neat 4 word code rite in your professors face and tell him that u r smarter than him?

    Ok, I do know who K&R are, I just don't understand what you are trying to get at. Your posts are terse to the point of being incomprehensible.

    Perhaps you would like to expand on your posts so that they are clearer.

    EDIT - In response to your question, I am not going to throw any code at anyone. I never suggested I was. Also, I have no idea what "4 word code" you are talking about.

    Secondly, it is not up to me to say who is right and who is wrong. The standard will do that for me.
    Last edited by tvsinesperanto; 03-16-2006 at 03:44 AM. Reason: Forgot to answer part of a question

  13. #13
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by qqqqxxxx
    lmao ,dont mind,just amusing myself,now delete all your replies just like i am going to do to mine
    I think I'll leave mine thanks

    EDIT - Why go to all the trouble of posting 8 or 9 posts aking me questions and making obscure allusions to the benefits/pitfalls of standard and non-standard C only to delete them all almost immediately afterwards.

    What a waste of time!
    Last edited by tvsinesperanto; 03-16-2006 at 03:51 AM. Reason: total incomprehension

  14. #14
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by qqqqxxxx
    why is that?
    See my edit above

  15. #15
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by qqqqxxxx
    i figured out that since u have me quoted in all your posts,why have my posts at all?
    Then why did you suggest I should delete mine as well? There would have been no record at all then.

    Look, this is going nowhere and is now so far off topic it isn't even funny. Forget about it.

    end.

    TV

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM