Thread: getchar()

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    getchar()

    I've got a simple program which asks users for a number 1- 10 and then checks if its a digit or not. At the end I want to prompt the user if they want to continue or exit
    and if they want to continue I want to go back to the top.

    I put the user's data in an array called c[i] for each character using getchar() and a
    loop.

    I've read that to clear the input buffer I need to do something like:

    while( (d = getchar()) && d != '\n' && d != EOF)
    d = getchar;

    Problem, input buffer doesn't get cleared. If it put another clear buffer at the top then the user must hit enter but they don't know that.


    insert
    Code:
    int main()
    {
      
        
        int i, j;
        char c[100];
        int lengthOfInput = 0;
        char d = 0;
        
        j = 0;
        
        while ( j == 0 )
        {
    
        // initialize array
        
        for (i = 0; i <= 100; ++i)
        {
            c[i] = '\0';
        }
        
        /* Ask for input */
        printf("Enter a number from 1-10.\n");
        for (i = 0; c[i - 1] != '\n'; ++i) {
            c[i] = getchar();
            lengthOfInput += 1;
        }
        
        
        /* Take care of 10 */
        if (((lengthOfInput-1) == 2) && (c[0] == '1') && (c[1] == '0'))
            printf("\'10 divided by 25 is %.1f'\n", 10.0/25.0);
        else if ((lengthOfInput-1) == 1)
        {   
            if (isdigit(c[0]))
            {
                switch(c[0])
                {
                        
                    case '4':
                        printf("You chose %c. 4 + 10 is %d.\n", c[0], 4 + 10);
                        break;
                    default:
                        printf("You chose %c.\n", c[0]);
                        break;
                }
            }
            else
            {
                printf("Not a valid digit\n");
            }
        }
        else
            printf("Not a valid digit 2.\n");
            
            
            printf("Hit 0 to continue\n");
            
            while ((d = getchar()) && d != '\n' && d != EOF) {
                d = getchar();
                if (d != '0') 
                    j = 1;
            }
        }
        
        
    }
    Last edited by bbray; 09-07-2011 at 10:10 AM. Reason: Accidentally typed in wrong variables

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    // wrong
    while( (c = getchar()) && c != '\n' && c != EOF)
    c = getchar;
    
    
    // better
    while( (c = getchar()) && c != '\n' && c != EOF)
    c = getchar();
    You need to watch your compiler's warnings and errors... it should have complained about the missing brackets.


    And here's a little trick for you... If you use scanf() instead of getchar(), you can tell it to ignore "whitespace" (spaces, tabs, \n, etc) in the buffer like this...
    Code:
    scanf (" %c", &charvar);
    Note the space between the " and % ...
    Last edited by CommonTater; 09-07-2011 at 10:14 AM.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Yes, you are right, it would complain. I typed it in wrong. In the code I had it right.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    @bbray,

    Since you declared d as a char type variable, you ought to read this.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by kermit View Post
    @bbray,

    Since you declared d as a char type variable, you ought to read this.
    Thanks! I went back to the drawing board(mostly) and implemented the change you pointed out. It works!!

    Here's my new program.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main ()
    {
    
        int c, i, count, tenCheck, e;
        char number[10];
        char d;
        
        d = '1';
        while (d == '1') 
        {
            
        
        /* Initialize array */
        for (i = 0; i < 10; ++i) 
        {
            number[i] = 0;
        }
        
        /* Get number */
        i = 0;
        count = 0;
        printf("Enter a number between 1 and 10\n");
        while ((c = getchar()) && c != EOF && c != '\n') // Clear input buffer in case of previous iteration
        {
            number[i] = c;
            ++count;
            ++i;
        }
            
        /* Check if 10 was chosen */
        tenCheck = 0;
        if ((count == 2) && (number[0] == '1') && (number[1] == '0'))
        {
            printf("10 divided by 25 is %.1f\n", 10.0/25.0);
            ++tenCheck;
        }
        if (tenCheck == 0)
        {
        /* Check for digit */
        e = 0; // Initializes case if '0' is chosen
        for (i = 0; i < count; ++i) 
        {
            if ((count == 1) && (isdigit(number[0]))) 
            {
                switch (number[i]) 
                {
                    case '4':
                        printf("4 + 10 is %d\n", 4+10);
                        break;
                    case '0':
                        printf("You entered \'0\'.\n");
                        printf("Try again.\n");
                        e = 1;
                        break;
                    default:
                        printf("You chose %c.\n", number[i]);
                        break;
                }
            }
            else
                printf("Not a digit\n");
            
        }
        }
        /* Check if zero was entered, if so re-run program */
        if (e == 1)
        {
            d = '1';
        }
        else
        {
            /* Prompt if user wants to run again or not */
            c = 0;
            printf("\nEnter 1 to continue, enter any other key to end.\n");
            while ((c = getchar()) && c != EOF && c != '\n')
            {
                d = c;
            }
        }
        }
            
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-25-2009, 09:51 PM
  2. Why do I need getchar(); in this example?
    By JDShaffer in forum C Programming
    Replies: 10
    Last Post: 05-15-2007, 06:53 AM
  3. getchar() and '\b'
    By snfiddy in forum C Programming
    Replies: 2
    Last Post: 11-30-2005, 05:52 PM
  4. getchar
    By pktcperlc++java in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2005, 08:31 PM
  5. getchar()
    By linuxdude in forum C Programming
    Replies: 7
    Last Post: 09-05-2004, 09:17 PM