usage of do while loop for yes no prompt in c
usage of do while loop for yes no prompt in c
I am learning to code in c online and was trying this program to use the do while loop
This is a program for a dice game
A player can enter the game with minimum amount of 1000
.
A player can take any number of chances. If the value of the dice
is 1
, the player leaves the game with half the amount.
If the value is other than 1
, the total amount is raised by multiple of 100*value
of dice
.
If the player quits, he/she leaves the game with amount he/she has got.
#include #include int main () { int cash, dice; char ch; printf ("cash\n"); scanf ("%d", &cash); if (cash < 1000) { printf ("No\n"); exit (0); } else { do { printf ("dice\n"); scanf ("%d", &dice); while (dice < 1 || dice > 6) { printf ("\n invalid"); printf ("dice\n"); scanf ("%d", &dice); } if (dice == 1) { cash = cash / 2; break; } cash = (dice * 100) + cash; printf ("Do you want to continue"); scanf ("%c", &ch); } while (ch == 'y'); } printf ("won=%d", cash); return 0; }
This program isn't accepting the y
or n
input. It displays the statement do you want to continue
and directly goes to won statement.
Answer by Black Bird for usage of do while loop for yes no prompt in c
You have to use a space before %c
scanf(" %c", &ch);
Answer by sameera sy for usage of do while loop for yes no prompt in c
changing your scanf will solve all the problems
scanf(" %c",&ch); //notice space
Answer by LPs for usage of do while loop for yes no prompt in c
Your second scanf
is consuming the '\n'
char left into stdin
by the dice
one
Simply adding a space before format specifier you can consume all chars in stdin before accempting the character inserted by user:
scanf (" %c", &ch);
In a scanf()
format, a blank, tab or newline means 'skip white space if there is any to skip'.
Answer by where_is_tftp for usage of do while loop for yes no prompt in c
printf ("dice\n"); scanf ("%d", &dice);
This has left '\n' in the buffer and your second call to scanf reads this '\n' instead of 'n' and continues. You must read remaining '\n' before reading character.
man scanf(3):
The format string consists of a sequence of directives which describe how to process the sequence of input characters. If processing of a directive fails, no further input is read, and scanf() returns. A "failure" can be either of the following: input failure, meaning that input characters were unavailable, or match? ing failure, meaning that the input was inappropriate (see below). A directive is one of the following: ? A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space,
There are two options.
scanf (" %c", &ch);
or
(void) getchar (); scanf ("%c", &ch);
Answer by David C. Rankin for usage of do while loop for yes no prompt in c
The biggest problem you have is not accounting for the '\n'
left in your input buffer (stdin
here) that results from the user pressing Enter after entering dice
as the %c
format specifier will happily take the '\n'
as the user-input for ch
.
The next problem is ch
should be and int
not char
or you will never be able to test/trap EOF
.
Further, you fail to validate the return
of scanf
to confirm any of the conversions are valid.
Given the problems associated with taking user input with scanf
, you are better served taking user input with fgets
and reading the entire line of user input into a buffer of sufficient size for the input and then parsing your numeric values from the buffer with sscanf
(or simply subtracting '0'
for a single digit).
Putting those pieces together, you could do something like the following:
#include #include #define BUFLEN 128 int main () { int ch, cash, dice; char buf[BUFLEN] = ""; printf ("cash: "); if (!fgets (buf, BUFLEN, stdin)) { /* read/validate cash */ fprintf (stderr, "error: invalid input - cash.\n"); return 1; } if (sscanf (buf, "%d", &cash) != 1) { /* parse cash from buf */ fprintf (stderr, "error: invalid conversion - cash.\n"); return 1; } if (cash < 1000) { printf ("No\n"); return 1; } do { ch = 'n'; /* set/reset ch to exit each iteration */ printf ("dice: "); if (!fgets (buf, BUFLEN, stdin)) { /* read/validate dice */ fprintf (stderr, "error: invalid input - dice.\n"); break; /* break on EOF */ } /* parse/validate dice */ if (sscanf (buf, "%d", &dice) != 1 || dice < 1 || dice > 6) { fprintf (stderr, "error: invalid conversion - dice.\n"); ch = 'y'; /* set ch to 'y' */ continue; /* prompt again, etc */ } if (dice == 1) { cash = cash / 2; /* note: truncated division */ break; } cash = (dice * 100) + cash; printf ("\nDo you want to continue? (y/n): "); if (fgets (buf, BUFLEN, stdin)) ch = *buf; /* simply assign first char */ else break; /* exit loop on EOF */ } while (ch == 'y'); printf ("\nwon = %d\n\n", cash); return 0; }
(note: tests to validate less than 128 chars entered for each input omitted)
Example Use/Input
$ ./bin/rfmt cash: 1200 dice: 7 invalid dice. dice: 5 Do you want to continue? (y/n): y dice: 2 Do you want to continue? (y/n): y dice: 6 Do you want to continue? (y/n): n won = 2500
Look over all the answer and decide whether you want to go with scanf
or fgets/sscanf
. Both are doable, you just lose a bit of flexibility when you couple both your read and parse in a single scanf
call. Let me know if you have any questions.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment