C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-14-2009, 05:06 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 45
clearBuffer()

so I need to write a function clearBuffer() to clear the buffer after scanf...
anyone can give me any help??
I have no clue how to do this...i just know that i get a 'bus error' otherwise..


Code:
void getBet(int* currentBet) {

	int bet = 0;
	for(printf("Enter Bet:\n"); 1; printSeparator()) {
		scanf("%d", &bet);
		// clearBuffer();
		if ( bet <= money && (bet % 5) == 0 && bet > 0 && bet <= 500 ) {
			break;
		}
		printSeparator();
		printf("Invalid BET, try again:\n");
	}
	money = money - bet;
	*currentBet = bet;
}
fcommisso is offline   Reply With Quote
Old 12-14-2009, 05:16 PM   #2
Registered User
 
Join Date: Oct 2009
Posts: 45
i tried fflush(stdin);
but no luck
fcommisso is offline   Reply With Quote
Old 12-14-2009, 05:17 PM   #3
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
fflush(stdin)?
__________________
C/C++ Environment: GNU CC/Emacs
Make system: CMake
Debuggers: Valgrind/GDB
jeffcobb is offline   Reply With Quote
Old 12-14-2009, 05:19 PM   #4
Registered User
 
Join Date: Oct 2009
Posts: 45
nope.... unless i'm using it at the wrong time???

Code:
void getBet(int* currentBet) {

	int bet = 0;
	for(printf("Enter Bet:\n"); 1; printSeparator()) {
		scanf("%d", &bet);
		fflush(stdin);
		if ( bet <= money && (bet % 5) == 0 && bet > 0 && bet <= 500 ) {
			break;
		}
		printSeparator();
		printf("Invalid BET, try again:\n");
	}
	money = money - bet;
	*currentBet = bet;
}
fcommisso is offline   Reply With Quote
Old 12-14-2009, 05:33 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 11,292
Read the FAQ.

Cprogramming.com FAQ > Why fflush(stdin) is wrong
Cprogramming.com FAQ > Flush the input buffer

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 12-14-2009, 05:34 PM   #6
Robot
 
Join Date: Mar 2009
Posts: 372
EDIT: Meh.
Memloop is offline   Reply With Quote
Old 12-14-2009, 05:41 PM   #7
Registered User
 
Join Date: Oct 2009
Posts: 45
Oh great links quzah! Thank you. I really didn't get how to do such a thing... shame that it isn't part of a library, uh?
fcommisso is offline   Reply With Quote
Old 12-14-2009, 05:48 PM   #8
Registered User
 
Join Date: Oct 2009
Posts: 45
ok.. sorry, but I still need more information. How can I implement it to my code?


Code:
void getBet(int* currentBet) {

	int bet = 0;
	for(printf("Enter Bet:\n"); 1; printSeparator()) {
		scanf("%d", &bet);
               if (fgets(buf, sizeof(buf), stdin)) {
  
		     fflush(stdin);
		     if ( bet <= money && (bet % 5) == 0 && bet > 0 && bet <= 500 ) {
			   break;
		     }
	      }
              printSeparator();
              printf("Invalid BET, try again:\n");
	}
	money = money - bet;
	*currentBet = bet;
}

???
fcommisso is offline   Reply With Quote
Old 12-14-2009, 05:58 PM   #9
Registered User
 
Join Date: Oct 2009
Posts: 45
ok. that ws horrible code. ignore that.

what will this do?

Code:
void fflushstdin( void )
{
    int c;
    while( (c = fgetc( stdin )) != EOF && c != '\n' );
}
fcommisso is offline   Reply With Quote
Old 12-14-2009, 06:09 PM   #10
Epy
I like turtles
 
Join Date: Sep 2009
Location: Ohio
Posts: 408
money = money - bet; ==> money -= bet;

fgetc(stdin) ==> getchar()
__________________
-Jake
http://thatcadguy.blogspot.com/
Epy is offline   Reply With Quote
Old 12-14-2009, 06:28 PM   #11
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
I stand corrected. I don't do alot of non-file stream IO ^__^

Tks for the input. The solution does seem...inelegant though...it is exactly what the FAQ suggests I know but still looks like a hack...
__________________
C/C++ Environment: GNU CC/Emacs
Make system: CMake
Debuggers: Valgrind/GDB
jeffcobb is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 12:14 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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