Thread: put number in array

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    put number in array

    Hi,
    I'm new with C programming and I would like to know the code that would allow me to enter numbers like 987654321 and put each of them in an array [0] --> 9, [1]-->8...

    do
    { puts("Enter your nine number ");
    gets(number);
    AnalyseNumber //where I want my array with my numbers

    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    gets() is evil and should never be used. Use fgets() instead. But if you've got your number string you could simply do:
    Code:
    for(i = 0;i < 9;++i)
      array[i] = number[i] - '0';
    The - '0' effectively removes the shift inherit in the ASCII values (e.g. it turns '7' into 7, '5' into 5, etc.).
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    gets() is evil and should never be used.
    gets() is easy and doesn't save the new line character. Never use it in any serious programs, but it's okay for quick or test programs that don't last long and only you use.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I'd still stay away from it. Especially if you were to be a beginner at C and weren't sure of what could go wrong with it.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Sure, if all that everyone say is gets() is evil so you don't ever find out what could go wrong with it. If you know what could go wrong, you can use it safely. If you don't know what could go wrong, you can still get away with using it in throw away programs.

    For nevrax, here's why gets() is "evil". It only takes one parameter, the array to put a string into, but the array has a finite size and gets() has no way of telling what that size is. So gets() assumes that the array is big enough and just copies everything it gets even if the array isn't big enough. That's called buffer overrun, and it can be used by hackers to circumvent the security on a computer and break into it.

    But if you're the only one who uses your program, even if you don't know the size of an array that you declared, chances are good that you're not trying to break into your own computer, right? It's just that easy. Not really as evil as you were led to believe, huh?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    fgets() is not much harder to use. It's probably a good idea for new programmers to get used to using it in test programs, so when they work on "serious" projects, they don't have to worry about remembering how to use it.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > chances are good that you're not trying to break into your own computer, right?
    How about learning to do it the right way all the time, rather than second guessing who's going to use it?

    If you do it the right way out of habit, then it will be right when it matters, not when you can be arsed to make it right.
    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
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    fgets() is not much harder to use.
    Here's a fun problem, explain how fgets() is not much harder to use to a beginner when he encounters it:
    Code:
    fgets( password, sizeof password, stdin );
    
    if ( strcmp( password, "opensezme" ) == 0 ) {
      // let the user in
    } else {
      // mean error message
    }
    How about learning to do it the right way all the time, rather than second guessing who's going to use it?
    You're assuming that gets() is wrong. It's like saying that strcpy() is wrong for the same reason, or calloc is wrong because 0 filling doesn't mean initializing to 0, or printf is wrong because it's not type safe, or scanf is wrong because it's not the inverse of printf. How many legitimate functions do you ignore because of some imagined sense of right and wrong, Salem?

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    strcpy() is not wrong in the same sense because you can take care to never mess up with it. You can always be aware of the sizes of the strings in question. Again, with printf(), same type of argument. If something goes wrong with printf(), you messed up as a programmer.

    With gets(), you are totally at the mercy of whatever happens to be coming in through stdin. scanf() is also bad if you use it improperly, and even if you use it properly, you're still somewhat at the mercy of whatever stdin is holding for you.

    Safest way of reading input is generally to use fgets().

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I know there is no possible way to make gets() safe in any circumstance you care to mention.

    You can do what the hell you like in your own code, but for sure you're never going to get away with using gets() on a forum without someone hauling your ass over the hot coals for it.

    > Here's a fun problem, explain how fgets() is not much harder to use to a beginner when he encounters it:
    Awww - and the solution is so easy to fix.
    Is it that hard to explain one extra line of code?
    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.

  11. #11
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    strcpy() is not wrong in the same sense because you can take care to never mess up with it. You can always be aware of the sizes of the strings in question. Again, with printf(), same type of argument. If something goes wrong with printf(), you messed up as a programmer.
    It's the same way with gets(). It's perfectly safe if you control the input and can guarantee that it all fits in the array. The only difference is that gets() is "evil" so you make an exception.
    I know there is no possible way to make gets() safe in any circumstance you care to mention.
    I wrote the program so I know how big the array is, I'm the only one who uses the program so I control the input, and I know the problem with gets() so I make sure not to overrun the buffer. That's my circumstance, and it's perfectly safe no matter how much hand waving you do.
    You can do what the hell you like in your own code, but for sure you're never going to get away with using gets() on a forum without someone hauling your ass over the hot coals for it.
    So that stuff about doing it right out of habit was a load of bull? If the only use of gets() you care about is code posted here, then that kind of makes you a hypocrite. You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see. Your argument is very convincing.
    Awww - and the solution is so easy to fix.
    Is it that hard to explain one extra line of code?
    At least 2 lines if you don't do something obscure and use good formatting, an extra variable if you want it to be fast, and yes, it's hard to explain to a beginner if you want them to understand the problem and the solution so they don't do it again. Beginners won't want to write code if they think it's useless, and that's useless when they look at how easy gets() is.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Noir
    It's the same way with gets(). It's perfectly safe if you control the input and can guarantee that it all fits in the array. The only difference is that gets() is "evil" so you make an exception.
    There is a reason why it is considered deprecated.

    For you to write one or two lines more for fgets() to avoid any crashes and bugs later can hardly take too much of your time. Why you would not want to do that, and why you would encourage new programmers to get used to using a deprecated function is beyond me.

    Quote Originally Posted by NOIR
    I wrote the program so I know how big the array is, I'm the only one who uses the program so I control the input, and I know the problem with gets() so I make sure not to overrun the buffer. That's my circumstance, and it's perfectly safe no matter how much hand waving you do.
    If you can control the input, you are correct, it is "safe", even though you can still crash the program, however. If you want to write programs that you can crash, that is your right, but understand that you are on your own for that. If your test programs don't work when you are sloppy in your coding, don't be surprised. Many people will not want to help debug those type of programs, and on top of it, some may say thay you deserve any errors that you get.

    Quote Originally Posted by NOIR
    So that stuff about doing it right out of habit was a load of bull? If the only use of gets() you care about is code posted here, then that kind of makes you a hypocrite. You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see. Your argument is very convincing.
    Don't be obtuse. We can't stop you from writing garbage on your own. If you want to write spaghetti code with goto, for example, we can't stop you from doing it, even though we will tell you it's wrong. Same with any other bad coding practice. If you post it on the forums, you obviously shouldn't expect anyone to help you with it, and we will tear you apart for posting it when you know better.

    Quote Originally Posted by NOIR
    Beginners won't want to write code if they think it's useless, and that's useless when they look at how easy gets() is.
    When they are taught to use gets(), they will think it's the proper way to read a string. If taught otherwise from the start, they can save themselves many problems resulting from pointer/buffer issues.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I think what you're forgetting is that a lot of good books on C, and classes as well, teach using gets() and scanf().

    Your arguments are all logical, BUT they ignore the reality of what the students are coming here with in their class notes, and in their books.

    I believe a beginner should learn and work with gets() and scanf(), just to become somewhat familiar with them AND with their potential for trouble. THEN as intermediate students of C, they go on to practice fgets() and etc.

    I don't see the point of "raking someone over the hot coals" for using or explaining code with gets() or scanf(). A learning program is just for learning - no commercial robustness is needed, or should be expected.

  14. #14
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I can confirm that most tutorials and C classes, especially when dealing with people who first venture into the world of programming, only mention scanf() for formatted and gets() for any other input method. However, they almost always mention that there is a problem with these two and only later on come back on that exact problem and explain how to do it "better".

    In the K&R The C Programming Language, they don't mention gets() and puts() at all, apart from the function listing at the end. They start by using only getchar() and later on formatted input with scanf, then immediately explain fgets().

    However, a beginner doesn't care about "safe" code as long as his program runs 99% of the time or even worse, corresponds to his homework assignment. In fact, some stuff, as you can see here is terribly hard to explain with all the details you have to watch out for, that beginners start losing their hope because they think C is too hard.

    As long as you mention that there's a problem with buffer overflows, I don't think it's a problem to explain something with gets() but most of the time I decide based on the experience grade of the op.

  15. #15
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Yeah, it goes without saying that we should teach beginners why gets() should to be avoided so they can decide when to listen. I can't believe that people here really think a "do it this way or else" attitude promotes learning, so they probably just aren't expressing themselves well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM