Home > Blockchain >  Char input does not continue my program as it should?
Char input does not continue my program as it should?

Time:10-16

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();         
}

CodePudding user response:

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');

}

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related