Thread: Best way to get user input?

  1. #1
    Registered User purple's Avatar
    Join Date
    Mar 2002
    Posts
    28

    Best way to get user input?

    I'm trying to hone and expand my C programming skills and I wanted to get some ideas on the best way to get user input at runtime, not command line, to avoid overflow.

    This isn't for any program in particular, just for my own personal gain and that of anyone that reads this post.

    Feel free to post examples as they will most likely be the best way to demonstrate to myself and others.

    I have already searched the boards, so sincere replies only please.

    Thanks...
    Last edited by purple; 08-01-2002 at 01:31 AM.

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    i agree that this is a good thing to discuss. but i'm not very knowledgeable in this area either :/

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It depends what you want to read. Want a string? Use fgets. Want a character? Use fgetc. Want a number? Use fgets. See a pattern here?

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

  4. #4
    Registered User purple's Avatar
    Join Date
    Mar 2002
    Posts
    28
    quzah you failed to understand the main point of my post: sincere replies only please.

    I don't know why everyone must act like a smart ass when people ask legitimate questions.

    However, you did give a decent option and I was able to implement fgets with the standard input stream since the third parameter requires a file stream pointer.
    Code:
    #include <stdio.h>
    
    #define MAX 10
    
    void main()
    {
    	char ar[MAX];
    	
    		printf("\n- Enter some sheeot: ");
    
    		//the "stdin" parameter specifies the standard input stream
    		fgets(ar, MAX, stdin);
    		printf("\n- The contents of the array up to its maximum amount (%d): %s", MAX, ar);
    
    		//keeps the window open
    		printf("\n\n- Press ENTER to exit\n");
    		fgets(ar, MAX, stdin);
    
    	return;
    }
    Last edited by purple; 08-01-2002 at 04:12 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    quzah you failed to understand the main point of my post: sincere replies only please.

    I don't know why everyone must act like a smart ass when people ask legitimate questions.

    However, you did give a decent option and I was able to figure out how to use fgets with the standard input stream since the third parameter requires a file stream pointer.
    What the **** are you talking about? That was a perfectly legitimate answer, and I was 100% serious in all respects.

    1) If you are trying to read a string:

    fgets( buf, BUFSIZ, stdin );

    2) If you are trying to read a character:

    int c;
    c = fgetc( stdin );

    3) If you are trying to read anything other than a character:

    fgets( buf, BUFSIZ, stdin );
    ...then validate the data...

    I am 100% serious. fgets is without a doubt THE BEST way to read data, especially strings. Period. If I only had one option ever available to me to read data from the keyboard, it would be either fgets or fgetc.

    Actually, were I to only have the option of ever using one again, I'd use fgetc. However, I'm more likely to know how to use it better than you (no slander intended) or a novice user, so if I was recommending one and only one way of getting data, I'd say use fgets.

    There was no sarcasm or non-seriousness at all in that post. I stand by what I said 100% and dare someone to find a better way than what I have suggested.

    There's your challenge. Find a better way of reading data [from the console] than what I have suggested that is portable and will prevent buffer overflow. You cannot use fgets or fgetc, since I've already claimed them.

    I doubt you'll find a better way than either of those two. I'd love to see it.

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

  6. #6
    Registered User purple's Avatar
    Join Date
    Mar 2002
    Posts
    28
    I appreciate you clarifying and I apologize for calling you a smart ass, but your "See a pattern here?" comment came by as a little cocky.

    Anyone else have any other decent methods to prevent overflow at runtime?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "See the pattern here?" was to make a point that there is nothing else to look for. Use these functions and you can't go wrong.

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

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    2) If you are trying to read a character:

    int c;
    c = fgetc( stdin );
    Can I use getch() or getche() ?
    If not, may i know what's wrong with them?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Krupux
    Can I use getch() or getche() ?
    If not, may i know what's wrong with them?
    They're non-standard functions, meaning that not all compilers will have them. If you want to write portable code, you have to avoid them.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 06:37 AM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM