Search code examples
cfunctionstructstructure

please check the program it automatically takes the newline when i input in the console


thanks for the previous help

now i am facing issues with the output it automatically takes newline \n when i input in the console as evident from the screenshot i am attaching

please identify the issues

PS: if anyone could tell me what is "stdin" i would be really greatful NOTE: i just updated the code please have a look


#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


Solution

  • You are invoking undefined behavior by not matching arguments in function declaration and function call.

    You should remove the not passed and harmful (shadowing global variables) arguments from the function definition.

    Also you should add headers and declarations of function to use before points where the functions are used.

    /* 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");
        }
       
      }
    }
    

    The next step will be stop using gets(), which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11. Using fgets() and removing newline characters in the buffer (strtok() with the delimiter "\n" is useful) will be the alternative.