-1

I wrote this table writing program in which I wanted to give program ability to continue depending upon char input value but after taking input, Even if input is y the loop still doesnt execute and program moves towards next line

#include <stdio.h>
#include <conio.h>

int main()    
{
    int T, N, P;
    int K = 1;
    char ch;
      
    do
    {       
    printf ("\nWhich Number's Table do you want?");
    scanf ("%d", &T); 
    printf ("\nTable should be Uptil?");
    scanf ("%d", &N);
 
    do
    {
      P= T * K;
      printf("\n %dx%d = %d", T, K, P);
      K= K + 1;
    } while(K <= N);
     
     printf("\nDo you want to continue (Y/N)?");
        scanf("%c ", &ch);

    } while (ch == 'y'); 

    getch();         
}

Harith
  • 4,663
  • 1
  • 5
  • 20
  • 2
    Change `scanf("%c ", &ch);` to `scanf(" %c", &ch);` (Space before, not after, %c.) `scanf()` is not friendly. You should move on to using big-boys' toys like `fgets()` – Fe2O3 Oct 16 '22 at 07:05
  • 1
    ⟼Remember, it's always important, *especially* when learning and asking questions on Stack Overflow, to keep your code as organized as possible. [Consistent indentation](https://en.wikipedia.org/wiki/Indentation_style) helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what's going on. – tadman Oct 16 '22 at 07:11

2 Answers2

0

you needgetchar() to skip Enter(\n)

#include <stdio.h>

int main() {
    int T, N, P;
    int K = 1;
    char ch;
    do {
        printf("\nWhich Number's Table do you want?");
        scanf("%d", &T);
        getchar();//================here========
        printf("\nTable should be Uptil?");
        scanf("%d", &N);
        getchar();//================here========

        do {
            P = T * K;
            printf("\n %dx%d=%d", T, K, P);
            K = K + 1;
        } while (K <= N);

        printf("\nDo you want to continue (Y/N)?");
        scanf("%c", &ch);
        getchar();//================here========

    } while (ch == 'y');

}
zhj
  • 478
  • 3
  • 16
-1

Just changing scanf("%c ", &ch); to scanf(" %c", &ch); fixed my problem. I guess the character doesnt get read properly just due to the intendation difference.

  • The issue is that you hit enter after entering the previous number and you have no code to read that enter character. So if your `scanf` format string starts with `%c`, you'll read the enter character and store it. – David Schwartz Oct 16 '22 at 07:24