Thread: getchar() problems

  1. #1
    decontrol
    Guest

    getchar() problems

    For some reason this isn't working and I don't know why. It seems to skip over 'c = getchar();' entirely.

    --------------------------
    Code:
    while (flag_done==0 && max<30)
    {
    	printf("Enter the person's first name: ");
    	scanf("%s", &person[max].firstname);
    	printf("Enter the person's last name: ");
    	scanf("%s", &person[max].lastname);
    	printf("Enter the person's age: ");
    	scanf("%d", &person[max].age);
    	printf("\n");
    	max++;
    	printf("Would you like to add another record? (Y/N)");
    	c = getchar();
    	printf("\n");
    	if(c=='n') {flag_done = 1;}
    }
    --------------------------
    Here is the output...

    --------------------------
    Enter the person's first name: FirstName
    Enter the person's last name: LastName
    Enter the person's age: 20

    Would you like to add another record? (Y/N)
    Enter the person's first name:
    --------------------------

    And the entire program...
    --------------------------
    Code:
    #include<stdio.h>
    
    struct rec
    {
    	char firstname[15], lastname[15];
    	int age;
    } person[30], t;
    
    void bubble_sort(int max)
    {
        int x,y;
        for (x=0; x < max; x++)
        {
            for (y=0; y < max-x-1; y++)
                if (person[y].lastname > person[y+1].lastname)
                {
                    t=person[y];
                    person[y]=person[y+1];
                    person[y+1]=t;
                }
        }
    }
    
    int main()
    {
    	int x, flag_done, max=0;
    	char c;
    
    	while (flag_done==0 && max<30)
    	{
    		printf("Enter the person's first name: ");
    		scanf("%s", &person[max].firstname);
    		printf("Enter the person's last name: ");
    		scanf("%s", &person[max].lastname);
    		printf("Enter the person's age: ");
    		scanf("%d", &person[max].age);
    		printf("\n");
    		max++;
    		printf("Would you like to add another record? (Y/N)");
    		c = getchar();
    		printf("\n");
    		if(c=='n') {flag_done = 1;}
    	}
    
    	printf("NAME\t\t\tAGE\n");	
    		
    	bubble_sort(max);
    	
    	for (x=0; x<=max; x++)
    	{
    		printf("\n%s, %s\t\t%d", person[x].lastname, person[x].firstname, person[x].age);
    	}
    
    	return 0;
    }
    --------------------------

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    26
    I think the reason why it skips is because c = getchar(); line
    is getting the enter you push after type in the age.

    I just compiled your program and if you enter in the line
    c=getchar();
    twice it worked.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    5
    Hi,
    I also had the same problem. The solution is to use fflush(stdin) before the getchar().
    I hope this will solve your problem.

    Happy coding
    Hitesh A. K.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Salem
    > The solution is to use fflush(stdin) before the getchar().
    A better solution is to read the FAQ
    Since reading the FAQ does not point to a better solution, try one of these:

    1) if fflush(stdin) does work and the code is not to be used on another compiler, use it. You're probably one of the lucky ones where the last line of the FAQ is true.

    2) Change all the scanf()'s to fgets() statements, and use atoi() to convert the age into an int.

    scanf() has always been strange in the way it handles input so I've found avoiding it is extremely helpful in curing mental anguish.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Salem
    What's this then - scotch mist?
    "To learn how to flush the input buffer correctly, read this FAQ entry."
    Yeah, the bit at the end is a hyperlink to another FAQ entry
    Sorry, missed that link, it wasn't colored. The difficulty with that FAQ is:
    However, if you call these when there is no data in the input stream, the program will wait until there is, which gives you undesirable results.
    Bottom line is with scanf() you might have extra data in your buffer (undesirable) and it must be cleared. Clearing the buffer works only if there is data in the buffer, which there might not be (undesirable). So scanf() causes more headaches than it's worth and requires more than neo-knowledge of C to cure. I therefore still recommend fgets() to replace scanf().

    And what's the origin of the phrase scotch mist? I haven't run across that term before.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. getchar() help!
    By Brian n. in forum C Programming
    Replies: 5
    Last Post: 12-10-2002, 11:44 PM
  5. getchar() and while loops
    By dbaryl in forum C Programming
    Replies: 2
    Last Post: 07-26-2002, 12:38 PM