Thread: geting input in C, problem in getting input

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    geting input in C, problem in getting input

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        while(1)
        {
        char *client_balance[]={"20000", "18000", "16000", "14000", "12000"};
        int client_num=2;
        char buf[6];
        start:
        system("PAUSE");    
        printf("Please type the new PIN:");
        fgets(buf,7,stdin);
        printf("%s\n", buf);
        client_balance[client_num]=buf;
        printf("%s\n", client_balance[client_num]);
        }
       
      system("PAUSE");    
      return 0;
    }

    My Input:
    PIN: 123456789
    Output:
    123456
    123456

    then it does not wait for my input, it uses the "789" leftover from before,
    How to fix this bug? Make it ask me for input each time and use the 6 digits i enter only?

    i want the extra numbers to be discarded,
    i.e
    if first time i entered 123456789
    i want before and after to have the first 6 digits
    and next time, to ask for my iput again, so that i can enter new numbers

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Woop?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you trying to read more digits than your buffer holds?
    Code:
    char buf[6];
        start:
        system("PAUSE");    
        printf("Please type the new PIN:");
        fgets(buf,7,stdin);

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

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by quzah View Post
    Why are you trying to read more digits than your buffer holds?
    Code:
    char buf[6];
        start:
        system("PAUSE");    
        printf("Please type the new PIN:");
        fgets(buf,7,stdin);

    Quzah.
    i just wanna know, what happens if we try to read more inputs than declared using fgets().does it functions similar to gets(), i mean it causes buffer overrun.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  4. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 11:34 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM

Tags for this Thread