may i have some help? Here's my script.
#include <stdio.h>
void main(void) {
int u;
char ktp;
printf("Masukkan umur anda : ", &u);
scanf ("%i ", &u);
printf("Apakah anda memiliki ktp (y atau n): ", &ktp);
scanf ("%c", &ktp);
if (u>=17 && ktp=='y') {
printf("Selamat, anda layak untuk memilih.");
} else if(u<17) {
printf("Mohon maaf, anda belum layak untuk memilih.");
} else {
printf("Tolong masukan input yang benar.");
}
}
1.First of all, why i cant input the second input?? when I inputed the variable u, the value is immediately displayed without inputting the variable ktp. 2.how to make selection with yes or no and scan it to compare to the if else statement.
CodePudding user response:
1.First of all, why i cant input the second input?? when I inputed the variable u, the value is immediately displayed without inputting the variable ktp.
In scanf ("%i ", &u);
, the space in the format string tells scanf
to consume white space characters, which includes the new-line character. Thus, when you enter a number and press enter, which sends your input and a new-line character to the program, scanf
reads the new-line character and continues trying to read. It waits for more input, so your program does not proceed to the second prompt.
To fix this, remove the space, changing the call to scanf("%i", &u);
. Then, when scanf
finishes reading the number and reads the next character, which is not part of the number, it will put that next character back in the input stream and return. Then your program will continue to the next prompt and the next scanf
.
Change that second scanf
to scanf(" %c", &ktp);
. Here, the space will also tell scanf
to consume white space characters, which you want it to do to consume the new-line that is still in the stream. After it consumes the white space characters, it will put the next character in ktp
.
2.how to make selection with yes or no and scan it to compare to the if else statement.
Use strcmp
to compare strings:
if (u >= 17 && strcmp(ktp, "y") == 0)
strcmp
returns 0 the two strings passed to it are the same, a negative number if the first string is earlier in sort order, and a positive number if the second string is later.
CodePudding user response:
#include <stdio.h>
void main(void) {
int u;
char ktp;
printf("Masukkan umur anda : ");/*", &u" - this is meaningless*/
scanf ("%d", &u);\\%*c
printf("Apakah anda memiliki ktp (y atau n): "); /*, &ktp); - this is meaningless*/
scanf (" %c", &ktp);
if (u>=17 && ktp=='y') {
printf("Selamat, anda layak untuk memilih.");
} else if(u<17) {
printf("Mohon maaf, anda belum layak untuk memilih.");
} else {
printf("Tolong masukan input yang benar.");
}
}
The problem as @weather-vane write is that scanf takes your new-line into the buffer and not as an ending input.
also, you tried to print &u while you didn't used the printf %d to print it. anyway, it is undefined variable so you shouldn't do that.
CodePudding user response:
In these two calls of printf
printf("Masukkan umur anda : ", &u);
printf("Apakah anda memiliki ktp (y atau n): ", &ktp);
the argument expressions &u
and &ktp
are not used.
So rewrite them like
printf("Masukkan umur anda : " );
printf("Apakah anda memiliki ktp (y atau n): " );
Remove the space character from the format string in this call of scanf
scanf ("%i ", &u);
That is write
scanf ("%i", &u);
Also as @Weather Vane
wrote in a comment if you want the entered number is interpreted as a decimal constant even if it is preceded by the 0 then use the conversion specifier %d
instead of %i
.
scanf ("%d", &u);
After this call of scanf the input buffer contains the new line character '\n'
that corresponds to the pressed Enter key.
So the following call of scanf
scanf ("%c", &ktp);
reads this new line character in the variable ktp
. You need to skip it. To do that prepend the format string with a space character like
scanf ( " %c", &ktp);
^^^^^
From the C Standard (7.21.6.2 The fscanf function)
8 Input white-space characters (as specified by the function) are skipped, unless the specification includes a [, c, or n specifier.
and
5 A directive composed of white-space character(s) is executed by reading input up to the first nonwhite-space character (which remains unread), or until no more characters can be read. The directive never fails.
Pay attention to that according to the C standard the function main without parameters shall be declared like
int main( void )