0

This was originally from another program, but this segment will not work the way I need it to and I imagine others may be having trouble as well. Also of note that after the user input is accepted it is used in a while loop.

printf("would you like to check another time?(y/n)?");
fflush(stdin);
scanf("% c", &yesno);
while(yesno != 'y' && yesno != 'n')
{
   printf("That was not a valid entry, please re entery your choice.");
   fflush(stdin);
   scanf("% c", &yesno);
}/*End of verification loop*/

I want the user to input a character, and after verifying it is either a y or an n, have it go to a while loop where if the character is a y it will continue the program, and if not it will end it.

cf-
  • 8,598
  • 9
  • 36
  • 58
user3259144
  • 71
  • 1
  • 9
  • 2
    what is the way you want it work? – Iłya Bursov Apr 02 '14 at 04:13
  • I want the user to input a character, and after verifying it is either a y or an n, have it go to a while loop where if the character is a y it will continue the program, and if not it will end it. – user3259144 Apr 02 '14 at 04:20
  • [clicky clicky](http://en.wikibooks.org/wiki/C_Programming/Simple_input_and_output) – motoku Apr 02 '14 at 04:26
  • @user3259144 Generally you want to put such information in your original question where people can more easily find it. For future reference, when you ask a question, please explain what you *want* your code to do and what it's *actually* doing; this will help people understand what the problem could be and how best to address it. – cf- Apr 02 '14 at 04:26
  • 1
    @user3259144 You can exit your program in by putting condition `if(yesno == 'n')` then `return` after your `scanf("% c", &yesno);` statement in `while..loop`.Or if input is `y` then automaticaly your `while..loop` break and continue your programme. – Jayesh Bhoi Apr 02 '14 at 04:29
  • 1
    You probably want `" %c"` (space before the `%`, not after). That skips white space, including newlines, before reading a non-white-space character. – Jonathan Leffler Apr 02 '14 at 04:38
  • I added that, but now it keeps exiting regardless of user input input. – user3259144 Apr 02 '14 at 04:38
  • 1
    `scanf("% c", &yesno);` ... `% c` should be `%c` without space – Tun Zarni Kyaw Apr 02 '14 at 04:38
  • There is a [SO post on why you should not call `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin/2979217#2979217). – R Sahu Apr 02 '14 at 05:42

2 Answers2

2
    printf("would you like to check another time?(y/n)?\n");
    fflush(stdin);
    scanf("%c", &yesno);
    while(yesno != 'n' && yesno != 'y')
    {
       printf("That was not a valid entry, please re-enter your choice.\n");
       fflush(stdin);
       scanf("%c", &yesno);

    }
    if (yesno == 'n') return 0; // program terminated here

// else it is automatically 'y' so your program continues here ...

additional

I just noticed another crucial failure that is affecting your snippet (and i imagine the next lines of codes also )

scanf("% c", &yesno); // will not read input
scanf("%c", &yesno); // will read input, there is no space between % and c, it is %c not % c
chouaib
  • 2,763
  • 5
  • 20
  • 35
1

Please note that fflush is defined only for output streams. Calling fflush on stdin is invokes undefined behaviour. You can use getchar function to read and discard extraneous input from the stdin.

printf("would you like to check another time?(y/n)?");

// read and discard any number of leading whitespace characters
scanf("% c", &yesno); 
char ch;
while(yesno != 'y' && yesno != 'n') {
    // stdout is line buffered. Outputting a newline will immediately flush
    // the output to the console
    printf("That was not a valid entry, please reenter your choice.\n");

    // if the input character is not a newline, then read and discard
    // all character up till and including the newline 
    if(yesno != '\n')
        while((ch = getchar()) != '\n');  // note the null statement

    // read the input from the user afresh
    scanf("% c", &yesno);
}
ajay
  • 9,402
  • 8
  • 44
  • 71