![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 45
| clearBuffer() 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 | |
| | #2 |
| Registered User Join Date: Oct 2009
Posts: 45
| i tried fflush(stdin); but no luck |
| fcommisso is offline | |
| | #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 | |
| | #5 |
| +++ OK NO CARRIER 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 | |
| | #6 |
| Robot Join Date: Mar 2009
Posts: 372
| EDIT: Meh. |
| Memloop is offline | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #10 |
| I like turtles Join Date: Sep 2009 Location: Ohio
Posts: 408
| money = money - bet; ==> money -= bet; fgetc(stdin) ==> getchar() |
| Epy is offline | |
| | #11 |
| Registered User 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... |
| jeffcobb is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|