Problem with getchar()

This is a discussion on Problem with getchar() within the C Programming forums, part of the General Programming Boards category; Hi all, I'm trying to scan a character in a function program using getchar() but for some unknown reason it ...

  1. #1
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70

    Question Problem with getchar()

    Hi all, I'm trying to scan a character in a function program using getchar() but for some unknown reason it doesn't work.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "addition.h"
    
    /* This function will create random exercises of addition */
    int addition()
    {
    	int first, second, answerout, answerin;
    	char repeat;
    	printf("Addition:\n");
    	do{
    	first = 1 + rand() % 10;
    	second = 1 + rand() % 10;
    	answerout = first + second;
    	printf("\n");
    	printf("   What is %d + %d = ? ", first, second);
    	scanf("%d", &answerin);
    	printf("\n");
    	while ( answerin != answerout)
    	{
    		printf("No. Try Again. What is %d + %d = ? ", first, second);
    		scanf("%d", &answerin);
    		printf("\n");
    	}
    	printf("Correct!\n");
    	printf("\n");
    	printf("Play Again? ");
    	repeat = getchar();
    	while ( getchar() != '\n');
    	} while(repeat == 'Y' || repeat == 'y');
    	
    	return EXIT_SUCCESS;
    }
    Hope you can find what's wrong.

    Thank you.

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    What does “doesn't work” mean in this context?

  3. #3
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by cas View Post
    What does “doesn't work” mean in this context?
    It doesn't declare repeat with the character that I want to input.

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    If I understand correctly, what the user types is not being stored into repeat, and so repeat is never equal to 'y' or 'Y'?

    If so, the problem is that you have code that is roughly the following:
    Code:
    scanf("%d", &whatever);
    repeat = getchar();
    while ( getchar() != '\n');
    So the user enters a number, and hits enter. The next call to getchar() thus returns a newline (because the user hit enter), which is assigned to repeat. Your loop then discards all user input until he hits enter again. Unless the user is doing something like entering "11y" for an answer, repeat will always contain a newline, and thus will never be 'y' or 'Y'.

    User input in C is an annoying thing. For a quick fix you can replace the second two lines in the excerpt above with:
    Code:
    while( (repeat = getchar()) == '\n' );
    This will keep reading characters until the user enters a non-newline character. repeat will contain the last character the user entered.

    You'll also want to make repeat an int, not a char. Despite its name, getchar() returns an int, and for good reason: otherwise, you would not be able to reliably detect EOF—something you'll want to do, at least at some point in your future C endeavors.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    this works.

    ive bolded the changes

    despite its name, getchar works with integers, not chars. I think this program will still work if you have char repeat, but it should be an integer. I believe the reason relates to EOF.

    i changed the do while to a while loop.

    Code:
    int addition(void) {
    	int first, second, answerout, answerin;
    	int repeat = 'y';
    	printf("Addition:\n");
       while(repeat == 'Y' || repeat == 'y') {
    	first = 1 + rand() % 10;
    	second = 1 + rand() % 10;
    	answerout = first + second;
    	printf("\n");
    	printf("   What is %d + %d = ? ", first, second);
    	scanf("%d", &answerin);
    	printf("\n");
    	while ( answerin != answerout){
    		printf("No. Try Again. What is %d + %d = ? ", first, second);
    		scanf("%d", &answerin);
    		printf("\n");
    	}
    	printf("Correct!\n");
    	printf("\n");
    	printf("Play Again? ");
    	repeat = getchar(); // this eats up the '\n' leftover from the scanf()
    	repeat = getchar(); // this gets their decision, y or n
    	}
    
    	return EXIT_SUCCESS;
    }
    you might also like to include

    Code:
    #include <time.h>
    ....
    srand(time(NULL)); // anywhere before the rand();
    which will give you random rumbers

  6. #6
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by cas View Post
    If I understand correctly, what the user types is not being stored into repeat, and so repeat is never equal to 'y' or 'Y'?

    If so, the problem is that you have code that is roughly the following:
    Code:
    scanf("%d", &whatever);
    repeat = getchar();
    while ( getchar() != '\n');
    So the user enters a number, and hits enter. The next call to getchar() thus returns a newline (because the user hit enter), which is assigned to repeat. Your loop then discards all user input until he hits enter again. Unless the user is doing something like entering "11y" for an answer, repeat will always contain a newline, and thus will never be 'y' or 'Y'.

    User input in C is an annoying thing. For a quick fix you can replace the second two lines in the excerpt above with:
    Code:
    while( (repeat = getchar()) == '\n' );
    This will keep reading characters until the user enters a non-newline character. repeat will contain the last character the user entered.

    You'll also want to make repeat an int, not a char. Despite its name, getchar() returns an int, and for good reason: otherwise, you would not be able to reliably detect EOF—something you'll want to do, at least at some point in your future C endeavors.
    Thank you, that was the problem, I didn't know that getchars() were affected by scanfs done before (because of the ENTER).
    Good to know!

  7. #7
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by Brain_Child View Post
    this works.

    ive bolded the changes

    despite its name, getchar works with integers, not chars. I think this program will still work if you have char repeat, but it should be an integer. I believe the reason relates to EOF.

    i changed the do while to a while loop.

    Code:
    int addition(void) {
    	int first, second, answerout, answerin;
    	int repeat = 'y';
    	printf("Addition:\n");
       while(repeat == 'Y' || repeat == 'y') {
    	first = 1 + rand() % 10;
    	second = 1 + rand() % 10;
    	answerout = first + second;
    	printf("\n");
    	printf("   What is %d + %d = ? ", first, second);
    	scanf("%d", &answerin);
    	printf("\n");
    	while ( answerin != answerout){
    		printf("No. Try Again. What is %d + %d = ? ", first, second);
    		scanf("%d", &answerin);
    		printf("\n");
    	}
    	printf("Correct!\n");
    	printf("\n");
    	printf("Play Again? ");
    	repeat = getchar(); // this eats up the '\n' leftover from the scanf()
    	repeat = getchar(); // this gets their decision, y or n
    	}
    
    	return EXIT_SUCCESS;
    }
    you might also like to include

    Code:
    #include <time.h>
    ....
    srand(time(NULL)); // anywhere before the rand();
    which will give you random rumbers
    Great observation, thank you for everything!

    Have a nice day and happy programming!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21