Thread: getchar()

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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