0

I am working on a school project where I have to make a program that let the user enter information about new books, delete books or display all registered books.

The unexpected behavior is when I choose to display all books : THE SECOND BOOK'S TITLE ALWAYS print some strange random characters or about 15 empty lines while the rest remain normal.

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

typedef struct {
    int Pages,RlsYear,AtrNbr; //"Pages" stands for "the number of pages", "RlsYear" stands for "Release Year"
    char Title[128],Genre[128],Authors[256];
}book;

int main()
{
    int i,nb;
    char ch1,Tmp[128],cln; 
    book* b = malloc(1 * sizeof(book));

    nb=0;
    puts("\n Enter your choice : ");
    ch1=getch();

    while (ch1 != EOF && ch1 != 'q')
    {
        switch(ch1) {
            case 'n': // N to enter a new book
            case 'N':
            {
                printf("\n enter the book's Title : ");
                gets(b[nb].Title);
                printf(" enter the book's Genre : ");
                gets(b[nb].Genre);
                printf(" how many pages does the book have ? ");
                scanf("%d",&b[nb].Pages);
                gets(cln); //i added those "gets(cln)" to avoid problems from "scanf" so you can just ignore them
                printf(" when was the book released (year)? ");
                scanf("%d",&b[nb].RlsYear);
                gets(cln);
                printf(" how many authors does this book have ? ");
                scanf("%d",&b[nb].AtrNbr);
                gets(cln);
                strcpy(b[nb].Authors,"");

                for(i=0;i<b[nb].AtrNbr;i++)
                {
                    printf("\t\t enter the %d author : ",i+1);
                    gets(Tmp);
                    strcat(b[nb].Authors,Tmp);
                    strcat(b[nb].Authors," | ");
                }
                nb=nb+1;
                book* B = realloc(b, nb+1 * sizeof(book));
                b = B;
            }
            break;

            case 'i': // I to display registered books
            case 'I':
            {
                for(i=0;i<nb;i++)
                    printf("\n %s",b[i].Title);
            }
            break;

             default:
                 printf("unknown choice !");
                 break;
        }
        puts("\n Enter your choice : ");
        ch1=getch();
    }
    return 0;
}
Shawn
  • 47,241
  • 3
  • 26
  • 60
iyy0v
  • 51
  • 1
  • 5
  • 2
    Step 1, Use `int ch1`, not `char ch1` to handle the typically 257 different return values from `getch()`. – chux - Reinstate Monica Mar 21 '20 at 19:42
  • 1
    Post your _exact_ input. If is like `N`, `'\n'`, `"book title...\n"`, then code has not consumed the `'\n'` before advancing to read the title. So the title becomes `""`. – chux - Reinstate Monica Mar 21 '20 at 19:44
  • Where in the world did you find something saying to use `gets()`? That function is so unsafe it's the only one to have been removed from the standard. – Shawn Mar 21 '20 at 20:13
  • The compiler might be telling you that `gets(cln);` has the wrong argument type anyway, because `char cln` is not an array. IMO it's a mistake to mix your input methods anyway, and trying to "fix" the behaviour of `scanf`. – Weather Vane Mar 21 '20 at 20:47
  • @Shawn when i looked about it here. Actually **gets()** worked fine while **scanf()** caused me a lot of trouble – iyy0v Mar 22 '20 at 17:21
  • @WeatherVane even when i removed it, the problem was still there. Also how can i "fix" the behaviour of **scanf**. – iyy0v Mar 22 '20 at 17:24
  • @chux-ReinstateMonica But the problem is the error appears only in the SECOND title ! – iyy0v Mar 22 '20 at 17:25
  • @Shawn i just tried switching to **scanf()** and the problem is still there ! – iyy0v Mar 22 '20 at 18:23
  • 2
    You are using `getch()` and `scanf()` and `gets()`. As mentioned, don't mix your methods, and stop using `gets()` completely because it is no longer a C function. Please read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Weather Vane Mar 22 '20 at 18:40

1 Answers1

0

I solved the problem when i changed the b.Title size from 128 to 60. Apparently the problem appears when the b.Title size passes 80. Can someone explain please? i'm more confused now

iyy0v
  • 51
  • 1
  • 5