Question about limiting the number of user inputs in C

This is a discussion on Question about limiting the number of user inputs in C within the C Programming forums, part of the General Programming Boards category; Hi , I am new to C programming and have only basic knowledge about it, I just want to ask ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    Question about limiting the number of user inputs in C

    Hi , I am new to C programming and have only basic knowledge about it, I just want to ask you guys if you knew how to limit, for example, the number of inputs a user can input.

    To make my point clear I'll give an example,

    If I make a program that asks you a name, how can i limit your inputted letters to ,say, 5 letters only?

    Is it possible through simple coding? thnx and a pleasant day to you all.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is it possible through simple coding?
    Yes, but it's not portable. You'll need to provide your compiler and OS so that we can suggest non-portable options for directly controlling keyboard input.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    wow tnx for the fast reply.

    So it is posible . But what do you mean by controlling the keyboard input?
    Im using a c compiler from Borland, and im using windows XP.

    I was wondering if you could post a simple code about asking the user to input a 2 digit number (like 01 - 99) but wouldnt let you input 3 numbers.

    Thanks for replying again and sorry if i am asking for a code, I really cant understand it if i wouldnt see it in action, primarily because I cant understand english well coz im from philippines sorry bout that. I'll understand if it's not allowed tnx again!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > Im using a c compiler from Borland
    Not Turbo C surely?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    yeah turbo C++, thnx for pointing that one out !

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > Im using a c compiler from Borland, and im using windows XP.
    Buy new Ferrari.
    Replace engine with elastic band.

    You've just reduced your machine to one with 640K of memory, and you've sliced the processor in half.

    Your compiler is so old it was used by the Druids to build Stonehenge!.
    Try belonging to this millenium by using a more up to date compiler. There are many excellent and free compilers to choose from nowadays without having to use that old piece of museum tat.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    lol can you suggest a good compiler man?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    http://www.compilers.net/Dir/Free/Compilers/CCpp.htm
    - Borland C++ Compiler 5.5
    - Bloodshed Dev-C++
    - Cygwin Project (C/C++)
    - Digital Mars C and C++ Compilers
    - DJGPP
    - MingW
    - Watcom C++
    All of these would be fine choices. A lot of people go for dev-c++, as you get an editor and debugger as well, all in a single file download (easy to install).

    There's also visual studio express
    http://en.wikipedia.org/wiki/Visual_Studio_Express
    But you also need to download the platform SDK to make it useful.

    Avoid like the plague the "Miracle C", it's even worse than the one you have already.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code::Blocks is still in developement, but appears to have promise.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    I've seen Dev-C++ but it only works on windows 9x and 2k and I got a windows XP.
    I've also seen the other ones you recommended and I tried and liked the bloodshed one
    Hey guys thanks for your help in finding a good compiler.

    About my first question, do you guys know how to do this? because if I were to write a program, for example a program asking only 2 digit numbers, I could put as many numbers as I like.

    Thanks again guys!

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by chobibo
    I've seen Dev-C++ but it only works on windows 9x and 2k and I got a windows XP.
    I use WXP on my laptop. I have Deb-C++, Code::Blocks, Turbo. . . etc. . .etc.

  12. #12
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by Salem
    Not Turbo C surely?
    Banged, that shoots directly towards me.
    (I migrated to Visual C++ 2005 Express, + PSDK)

    Im quite sure that chobibo is Filipino as well,
    we have similar compilers before, fossil-like. (like what Mr. Quzah and Salem said)
    Last edited by Xero; 08-24-2006 at 09:48 AM.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could read in a whole line of text at a time, then check if the text represents a two digit number. If it doesn't alert the user and ask him/her to re-enter the input. Try doing an online search for "fgets" and "sscanf" for C, and "std::getline" and "std::stringstream" for C++, or just check out the FAQ for examples.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If I make a program that asks you a name, how can i limit your inputted letters to ,say, 5 letters only?
    You could limit the length of the string read in to five characters quite easily, but that doesn't prevent the user from typing more than that.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char name[6], *p;
        int c;
    
        printf("Enter your name: ");
        fgets(name, sizeof(name), stdin);
        if((p = strrchr(name, '\n'))) *p = 0;
        while((c = getchar()) != '\n' && c != EOF);
    
        printf("Hello, %s!", name);
    
        return 0;
    }
    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.

  15. #15
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    You can surely do this using ncurses, but this may require rewriting some of your code. PDCurses can be used on Windows, and it provides identical functionality as ncurses (look at the demos for PDCurses; they're quite impressive).
    But I notice that you are beginner, so this maybe too complex of a suggestion.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. pin number question
    By melee in forum C Programming
    Replies: 19
    Last Post: 12-11-2004, 12:39 PM
  3. Replies: 5
    Last Post: 05-30-2003, 12:46 AM
  4. C number operator question
    By unanimous in forum C Programming
    Replies: 6
    Last Post: 10-26-2001, 09:36 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21