尝试在c中的结构中获取输入时出错,fgets()不';我不能解决它

尝试在c中的结构中获取输入时出错,fgets()不';我不能解决它,c,C,我碰巧在从我的结构中获取输入时遇到了问题。gets()有问题。我阅读了一些问题的答案并使用了fgets(),但我仍然有同样的问题。谁能帮帮我吗 #include < stdio.h > #include < string.h > #include < stdlib.h > #include < conio.h > struct liber { char titulli[50]; char autori[20]; ch

我碰巧在从我的结构中获取输入时遇到了问题。gets()有问题。我阅读了一些问题的答案并使用了fgets(),但我仍然有同样的问题。谁能帮帮我吗

#include < stdio.h > 
#include < string.h >
#include < stdlib.h > 
#include < conio.h >
  struct liber {
    char titulli[50];
    char autori[20];
    char kategoria[20];
    char id[8]; //structure for books
    int kopje;
  }
libra[50];
/*this is another structure meant to be used for clients bu i havent used it yet :)*/
struct klient {
  char emri[20];
  char mbiemri[20];
  char tel[13];
  char id[8];
}
klient[20];

int nr = 0;

void shtoliber() {
  int i, n = 0; //the function that adds the book in file
  FILE * flib;
  flib = fopen("libra.dat", "wb");
  if (flib == NULL) {
    printf("\nGabim ne hapjen e file!!");
    getch();
    exit(1);
  }
  printf("\n\tShkruani numrine librave qe doni te shtoni: ");
  scanf("%d", & n);
  for (i = 0; i < n; i++) {
    printf("\nTitulli: ");
    fgets(libra[nr].titulli, 50, stdin);
    printf("\nAutori: ");
    fgets(libra[nr].autori, 20, stdin);

    printf("\nKategoria: ");
    fgets(libra[nr].kategoria, 20, stdin);
    printf("\nID: ");
    fgets(libra[nr].id, 10, stdin);
    printf("\nKopje: ");
    scanf("%d", & libra[nr].kopje);
    printf("\n*****************\n");
    fwrite( & libra[nr], sizeof(struct liber), 1, flib);
    printf("\nLibri u regjistrua.\n\v");
    nr++;

  }
  fclose(flib);
}

void kerko_titulli() { //this function is used to search books
  FILE * flib;
  flib = fopen("libra.dat", "rb");
  if (flib == NULL) {
    printf("Gabim ne leximin e file!!");
    return;
  }

  printf("\nKerkim sipas autorit: ");
  char v_autori[50];
  printf("\nJepni autorin e librit qe kerkoni: ");
  scanf("%s", v_autori); {
    nr = 0;
    while (fread( & libra[nr], sizeof(struct liber), 1, flib) == 1);
    if (strcmp(v_autori, libra[nr].autori) == 0) {
      printf("\n\nLibri u gjet : ");
      printf("\nTitulli: ");
      puts(libra[nr].titulli);
      printf("\nAutori:");
      puts(libra[nr].autori);
      printf("\nKategoria:");
      puts(libra[nr].kategoria);
      printf("\nID:");
      puts(libra[nr].id);
      exit(1);
      nr++;
    }
  }
  fclose(flib);
}

void menu() {
  printf("  \t*********************");
  printf("  Detyre Kursi");
  printf("  *********************");
  printf("\n1. Shto libra");
  printf("\n2. kerko libra");
  printf("\ne Exit");
  printf("\n\nZgjedhja: ");

}
int main() {
  char z;
  do {
    menu();
    scanf("%d", & z);
    if (z == 1) shtoliber();
    else if (z == 2) kerko_titulli();

  }
  while (z != 'e' && z != 'E' && z != 27);
}
#包括
#包括
#包括
#包括
结构自由{
char tituli[50];
char-autori[20];
char kategoria[20];
char id[8];//书籍的结构
内科普杰;
}
天秤座[50];
/*这是另一个用于客户的结构,但我还没有使用它:)*/
结构客户端{
charemri[20];
char-mbiemri[20];
电话[13];
字符id[8];
}
克利特[20];
int nr=0;
void shtoliber(){
int i,n=0;//在文件中添加书本的函数
文件*flib;
flib=fopen(“天平数据”、“wb”);
如果(flib==NULL){
printf(“\nGabim ne hapjen e file!!”);
getch();
出口(1);
}
printf(“\n\tShkruani numrine librave qe doni te shtoni:”);
scanf(“%d”和“&n”);
对于(i=0;i
输出

Shkruani-numrin e-librave qe-doni-te-shtoni:1
提图利:
自动识别:u

它不会得到Titelli的第一个输入:
在我声明了要添加的图书数量后,它会立即请求第二个输入,而不会得到第一个。

混合调用
scanf
fgets
是个坏主意。原因是
scanf
只消耗一行的一部分,剩下的一行将用于下一个
fgets
。余数通常只是换行符,因此下一个
fgets
读取一个空行

解决方案是使用
fgets
读取每一行,然后使用
sscanf
从该行提取信息。例如,以下两行:

printf("\n\tShkruani numrine librave qe doni te shtoni: ");
scanf("%d",&n);
应改为:

printf("\n\tShkruani numrine librave qe doni te shtoni: ");
char line[256];
if (fgets(line, sizeof(line), stdin) == NULL) {
    // handle error
}
if (sscanf(line, "%d", &n) != 1) {
    // handle error
}

是的,代码确实需要检查每个
fgets
sscanf
的返回值,因为当用户输入无效时,它们会失败。

谢谢,但我如何修复它,因为我无法移除扫描。很抱歉,我在这方面是个乞丐。非常感谢。现在它工作得很好:)