C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-09-2009, 01:02 AM   #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.
georgio777 is offline   Reply With Quote
Old 11-09-2009, 01:21 AM   #2
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
What does “doesn't work” mean in this context?
cas is offline   Reply With Quote
Old 11-09-2009, 01:27 AM   #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.
georgio777 is offline   Reply With Quote
Old 11-09-2009, 01:46 AM   #4
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
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.
cas is offline   Reply With Quote
Old 11-09-2009, 01:50 AM   #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
Brain_Child is offline   Reply With Quote
Old 11-09-2009, 01:55 AM   #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!
georgio777 is offline   Reply With Quote
Old 11-09-2009, 01:58 AM   #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!
georgio777 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Laptop Problem Boomba Tech Board 1 03-07-2006 06:24 PM
getchar() problem jlharrison C Programming 6 01-25-2006 02:49 PM
problem with parser code ssharish2005 C Programming 2 12-02-2005 07:38 AM
Sorting problem.. well actually more of a string problem fatdunky C Programming 5 11-07-2005 11:34 PM


All times are GMT -6. The time now is 06:08 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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