Char input does not continue my program as it should? [duplicate] - c

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 months ago.
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();
}

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

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.

Related

what is the difference between char and int used in scanf function in a loop? [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 11 months ago.
when I write a for loop to scanf some int value, the output is correct.
however, I try to this loop to scanf some char value, it seems something wrong.(I was wondering the space or '\t' as a char to be scanned)
//when the input are
2
1 2
3 4
//
#include<stdio.h>
int main(void){
int n;
scanf("%d", &n);
int x;
int y;
for(int i = 0; i< n; i++){
scanf("%d %d", &x, &y);
}
return 0;
}
//when I enter
2
a b
the program is finished and i cannot enter more char.
//
#include<stdio.h>
int main(void){
int n;
scanf("%d", &n);
char x;
char y;
for(int i = 0; i< n; i++){
scanf("%c %c", &x, &y);
}
return 0;
}
Remember, the space (' ') is also a char, so the scanf is reading the space instead of the second letter.

Trouble with a C program

I had a homework. The condition was: "Make a program in witch will be displayed how many 'A' and 'B' where typed". I tried and this is what i made:
#include <stdio.h>
int main()
{
int n, i, a=0, b=0;
char cr;
scanf ("%i", &n);
for (i=0; i<=n; i++) {scanf ("%c", &cr); if (cr='A') a++; else if (cr=='B') b++;}
printf ("A-%i\n", a);
printf ("B-%i", b);
}
The problem is when i type for example 10, it only lets me type 5 characters, not 10. Why?
The program can be made with for, while or do while.
Problems and solutions
you need to use scanf(" %c", %cr) to prevent the program from reading trailing whitespaces
You should be using if (cr == 'A') and not if (cr = 'A') because you are comparing and not assigning
the loop should be for (i = 0; i < n... not for (i = 0; i <= n.. as the loop would ask for 1+ input from the specified range
#include <stdio.h>
int main() {
int n, a = 0, b = 0;
char cr;
printf("Enter number of tries :\n");
scanf ("%i", &n);
for (int i = 0; i < n; i++) {
printf("Enter char\n");
scanf(" %c", &cr);
if (cr == 'A') a++;
else if (cr == 'B') b++;
}
printf ("A-%i\n", a);
printf ("B-%i", b);
}

Do/While not recognizing the character to end loop in C

I'm trying to write some characters in this "arq.txt" file and it works well at the beginning, but when I type zero to restart the program, the Do / While loop no longer recognizes zero I can't get out of it anymore. Please help me! Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *p;
char ch;
int c = 1, i, cont = 0;
while(c) {
printf("\n-------- DIGITANDO VALORES --------\n");
p = fopen("arq.txt", "w");
do {
printf("Type: ");
scanf("%c", &ch);
getchar();
putc(ch, p);
cont++;
} while(ch != '0');
fclose(p);
p = fopen("arq.txt", "r");
for(i = 0; i <= cont; i++) {
ch = fgetc(p);
printf("%c\n", ch);
}
fclose(p);
printf("If you wish to finish program, please type zero: ");
scanf("%d", &c);
}
}
scanf("%c", &ch); reads left-over newline from scanf("%d", &c);.
Try scanf("%c", &ch); getchar(); --> scanf(" %c", &ch);. Note space

Why is this reverse string program not working?

I am new to C programming so please do forgive my naivety. The following program when outputted fails to print the last character of the input string as the first character of the output string.
For example:
Enter no. of elements: 5
Enter string: hello
The reversed string is: lleh
Why is the o not printing?
#include <stdio.h>
int main() {
printf("Enter no. of elements: ");
int n;
scanf("%d", &n);
char string[10000];
printf("Enter string: ");
for (int i = 0; i < n; i++) {
scanf("%c", &string[i]);
}
printf("The reversed string is: ");
for (int i = (n - 1); i >= 0; i--) {
printf("%c", string[i]);
}
printf("\n");
return 0;
}
There is a side effect you take care of:
After scanf("%d", &n);, there is a pending newline in the input stream buffer.
When you later input n characters, scanf("%c", &string[i]) first reads the pending newline, then the n-1 first characters you type and the remainder of your input stays in the input buffer.
scanf() is a very clunky function. It is difficult to use properly.
Here is a way to fix your problem:
#include <stdio.h>
int main() {
char string[10000];
int i, n, c;
printf("Enter no. of elements: ");
if (scanf("%d", &n) != 1 || n < 0 || n > 10000)
return 1;
// read and discard pending input
while ((c = getchar()) != '\n' && c != EOF)
continue;
printf("Enter string: ");
for (i = 0; i < n; i++) {
if (scanf("%c", &string[i]) != 1)
break;
}
// the above loop could be replaced with a single call to fread:
// i = fread(string, 1, n, stdin);
printf("The reversed string is: ");
while (i-- > 0) {
printf("%c", string[i]);
}
printf("\n");
return 0;
}
Your scanf() should start with a space( more info about that ). Here is the code:
#include <stdio.h>
int main() {
printf("Enter no. of elements: ");
int n;
scanf(" %d", &n);
char string[10000];
printf("Enter string: ");
for (int i = 0; i < n; i++) {
scanf(" %c", &string[i]);
}
/* Just to be safer. */
string[n] = '\0';
printf("The reversed string is: ");
for (int i = (n-1); i >= 0; i--) {
printf("%c", string[i]);
}
printf("\n");
return 0;
}
Adding the space to the format string enables scanf to consume the
newline character from the input that happens everytime you press
return. Without the space, string[i] will receive the
char '\n'
So, merely one space is put before format specifier %c at line 11.
scanf(" %c", &string[i]);

My string compare is coming out wrong

hi my program is to enter a number which gives the length of the string then the string and then finally a letter which should then tell me how many times that letter is in the string. Currently to help me figure out what is wrong with my code i can see that the strcmp is resulting in the same ascii number but negative. eg for the letter a the number is 97 but the strcmp is giving out -97 so the strcmo doesnt show the character as being in the string and results in the incorrect result. Any help would be greatly appreciated. thanks
#include
#include
int main(void)
{
char myChar[100], z, k;
int counter, n, g=0, r, i, l;
counter=0;
scanf("%d",&n);
while (counter<n)
{
counter++;
scanf(" %c",&myChar[counter]);
}
scanf(" %s", &z);
for(i=0;i<n+1;++i)
{
k=myChar[i];
r=strcmp(&z, &k);
l=r;
//printf("\n%c", myChar[i]);
printf("%d\n", l);
if(r==0)
{
g++;
printf("%d\n", g);
}
}
printf("\n\n%d\n", g);
return (0);
}
#include <stdio.h>
#include <string.h>
int main(void){
char myChar[100], str[64];//
int counter, n;
int i, j, k;
printf("number of charactors : ");
scanf("%d",&n);
for (i=0; i<n; ++i){
scanf(" %c", &myChar[i]);
}
printf("input string : ");
scanf("%63s", str);//"%s" : The useless one letter to input as a string. "%c" for &z
for(i=0;i<n;++i){
counter = 0;
for(j=0;str[j]!='\0'; ++j){
k = (str[j] == myChar[i]);//Comparison of each character
counter += k;//if(str[j] == myChar[i]) ++counter;
}
printf("%c is %d\n", myChar[i], counter);
}
return (0);
}

Resources