【问题标题】:please check the program it automatically takes the newline when i input in the console [closed]请检查当我在控制台中输入时它自动换行的程序[关闭]
【发布时间】:2021-09-07 08:10:18
【问题描述】:

感谢之前的帮助

现在我面临输出问题 当我在控制台中输入时,它会自动换行 \n 从我附加的屏幕截图中可以看出

请指出问题

PS:如果有人能告诉我什么是“stdin”,我会非常感激 注意:我刚刚更新了代码,请看一下


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

void input();
void output();

struct book
{
  char title[70],id[70],aname[70],price[5];

}b1,b2;

void main()
{
  input();
  output();
}

void input()
{
  int i;
  char t[70],in[70],p[5],an[70];

  for(i=1;i<3;++i)
  {
    printf("type the ID for book %d:",i);
    fgets(in,70,stdin);

    printf("type the title for book %d:",i);
    fgets(t,70,stdin);

    printf("type the author name for book %d:",i);
    fgets(an,70,stdin);

    printf("type the price for book %d:",i);
    fgets(p,5,stdin);

    printf("\n");

    if(i==1)
    {
      strcpy(b1.id,in);
      strcpy(b1.title,t);
      strcpy(b1.aname,an);
      strcpy(b1.price,p);
    }
    else if(i==2)
    {
      strcpy(b2.id,in);
      strcpy(b2.title,t);
      strcpy(b2.aname,an);
      strcpy(b2.price,p);
    }
   
  }

}

void output()
{
  printf("Sr.No.\t\tID\t\tTITLE\t\tAUTHOR NAME\t\tPRICE\n");

  for(int i=1;i<=2;i++)
  {
    if(i==1)
    {
      printf("%d\t\t%s\t\t%s\t\t%s\t\t%s\t\t",i,b1.id,b1.title,b1.aname,b1.price);
      printf("\n");
    }
    if(i==2)
    {
      printf("%d\t\t%s\t\t%s\t\t%s\t\t%s\t\t",i,b2.id,b2.title,b2.aname,b2.price);
      printf("\n");
    }
    
  }
}

enter image description here

【问题讨论】:

  • 您正在调用函数 input() 和 output() 而不指定参数;.
  • 几乎可以肯定,gets(p); 会造成破坏。
  • 如果您输入超过 4 个字符的价格,所有的地狱都会崩溃。 gets()不知道你只分配了char p[5],你也没办法说出来。这就是为什么 gets() 已从语言中删除的原因,因为无法安全地使用它。
  • 请求更新程序的帮助
  • 如果有人能告诉我标准输入是什么,我会很高兴

标签: c function struct structure


【解决方案1】:

您正在调用未定义的行为,因为函数声明和函数调用中的参数不匹配。

您应该从函数定义中删除未传递和有害(隐藏全局变量)的参数。

您还应该在使用函数的点之前添加要使用的函数的标题和声明。

/* add headers */
#include <stdio.h>
#include <string.h>

struct book
{
  char title[70],id[70],aname[70],price[5];

}b1,b2,b3;

/* add declarations of functiosn to use */
void input(void);
void output(void);

int main(void) /* use standard signature of main() */
{
  input();
  output();
  return 0;
}

void input(void) /* remove arguments */
{
  int i;
  char t[70],in[70],p[5],an[70];

  for(i=1;i<=3;++i)
  {
    printf("type the ID for book %d:",i);
    gets(in);

    printf("type the title for book %d:",i);
    gets(t);

    printf("type the author name for book %d:",i);
    gets(an);

    printf("type the price for book %d:",i);
    gets(p);

    if(i==1)
    {
      strcpy(b1.id,in);
      strcpy(b1.title,t);
      strcpy(b1.aname,an);
      strcpy(b1.price,p);
    }
    else if(i==2)
    {
      strcpy(b2.id,in);
      strcpy(b2.title,t);
      strcpy(b2.aname,an);
      strcpy(b2.price,p);
    }
    else if(i==3)
    {
      strcpy(b3.id,in);
      strcpy(b3.title,t);
      strcpy(b3.aname,an);
      strcpy(b3.price,p);
    }
    
  }
  in[i]='\0';
  t[i]='\0';
  an[i]='\0';
  p[i]='\0';
}

void output(void) /* remove arguments */
{
  printf("Sr.No.\t\tID\t\tTITLE\t\tAUTHOR NAME\t\tPRICE\n");

  for(int i=1;i<=3;i++)
  {
    if(i==1)
    {
      printf("%d\t\t",i);
      printf("%s\t\t",b1.id);
      printf("%s\t\t",b1.title);
      printf("%s\t\t",b1.aname);
      printf("%s\t\t",b1.price);
      printf("\n");
    }
    if(i==2)
    {
      printf("%d\t\t",i);
      printf("%s\t\t",b2.id);
      printf("%s\t\t",b2.title);
      printf("%s\t\t",b2.aname);
      printf("%s\t\t",b2.price);
      printf("\n");
    }
    if(i==3)
    {
      printf("%d\t\t",i);
      printf("%s\t\t",b3.id);
      printf("%s\t\t",b3.title);
      printf("%s\t\t",b3.aname);
      printf("%s\t\t",b3.price);
      printf("\n");
    }
   
  }
}

下一步将停止使用gets(),它具有不可避免的缓冲区溢出风险,在 C99 中已弃用并从 C11 中删除。使用 fgets() 并删除缓冲区中的换行符(strtok() 与分隔符 "\n" 很有用)将是替代方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多