cfgetsstrncpy

How to remove weird character error in C?


so in the last printf, i wanted to take 2 first characters from user input using fgets. I did that but it seems there is a problem with these 2 digits outputs. There's an unexpected character in the last word, how do i remove that? Im using code block C Languange and with GNU GCC Compiler.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
 char nama[20],alamat[30],kota[20],jenis_kelamin[2];
 char nama2[4],alamat2[4],kota2[4];
 int nomor;

 printf("Enter your name                                   = ");
 fgets(nama,20,stdin);
 printf("Nama                                              = %s\n",nama);

 printf("Enter your address                                = ");
 fgets(alamat,30,stdin);
 printf("Alamat                                            = %s\n",alamat);

printf("Enter your city name                               = ");
fgets(kota,20,stdin);
printf("Kota                                              = %s\n",kota);

printf("Masukkan Jenis Kelamin Anda Dengan Huruf L atau P = ");
fgets(jenis_kelamin,2,stdin);
printf("Kelamin                                           = %s\n",jenis_kelamin);

printf("\nMasukkan Nomor NIM                                = ");
scanf("%d",&nomor);
printf("Nomor                                             = %d\n",nomor);

//strncpy(nama2,nama,2);
//strncpy(alamat2,alamat,2);
//strncpy(kota2,kota,2);

printf("\n*******************************************************\n");
printf(" Your name is                   * %s",nama);
printf(" Your address is                * %s",alamat);
printf(" Your cith name is              * %s",kota);
printf(" Jenis Kelamin anda adalah      * %s\n",jenis_kelamin);
printf(" Nomor NIM anda adalah          * %d ",nomor);
printf("\n*******************************************************\n");

strncpy(nama2,nama,2);
fflush(stdin);
strncpy(alamat2,alamat,2);
fflush(stdin);
strncpy(kota2,kota,2);

printf(" 2 First Character of your Name* %s\n",nama2);
printf(" 2 First Character of your Address* %s\n",alamat2);
printf(" 2 First Character of your City* %s",kota2);
printf("\n*******************************************************\n");

return 0;

}

However when i run this the output is not what i expected:

*******************************************************
Your name is                   * Mark
Your address is                * GPA
Your city name is              * New York
Jenis Kelamin anda adalah      * L
Nomor NIM anda adalah          * 13
******************************************************
2 First Character of your Name          * Ma√⌂L
2 First Character of your Address       * GPå¬Ma√⌂L
2 First Character of your City          * Ne
*******************************************************

There is a weird character in the last word of name and city. Like å¬Ma√⌂L and √⌂L

It should be like this:

******************************************************
2 First Character of your Name          * Ma
2 First Character of your Address       * GP
2 First Character of your City          * Ne
*******************************************************

How to solve this? What did i miss? Thank you.


Solution

  • You did not copy zero terminating character. strncpy does not copy it when the length of string exceeds the limit.

    strncpy(alamat2,alamat,2);
    alamat2[2] = '\0';
    // etc...
    

    Let's say fflush(stdin); is invalid. Do not use it.