1

I am trying to make the program print out "OK" for as long as i enter 'y' as a choice at the end, but it is not looping, it just prints out "OK" and ends the program even if i enter a 'y' at the end. Please help.

#include <stdio.h>
int main()
{
    char c = 'y';
    while (c == 'y')
    {
        printf_s("OK\n");
        scanf_s("%c", &c);
        if (c != 'y')
        {
            break;
        }
    }
    return 0;
}
3
  • 2
    you are in windows system use fflush(stdin) before scanf Commented Feb 6, 2014 at 14:57
  • 1
    You might want to check a scanf_s reference. Commented Feb 6, 2014 at 14:59
  • OK the fflush(stdin) helped when i wanted to keep the loop running Commented Feb 6, 2014 at 15:01

5 Answers 5

4

On first iteration when you press Enter key then a newline character \n is passed to the input along with y. On second iteration scanf_s reads \n. Change

scanf_s("%c", &c);  

to

scanf_s(" %c", &c);  
         ^Notice the space before %c  

A space before %c specifier can consume any number of white-space characters.

Sign up to request clarification or add additional context in comments.

1 Comment

The scanf_s method requires that all storage locations be followed with an explicit size.
2

Change the scanf_s line as follows

scanf_s("%c", &c, 1);

This extra parameter is specifying the size of the c argument. Plain old scanf doesn't require this argument but the versions ending with _s do

Also the if block with the break statement is unnecessary because the conditional on the while loop effectively does the same thing. It could be written as follows

while (c == 'y')
{
    printf_s("OK\n");
    scanf_s("%c", &c, 1);
}

Comments

1

OK I used scanf instead of scanf_s, that solved the problem, thanks everyone.

1 Comment

if you want to continue using scanf_s see my answer as it corrects the usage of htis API
0

You are wrong with scanf

if you use like this u will see it is working ..

       #include <stdio.h>

int main()
{
    int c = 1;
    while (c == 1)
    {
        printf_s("OK\n");
        scanf_s("%d", &c);
        if (c != 1)
        {
            printf_s("hello\n");
                continue;
        }

    }
    return 0;
}

4 Comments

He is not "wrong" with scanf. You can scan for a character.
He was using scanf_s to scan for a character. Changing it to an int does not help him solve the issue with his scanf_s not having the necessary third parameter. He is not "You are wrong with scanf "
I said that he is using it wrongly if he get with %c compilers reads new line as a char too. That's why he is using wrongly!
Then add more explanation to your answer instead of simply stating "wrong".
-1

Put a space before %c to skip whitespace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.