Thread: [HELP] Force stop for input

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17

    [HELP] Force stop for input

    What are the ways to get a string input from user that stops accepting input when the input's length is already 30 without the need to press ENTER or any other keys. Maybe you can show some code?

    My first attempt is to use:
    Code:
    char string[31];
    
    fgets(string, 31, stdin);
    The problem is that it still allows unlimited input and when you hit ENTER it only takes the first 30 characters.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What operating system are you on?

    If you are using a windows based operating system, you could make your own loop with the getch() function.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17
    Quote Originally Posted by Click_here View Post
    What operating system are you on?

    If you are using a windows based operating system, you could make your own loop with the getch() function.
    Good call. But then how could I detect ENTER or backspace in getch() in conio.h?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want unbuffered input, you have to use non-standard C:

    conio.h, ncurses, or something (like an API), from the operating system.

    I can show you how to do it with conio.h, but your compiler needs to support conio.h (and you need to have it of course).

    I'm not clear WHY you don't want to require the enter key be hit, to end the user's input? That would be the normal way to stop user input. If the input buffer is overfull, then that can be cleared out easily enough.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Click_here View Post
    What operating system are you on?

    If you are using a windows based operating system, you could make your own loop with the getch() function.
    You mean all Windows compilers have getch()? I thought it was only 1 - 2% of them...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by WaltP View Post
    You mean all Windows compilers have getch()? I thought it was only 1 - 2% of them...
    This is a quote from "Expert C Programming: Deep C secrets", page 213 from the section "Getting a Char Without a Carriage Return"

    "The C compilers from Microsoft and Borland provide getch() (or getche() to echo the character) to get input character-by-character without waiting for the whole line"

    From the MSDN site: The getch() function is compatible with "Win 98, Win Me, Win NT, Win 2000, Win XP"
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Works with Windows 7, also.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    OK, what about this partial list of Windows C/C++ compilers?
    Code:
    AMPC
    Bloodshed Dev-C++
    Borland C++
    C++ Builder
    C++ Compiler
    CCS C Compiler
    Ch
    CINT
    Clang
    CodeWarrior
    Comeau C/C++
    CoSy compiler development system
    CParser/libFirm
    Digital Mars
    Dignus Systems/C
    DMS Software Reengineering Toolkit
    EDGE ARM C/C++
    GCC
    IAR C/C++ Compilers
    Intel C++ Compiler
    Interactive C
    LabWindows/CVI
    Lattice C
    LCC
    Mark Williams C
    Microtec
    MikroC Compiler
    MULTI
    Open Watcom
    Pelles C
    PGCC/PGCPP
    Portable C Compiler
    QuickC
    RCC (RCOR C Compiler)
    RealView C/C++ Compiler (armcc)
    Ritchie C Compiler (PDP-11)
    Salford C++ Compiler
    SAS/C/C++
    Small Device C Compiler
    Small-C
    Systems/C,C++
    Tiny C Compiler
    Turbo C
    Turbo C++ Explorer
    VBCC
    VectorC
    Visual C++
    VisualAge C++
    Wind River (Diab) Compiler
    Do they all have conio.h? Yes, some do. But how many?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I can only speak for "CCS C Compiler"

    That is a non-ANSI PIC microprocessor programmer that I use all the time.

    And it does have getch() for streams like USART.

    Where did you get the 1% from?
    Fact - Beethoven wrote his first symphony in C

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If they are "Windows" compilers, then presumably they all have it. Why wouldn't they? It's up to you to find a "Windows" compiler that doesn't support it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Okay.

    Well, "Ritchie C Compiler" is not a "Windows" compiler.

    Many of those are not "Windows" compilers.

    Actually, not even every compiler on that list has support for the "stdio.h" header.

    Suggesting alternatives is fine, but you aren't doing that; you are attacking a point that has not been made.

    So, I'm curious, what exactly do you recommend?

    Don't forget, by your own standard every "Windows" compiler must support any suggestion you make!

    Soma

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by oogabooga View Post
    If they are "Windows" compilers, then presumably they all have it. Why wouldn't they?
    Why would they have it? Simply because Lattice C started it, then M$ and Borland picked it up? If the compiler designers wrote to the standard, why would they have conio.h?

    Quote Originally Posted by phantomotap View Post
    O_o
    What the heck does O_o mean anyway?

    Quote Originally Posted by phantomotap View Post

    Well, "Ritchie C Compiler" is not a "Windows" compiler.

    Many of those are not "Windows" compilers.
    All those compilers have Windows versions, including Ritchie. I removed all compilers from the original list that has no Windows versions.

    Quote Originally Posted by phantomotap View Post
    Suggesting alternatives is fine, but you aren't doing that; you are attacking a point that has not been made.
    There are no alternatives, officially. The statement I take exception with is "all Windows C compilers implemented conio.h."

    Quote Originally Posted by phantomotap View Post
    So, I'm curious, what exactly do you recommend?
    I recommend not stating "all Windows C compilers implemented conio.h" unless it's true. If the compiler in use does, fine. But conio.h is not a Windows-required header.


    And,
    Do you really need to be obnoxious with the text enhancements?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I do know enough about different C compilers for Windows - So I'm happy to learn more.

    Could you point out compilers that are for making c programs on Windows and do not have the getch() option?
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    All those compilers have Windows versions, including Ritchie. I removed all compilers from the original list that has no Windows versions.
    O_o

    If you don't know the difference between targeting a system and running on a system you shouldn't be talking about this at all.

    The statement I take exception with is "all Windows C compilers implemented conio.h."
    Even when a compiler doesn't come with the "conio" library you can easily add support by using an implementation from a different vendor.

    I recommend not stating "all Windows C compilers implemented conio.h" unless it's true.
    If a compiler has the set of headers and libraries available that are required to do WIN32API programming, "conio.h" support is available by definition as there are free implementations available.

    But conio.h is not a Windows-required header.
    The "Windows" headers themselves are not a requirement.

    Do you really need to be obnoxious with the text enhancements?
    ^_^

    Do you?

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to stop reading input...
    By intull in forum C Programming
    Replies: 8
    Last Post: 02-20-2011, 03:38 AM
  2. Scanf sometimes do not stop for input
    By kapil1089thekin in forum C Programming
    Replies: 1
    Last Post: 10-03-2010, 07:25 AM
  3. Force user input algorithm to stop at \n?
    By Elysia in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 06:04 AM
  4. How to force user to input a value in the type limit
    By salvadoravi in forum C Programming
    Replies: 11
    Last Post: 01-21-2008, 09:47 AM
  5. stop input being echoed
    By Kinasz in forum C Programming
    Replies: 11
    Last Post: 07-25-2004, 11:02 AM