Thread: Unable to scan a character repeatedly

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32

    Unable to scan a character repeatedly

    Here's the code I wrote:

    Code:
    #include<stdio.h>
    #include<conio.h>
    void initialize(int array[9][9])
    {
         int i = 0, j = 0;
         for(i = 0; i < 9; i++)
         {
               for(j = 0; j < 9; j++)
               {
                     array[i][j] = 0;
               }
         }
    }
    void display(int array[9][9])
    {
         int i = 0, j = 0;
         for(i = 0; i < 9; i++)
         {
               for(j = 0; j < 9; j++)
               {
                     printf("%d   ", array[i][j]);
               }
               printf("\n");
         }
    } 
    int main(void)
    {
        int array[9][9];
        char c;
        initialize(array);
        display(array);
        printf("Enter the array elements: \n\n");
        while(1)
        {
                printf("\nWant to enter array element? ");
                scanf("%c", &c);
                if(c == 'y')
                {
                     printf("Accepted");
                     
                }
                else
                {
                    printf("Bye bye");
                    break;
                }
        }
        getch();
        return 0;
    }
    The scanf function works only once, it only scans the character once.
    On the second iteration, the scanf function is skipped and the loop exits.

    Am I do anything wrong?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by Shyam Raj View Post
    Am I do anything wrong?
    Sort of ... you expect to press 2 keys, but the program to ignore one of them. It doesn't!
    Try printing the character you just read right after you read it in.

    Code:
                /* ... */
                scanf("%c", &c);
                printf("character read in: '%c' (value %d)\n", c, c);
                /* ... */

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    When you've done with qny's suggestion and seen what is happening, you do this

    scanf(" %c", &c); //!! note the leading space in the format
    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.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Also another irrelevant with scanf suggestion from me.
    Instead of having the prototype of the function like
    Code:
    void myFunction(int myArray[9][9])'
    i highly recommend you yo have it like this
    Code:
    void myFunction(int** myArray , int n);
    Why?
    Because in this way you make your function far more useable and re-usable in future programs, because know it will work for every size you want!

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32
    Hi qny, I understood what you said. Thanks for that.

    Salem, why is that we need to add a leading space? Why doesn't it work the way I tried?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Instead of having the prototype of the function like
    void myFunction(int myArray[9][9])'

    i highly recommend you yo have it like this
    void myFunction(int** myArray , int n);
    This does NOT work.

    A true 2D array does not reduce to a **

    You can get to int (*array)[9], int n, so you can pass in any array[X][9]

    But there is no(*) way to pass any 2D array to a generic function.


    (*) Unless you resort to array flattening and manual subscript calculations.
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Salem, why is that we need to add a leading space? Why doesn't it work the way I tried?
    A leading space causes scanf to discard any leading whitespace, such as the newline at the end of the previous input.
    This makes it behave like %s, %d, %f etc.

    %c (by itself) always takes the next character on the input stream (including any newlines left by previous scanf calls).
    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.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    scanf() was designed to deal with formatted data (reason for its name: scan_formatted).
    User input and formatted data are very very different.
    I suggest you use a function more capable of dealing with user input: fgets() and afterwards parse the input string for your needs (maybe with sscanf() or strtol() or ...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with repeatedly calling the user
    By AlexTank853 in forum C Programming
    Replies: 1
    Last Post: 09-30-2012, 02:59 AM
  2. Scan a string, present character
    By emdleb in forum C Programming
    Replies: 6
    Last Post: 02-11-2011, 10:21 AM
  3. To call a function repeatedly after certain period
    By babu198649 in forum C++ Programming
    Replies: 23
    Last Post: 07-15-2008, 08:21 AM
  4. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  5. save repeatedly entered info
    By dpacinator in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 07:54 PM