why isn't the user input printing in c? [duplicate] - c

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
How to do scanf for single char in C [duplicate]
(11 answers)
abnormal behavior of scanf [duplicate]
(3 answers)
Closed 4 months ago.
int userinput;
char convertfrom;
float value;
printf("What do you want to convert? Enter number from 1 to 5: ");
scanf("%d", &userinput);
if (userinput == 1) {
printf("enter letter K or P: ");
scanf("%c\n", &convertfrom);
printf("letter = %c", convertfrom);
}
I input "P" for the second scanf that asks for a letter, but the code keeps failing to print "letter = P". instead it only prints "letter = ".
Why can't I print the user input?

Related

Program not reading fgets at all [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Using scanf and fgets in the same program?
(4 answers)
Closed 5 months ago.
#include <stdio.h>
int main(void) {
int numWords;
char str[100];
printf("Enter how many words:\n");
scanf("%d", &numWords);
printf("Enter %d words:", numWords);
fgets(str, 100, stdin);
return(0);
}
This program does not read fgets at all. It will print the text and scan what what the user enters into the numWords, but will not read scanf. I have tried putting a newline character after scanf but then the "Enter %d words:" will not print. I would like everything to print in order.
This is just the beginning of a more complex program, not the whole thing.

Can we take different data type inputs in different consecutive lines in C [duplicate]

This question already has answers here:
Program doesn't wait for user input with scanf("%c",&yn);
(5 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
int main()
int c,s;
char h;
printf("Enter the carbon\n");
scanf("%d",&c);
printf("Enter the hard\n");
scanf("%c",&h);
printf("Enter strength\n");
scanf("%d",&s);
printf("%d%d",c,s);
printf("%c",h);
}
Output:
Enter the carbon
4
Enter the hard
Enter strength
My program is not reading character data types. Can anyone tell me the reason?
The compiler is not showing any error.

Why character value is not taking after integer value in c [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
So this is my c code
#include <stdio.h>
void main()
{
int a;
char b;
printf("Enter input \n");
scanf("%d", &a);
printf("Enter char input \n");
scanf("%c", &b);
printf("%d,%c", a, b);
}
And I'm getting output as
Enter input
3
Enter char input
3,
Can you explain me why its not taking character value the second time. This program works well if i take charcter input first.
you are entering space or enter button after integer input, So b is assigned that space/enter.
add getchar(); before input b

Reading 2 characters in C [duplicate]

This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 5 years ago.
#include<stdio.h>
int main()
{
char a,b;
printf("Enter Character 1 ");
scanf("%c",&a);
printf("Enter Character 2");
scanf("%c",&b);
printf("%c%c",a,b);
}
find output of program. It is not taking 2 characters.
When you enter the first character;
The character Ascii value is placed in the variable A .
Then you press enter and the ascii value of enter thats 13 is placed in the second variable.
So B should be 13.
In order to run this program use flushall() library function..
int main() {
char a,b;
printf("Enter Character 1 ");
scanf("%c",&a);
flushall();
printf("Enter Character 2");
scanf("%c",&b);
printf("%c%c",a,b);
return 0;
}
You could also use the standard Library function scanf()
Write something like:
Scanf(" %c",&variablename);
The space before the %c tells the compiler to skip any whitespace (if any).

Accepting only integer in C [duplicate]

This question already has answers here:
scanf format specifier to only read a number
(2 answers)
Closed 7 years ago.
How do you propose I write my code to accept only integers in C and if a letter is entered, prompt to enter only integers? And Vice versa.
printf("Enter integer");
scanf("%d", &a);
while(scanf("%d", &a)==0) //if scanf failed to scan an integer
{
printf("Invalid input. Try again\n");
int c;
while((c=getchar())!='\n' && c!=EOF); //clear the stdin
}

Resources