Thread: put number in array

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > If the only use of gets() you care about is code posted here, then that kind of makes you a hypocrite.
    Well obviously because I have no way of controlling (or even commenting) on what you do in private.
    Just so you know, I always use fgets(), no matter how trivial the program is.

    > 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.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things.

    > How many legitimate functions do you ignore because of some imagined sense of right and wrong, Salem?
    Lots of them - especially if I'm coding to MISRA standards.
    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.

  2. #17
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by Salem
    ... but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    this made my day

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Just to weight in on a heavy conflict. What does fgets() use or do differently then scanf() and gets(). Also back in the day when I was learning C at my old summer job, the book "C for dummies" quickly told the user after 1 page of using gets() that there is bug and not to use it. As I would like to say Noir has the upper hand in the debate based on his iambic pentatonic blank verse but Salem is right. Salem who does enjoying the coals of Satan behind the cynicism is really telling us (and those who agree with him) that well why would you want to be using something that will only cause you pain in the end, for an extra line of code you can achieve a higher understanding of C and prepare yourself for the future. Now I am not sure as to what the real issues here are about gets() and scanf() but if there is some form of error then I have no choice but to discontinue using them. I don't mind the extra line of code and if it going to make my life easer in 5 years then why waits 5 years to get comfortable with it? I must say it was comical how Noir attacked Salem but Noir who is new and has little to stand on VS the god of C in my book; it just isn't a fair fight! Now quzah VS Salem, that’s a fight I would pay to see.
    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.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  4. #19
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    gets() and fgets() treat the new line character different. gets() reads it and throws it away, so the string doesn't ever have a new line at the end, and fgets() reads it and copies it to the string, so the string sometimes has a new line at the end and sometimes it doesn't. scanf() does everything different and has more problems than just a new line.
    that well why would you want to be using something that will only cause you pain in the end, for an extra line of code you can achieve a higher understanding of C and prepare yourself for the future
    That's not the point. Yeah, gets() is a really bad choice in code you're going to keep or let others use. If you already know the problems and the solutions, that doesn't give you a higher understanding of C in toss away programs, it just gives you that much more boilerplate to write. If you don't know the problems or the solutions, Salem and friends want you to blindly use fgets() and not bother learning at all. Read this thread and see who explained what the problem is with gets() and who just called it "evil" or "wrong".

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    If you don't know the problems or the solutions, Salem and friends want you to blindly use fgets() and not bother learning at all.
    Here is the blind truth, I don't care why we have to do it a certain way. If you had a jigsaw skill saw, you don't use your hand to test the blade. Its common sense to lose the gets() (even though I never used it) and the scanf() (my favorite autistic friend) if there are issues. For the record in your quote are you saying that people can't learn another function or a better way?
    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.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  6. #21
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    What does fgets() use or do differently then scanf() and gets().
    fgets vs gets
    The basic idea is that with fgets you can specify how many characters you want to read, whereas with gets, you just send a buffer and it will stick everything from stdin into that buffer (even if it goes over the bound). As for scanf, I think Dave_Sinkula had started a thread a while back explaining what was wrong, but essentially you just can't be sure of how scanf will react to incorrect input (Don't quote me on that, it's from memory)

    Case in point, through the year and a half (close to 2 years) I've been here, the board here has been commited to providing correct, professional solutions to problems people could have with C. It's not about making one hit wonders. I feel that if people who ask questions here aren't willing to take the time to learn fgets to get input properly, they shouldn't be learning C.

    There's been tons of threads of people complaining about "but my teacher says use gets", which is fine. If your teacher tells you to use gets, sure, even if he's wrong, do it. But on the boards here, where things need to be correct no matter what, gets should never be suggested.
    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

  7. #22
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Salem and friends want you to blindly use fgets() and not bother learning at all.
    I wouldn't even mind if people did this. In every book I've read they teach you gets, but then tell you to never use it. That's like teaching you to drive a car, then taking the car away. If the only easy way to get input in the eyes of the people who write books is through gets and scanf, then maybe they should leave input handling to where it is more appropriate (AKA, not at the beginning.)
    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

  8. #23
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Ok question about fgets(), I wanted to try it out in an old program of mine.
    Code:
    #include <stdio.h>
    
    int main()
    {
    unsigned long int x,y;
    printf("welcome to the gfc:\n");
    fgets(x, sizeof x, stdin);
    printf("Second number:\n");
    fgets(y,sizeof y, stdin);
    }
    
    ok in Dev C++ 4.9.9.2 I get the following errors
    invalid conversion from "long unsigned int" to "char"
    Anyone care to explain what I am doing wrong? Does fgets only work with chars?
    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.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  9. #24
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Everything coming from outside your program is a character stream. Functions like scanf() automatically do formatting and conversions for you, but with unformatted functions like fgets(), you have to use a string and do the formatting and conversions yourself:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main( void )
    {
      unsigned long int x, y;
      char s[256];
    
      printf( "welcome to the gfc:\n" );
      fgets( s, sizeof s, stdin );
      x = strtoul( s, NULL, 0 );
      printf( "Second number: " );
      fgets( s,sizeof s, stdin );
      y = strtoul( s, NULL, 0 );
    
      return 0;
    }

  10. #25
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by KoG Metalgod View Post
    Ok question about fgets(), I wanted to try it out in an old program of mine.
    ...
    Anyone care to explain what I am doing wrong? Does fgets only work with chars?


    Did you ever take a look at the function prototype:
    Code:
    char * fgets ( char * str, int num, FILE * stream );

  11. #26
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    I don't understand why flamers come to boards, its a question and an answer. Thanks Noir for the response.
    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.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It wasn't a flame. It was in my opinion, "a question and an answer" all rolled into one. It was a question, asking you if you even bother looking at what the functions you try to use expect for arguments. It was an answer, because if you had, you wouldn't have wasted time posting on the forum when you would have solved it yourself.


    Quzah.
    Hope is the first step on the road to disappointment.

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