Don't getchar function print the second input? - c

#include <stdio.h>
int main()
{
char gh, hj;
printf("Enter the first letter of your name \n");
gh=getchar();
printf("Enter the last letter of your name\n");
hj=getchar();
getchar();
printf("The first and last leter of your name is %c%c", gh, hj);
return 0;
}
When I run this code the second %c doesn't get printed, unlike the first one. Why is this happening?

Since the second getchar peruses the newline character after the character you entered.

Related

C program, getting the integer value of a character. Why everything comes out after line 10?

Hi I am new to programming and I got a trouble when I try to make a little change to the example in the book.
/* Chapter 3 Example, C Prime Plus */
#include <stdio.h>
int main(void)
{
char Letter, ch;
int intValue;
printf("Please enter a letter: \n");
scanf("%c", &Letter); /* user inputs character */
printf("The code for %c is %d.\n", Letter, Letter);
printf("Now is another we to implement the process: \n");
printf("RN, the value of ch is %c, and the value of intValue is %d\n", ch, intValue);
printf("Please enter a letter: \n");
scanf("%c", &ch);
intValue = ch;
printf("The code for %c is %d.\n", ch, intValue);
return 0;
}
When I run it, the outcome would be
Please enter a letter:
M
The code for M is 77.
Now is another we to implement the process:
RN, the value of ch is , and the value of intValue is 0
Please enter a letter:
The code for
is 10.
and the part
"
Now is another we to implement the process:
RN, the value of ch is , and the value of intValue is 0
Please enter a letter:
The code for
is 10. " will all come out without asking me to enter a value.
I want to know why and are there any other way to implement it that is different from examples in the book?
Thank you for your time!
Hi Matt_C and welcome to SO.
First, you don't need the second bloc of printfs and the scanf, it just trying to do the same thing and there is an order error.
Second, it is tricky when you try consecutive scanf, it holds the last key pressed (enter is the last key pressed = \n). This is why it skips the second scanf.
There a little solution for that, add a space at the beginning of the scanfs. Try this:
int main() {
char exit, letter;
while (1) {
printf("Please enter a letter: ");
scanf(" %c", &letter);
printf("\nThe code for '%c' is %d. \n\n", letter, letter);
printf("Exit ? (y/n): ");
scanf(" %c", &exit);
if(exit == 'y')
{
break;
}
system("clear"); // UNIX
//system("cls"); // DOS
}
}
Don't forget to choose one answer that you believe is the best solution to your problem.

Why c programme code is not asking the second value & how come char is stored in int form I took this from an example on web

#include <stdio.h>
// source tutorial points
int main(){
int c;
char d;
printf("Enter First value \n");
c = getchar();
printf("Enter Second value \n");
d = getchar();
printf("You have entered first \n");
putchar(c);
printf("You have entered second \n");
putchar(d);
return(0);
}
when I am entering first value it is not asking for other value please help I do not expect a character not possible in char.
Because each time you input a character and hit ENTER. So, second getchar in your code reads the enter character.
Your code should change to:
c = getchar();
getchar(); // for consuming the enter character
printf("Enter Second value \n");
d = getchar();
getchar(); // for consuming the enter character
The output:
Enter First value
a
Enter Second value
b
You have entered first
a
You have entered second
b

Why isn't the Isdigit function working properly?

I wrote this short code to test my understanding of the isdigit function:
int inChar;
printf("enter input:");
scanf(" %d", &inChar);
if (isdigit(inChar))
printf("Your input was a number");
else
printf("Your input was not a number.\n");
When I test this program and I enter a number, C returns the else statement (Your input was not a number.). So regardless of if I enter a number or a letter, the program returns the else statement.
Why is this so?
isdigit() checks if a single character that was passed to it by converting the char value an unsigned char.
So, you can't directly pass any int value and expect it to work.
Man isdigit() says:
isdigit()
checks for a digit (0 through 9).
To check single digit, you can modify:
char inChar;
printf("enter input:");
scanf(" %c", &inChar);
if (isdigit((unsigned char)inChar)) {
printf("Your input was a number");
}
else {
printf("Your input was not a number.\n");
}
If you have an array (a string containing a number) then you can use a loop.
The function's purpose is to classify characters (like '3'). Running it on something that's read using %d doesn't make sense.
You should read a single char using %c. Remember to check that reading succeeded.
The C library function void isdigit(int c) checks if the passed character is a decimal digit character.
If you badly wanna try it with an int you can init in this way
int inChar = '2';
The following code gave expected results.
int main()
{
char inChar;
printf("enter input:");
scanf(" %c", &inChar);
if (isdigit(inChar))
printf("Your input was a number. \n");
else
printf("Your input was not a number.\n");
return 0;
}
Output:
vinay-1> ./a.out
enter input:1
Your input was a number

Why procedure if in C doesn't work with char

I am writing a simple quiz in C (using CodeBlocks 13.12)
It compiles, but doesn't work in second question. Whatever I will input, it always give answer 'that's sad'.
I can't understand what is wrong.
I came to this, where if I comment line 13 ( scanf("%d", &age); ) it's starting works ok for second question.
What the problem is?
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <clocale>
int main()
{
int age;
char S1;
printf("How old is your dog? \n");
scanf("%d", &age);
if (age <= 7)
{
printf(" very young. the end \n");
return 0;
}
else
{
printf("old dog. \n \n");
}
//question2
printf("Do you like dogs? y/n \n");
scanf("%c%c", &S1);
if (S1 == 'y')
{
printf("hey, that's nice \n");
}
else
{
printf(" that's sad :( . \n");
return 0;
}
return 0;
}
You cause undefined behavior by
scanf("%c%c", &S1);
scanf reads two chars, one stored in S1, one stored in some location on the stack because scanf expects a second char* to be supplied.
If your intention is to ignore the newline following the actual character, write
scanf("%c%*c", &S1);
Change the second scanf() to
scanf(" %c", &S1);
This would escape the left out newline character \n in the input buffer.
Plus, you are reading one char in this. So you need only one %c
scanf("%c", &S1);
is the correct way to input one character ,

Getting multiple character inputs inside for loop

char c;
int i;
for(i=0;i<5;i++)
{
printf("Enter a character : ");`
scanf("%c",&c);
}
getch();
The above code doesn't work properly.It is getting input for only 3 times. I am not able to find solution for it.Please help with it.Thanks in advance !!
This is because of the new-line character \n left behind by the previous scanf is read by the scanf in the next iteration. Place a space before the %c specifier to consume the \n
scanf(" %c",&c);
^Notice the space
char c is a single character variable.
In your code, your are reading time and time again characters and storing it into c, effectively overwriting it each time. If you would like to input multiple characters, consider using a character array likewise:
#include <stdio.h>
int
main (void)
{
char c[5];
int counter;
for (counter = 0; counter < 5; counter++)
{
printf ("Enter a character: ");
scanf ("%c\n", &c[i]);
}
printf ("The string you input is %s\n", c);
return 0;
}
Suppose you enter a, b and c on the screen then it is actually taking 5 inputs
Input 1: a
Input 2: \n
Input 3: b
Input 4: \n
Input 5: \n
The newline is also taken as input

Resources