1

I can't see why does the While loop just speed away and skipping the scanf for the char? It would not even ask for my input and just loop like there's no tomorrow.

#include <stdio.h>


int main()
{
    int number;
    int multiply, ans;
    char choice;

    printf("-------------------------------------");
    printf("\n      MULTIPLICATION TABLE           ");
    printf("\n-------------------------------------");


    do
    {

         printf("\nEnter an integer number:");
         scanf("%d", &number);


        printf("\nMultiplication of %d is :-\n", number);
        printf("\n");

        for(multiply=1; multiply<11; multiply++){
            ans = number * multiply;
            printf(" %d", ans);
        }

        printf("\n");
        printf("\nWould you like to continue? [Y] for Yes,[N] for no : ");
        scanf("%c", &choice);
        printf("\n");

    } 
    while(choice='Y');

    printf("Thank You");
    return 0;

}

3 Answers 3

2

scanf() doesn't do what you think it does (newline characters, buffering, etc.). It's preferred to use fgetc():

choice = fgetc(stdin);

For the same reason, you need to get rid of the trailing newline that

scanf("%d", &number");

leaves in the standard input buffer. To fix this, insert

fgetc(stdin);

after that particular call to scanf().

Also, C is not Pascal. The equality comparison operator - and the condition - you're looking for is

while (choice == 'Y')

the single equation mark denotes an assignment.

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

4 Comments

@DCoder And fixing that doesn't fix the erroneous assumptions about scanf().
Changed it but still skipping the input part.
@user1924648 what kind of insane input data are you using? Tried inserting another call to fgetc() after scanf("%d", &number);?
@user1924648 Yep, it does the trick, I just tried it as well.
2

It has been a long time since I programmed in that language, but at a glance, you have:

while(choice='Y');

instead of:

while(choice=='Y');

== compares, = sets equal to. So the while loop is actually not checking the condition you're trying to set.

1 Comment

"I've fix that part but it still won't wait for me to put an input. – user1924648 1 min ago"
1

I think you need to use == operator for the comparison in while condition check as:

   while(choice=='Y');

Currently you are using = operator, which is assigning Y to choice variable.

3 Comments

I've fix that part but it still won't wait for me to put an input.
@user1924648 Use fgetc(), really.
Used fgetc() and now it's going out of the loop without waiting for me to enter anything. It's working now, I just inserted an additional fgetc after getting the int.

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.